-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
207 lines (175 loc) · 5.19 KB
/
main.lua
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
require "libs/fsm"
require "libs/gamejolt"
require "libs/vector"
require "libs/utility"
require "libs/linked_list"
love.graphics.setDefaultFilter('nearest', 'nearest', 0)
Viewport = require("libs/viewport")
VHS = require("libs/inputman_recorder")
Input = require("input")
Sound = require("sound")
World = require("world")
Player = require("player/player")
HUD = require("hud")
function love.focus(f) gameIsPaused = not f end
function love.load(args)
love.graphics.setBackgroundColor(0, 0, 0)
view = Viewport.new({
width = global.screen_width,
height = global.screen_height,
scale = global.scale
})
world = World.new()
Input = VHS.new(Input, world)
game_state = require("game")(world)
menu_state = require("menu")
game_state.start()
end
function love.update(dt)
game_state.update(dt)
--Sound:printDebugQueue()
--Input:printDebugQueue()
end
-- Receives keyboard codes and Input states
function love.keypressed(key, isrepeat)
if (not love.window.hasFocus()) then return end
if (key == 'f11') then
view:setFullscreen()
view:setupScreen()
elseif (key == 'f10') then
love.event.quit()
elseif (key == 'q') then
Input:startRecording()
elseif (key:match("^[1-9]$")) then
if Input:isRecording() then
Input:save(key)
else
Input:playback(key)
end
elseif (key == 'r') then
if not Input:isRecording() and not Input:isPlayback() then
game_state.set("reset")
end
elseif (key == '-') then
play_rate = math.min(play_rate * 2, 16)
elseif (key == '=') then
play_rate = math.max(play_rate / 2, 1)
else
-- Pass through any states or non-global keys.
game_state.keypressed(key)
end
end
-- Receives keyboard codes and Input states
function love.keyreleased(key)
if (not love.window.hasFocus()) then return end
game_state.keyreleased(key)
end
function love.gamepadpressed(joystick, button)
if (not love.window.hasFocus()) then return end
end
function love.gamepadreleased(joystick, button)
if (not love.window.hasFocus()) then return end
end
function love.draw()
view:pushScale()
game_state.draw()
view:popScale()
end
function love.resize(w, h)
view:fixSize(w, h)
end
function love.threaderror(thread, errorstr)
print("Thread error!\n"..errorstr)
end
function love.joystickadded (j)
Input:updateJoysticks()
end
function love.joystickremoved (j)
Input:updateJoysticks()
end
function love.processevents()
-- Process events.
love.event.pump()
-- Process Input events into queue
Input:processEventQueue(function(event, states)
for i,state in ipairs(states) do
love.event.push(event, state)
end
end)
for e,a,b,c,d in love.event.poll() do
if e == "quit" then
if not love.quit or not love.quit() then
love.audio.stop()
return
end
end
love.handlers[e](a,b,c,d)
end
return true
end
function love.drawscreen(debug)
if love.window.isCreated() then
love.graphics.clear()
love.graphics.origin()
if love.draw then love.draw() end
-- Print debug information nicely if it was passed along
if (type(debug) == "table") then
local insert = table.insert
local printtable = {}
for k,v in pairs(debug) do
insert(printtable, k .. ": ")
insert(printtable, v)
insert(printtable, "\n")
end
local r,g,b,a = love.graphics.getColor()
love.graphics.setColor(0,0,0)
love.graphics.print(table.concat(printtable), 1, 1)
love.graphics.setColor(r,g,b,a)
love.graphics.print(table.concat(printtable))
end
-- Present the graphics
love.graphics.present()
end
end
function love.run()
love.math.setRandomSeed(os.time())
love.event.pump()
love.load(arg)
love.timer.step()
local t = 0
local updateCount = 0
local frameCount = 0
local internalRate = 1/60
local nextTime = 0
local updateRate = 0
local maxFrameskip = 4
-- Main loop
while true do
-- Tick-count/running time
love.timer.step()
t = t + love.timer.getDelta()
-- Update at constant speed. Game will slow if maxFrameskip exceeded
updateRate = 0
while(t >= nextTime and updateRate < maxFrameskip ) do
-- Update on a fixed timestep
if not love.processevents() then return end
love.update(internalRate)
nextTime = nextTime + (internalRate)
updateCount = updateCount + 1
updateRate = updateRate + 1
-- Update tickcount
love.timer.step()
t = t + love.timer.getDelta()
end
-- Draw
love.drawscreen({
t = t,
updateCount = updateCount,
frameCount = frameCount,
internalFPS = "1/" .. (1/internalRate),
frameskip = updateRate .. ":" .. maxFrameskip
})
frameCount = frameCount + 1
love.timer.sleep(0.001)
end
end