-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.lua
More file actions
208 lines (184 loc) · 5.03 KB
/
main.lua
File metadata and controls
208 lines (184 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
-- main.lua
local pepperfish_profiler = require "pepperfish_profiler"
local sandbox = require "sandbox"
local gauge = {}
gauge.event = require "event"
gauge.entity = require "entity"
gauge.input = require "input"
gauge.state = require "state"
gauge.map = require "map"
gauge.music = require "music"
gauge.time = require "time"
local tween = require "tween"
local profiler = newProfiler()
profiler:start()
local loading = false
love.load = function ()
local context = gauge.input.context.new({active = true})
context.map = function (raw_in, map_in)
if raw_in.key.pressed["escape"] then
map_in.actions["quit"] = true
end
return map_in
end
gauge.event.subscribe("input",
function (input)
if input.actions.quit then
local quit = love.event.quit or love.event.push
quit("q")
end
end
)
-- Set video mode (use settings.lua if present)
local mode = { width = nil, height = nil }
local fullscreen = true
if love.filesystem.exists("settings.lua") then
local settings = assert(love.filesystem.load("settings.lua"))()
mode = {
width = settings.screen_width or nil,
height = settings.screen_height or nil
}
if settings.fullscreen ~= nil then
fullscreen = settings.fullscreen
end
end
if not mode.width or not mode.height then
local modes = love.graphics.getModes()
table.sort(modes, function(a, b)
return a.width*a.height > b.width*b.height
end)
mode = modes[1]
end
love.graphics.setMode(mode.width, mode.height, fullscreen)
gauge.video_mode = mode -- XXX: ugly hack
local bgm = nil
local old_bgm_file = nil
local game_state = gauge.state.new()
gauge.event.subscribe("loadMap", function (arg)
loading = true
if love.filesystem.exists(arg.file) then
if game_state.map then game_state.map.delete() end
game_state.map = gauge.map.new({
data = love.filesystem.load(arg.file)
})
local bgm_file = game_state.map.properties().bgm
if bgm_file and bgm_file ~= old_bgm_file then
if bgm then bgm.stop() end
bgm = gauge.music.new({file="game/"..bgm_file, volume=1, loop=true})
bgm.play()
old_bgm_file = bgm_file
elseif not bgm_file then
bgm.stop()
old_bgm_file = nil
end
gauge.event.notify("input", {
actions = {reset = true},
states = {},
ranges = {}
})
else
gauge.event.notify("input", {
actions = {quit = true},
states = {},
ranges = {}
})
end
end)
game_state.render = function (lagging)
love.graphics.push()
--love.graphics.scale(game_state.camera.scale)
if game_state.map then
game_state.map.render(lagging)
end
love.graphics.translate(
(love.graphics.getWidth() / 2) - math.floor(game_state.camera.position.x),
(love.graphics.getHeight() / 2) - math.floor(game_state.camera.position.y))
gauge.entity.render()
love.graphics.pop()
end
game_state.update = function (dt)
gauge.entity.update(dt)
end
game_state.map = nil
game_state.camera = {
position = {
x = 0,
y = 0
},
speed = 0.05,
max_distance = 150,
scale = 1,
zoom = false
}
gauge.state.push(game_state)
local untrusted_code = assert(love.filesystem.load("game/main.lua"))
local trusted_code = sandbox.new(untrusted_code, {gauge=gauge, math=math, print=print, tween=tween, love=love})
pcall(trusted_code)
end
frames = 0
skipped = 0
local lagging = false
love.update = function (dt)
if not loading then
local input = gauge.input.update(dt)
if input then
gauge.event.notify("input", input)
end
gauge.time.update(dt)
frames = frames + 1
local updates = 1
if dt > 1/30 then
lagging = true
else
lagging = false
end
if dt > 1/60 then
updates = math.ceil(dt*60)
if updates > 8 then
updates = 8
dt = 1/60
skipped = skipped + 1
else
dt = dt/updates
end
end
local state = gauge.state.get()
for i = 1,updates do
if dt > 0 then
tween.update(dt)
end
state.update(dt)
end
else
loading = false
end
end
love.draw = function ()
gauge.state.get().render(lagging)
end
love.keypressed = function (key, unicode)
gauge.input.keyPressed(key)
end
love.keyreleased = function (key, unicode)
gauge.input.keyReleased(key)
end
love.joystickpressed = function (joystick, button)
gauge.input.joystickPressed(joystick, button)
end
love.joystickreleased = function (joystick, button)
gauge.input.joystickReleased(joystick, button)
end
love.focus = function (f)
end
love.quit = function ()
profiler:stop()
print("Average fps",frames/gauge.time.get())
print("% frames skipped",(skipped/frames) * 100)
local outfile = io.open( "profile.txt", "w+" )
profiler:report( outfile )
outfile:close()
outfile = io.open("report.txt", "w+")
outfile:write("Average fps: ",frames/gauge.time.get())
outfile:write("\n% frames skipped: ",(skipped/frames) * 100)
outfile:close()
end