-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine.lua
More file actions
292 lines (240 loc) · 7.07 KB
/
Copy pathEngine.lua
File metadata and controls
292 lines (240 loc) · 7.07 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
--[[
chip.lua: a simple 2D game framework built off of Love2D
Copyright (C) 2024 swordcube
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
]]
---@diagnostic disable: invisible
local tmr = love.timer
local Color = crequire("utils.Color") --- @type chip.utils.Color
local Signal = crequire("utils.Signal") --- @type chip.utils.Signal
---
--- @class chip.Engine
---
local Engine = {}
---
--- The background color of the game.
---
Engine.clearColor = Color.BLACK --- @type chip.utils.Color
---
--- The width of the game area. (in pixels)
---
Engine.gameWidth = 640 --- @type integer
---
--- The height of the game area. (in pixels)
---
Engine.gameHeight = 480 --- @type integer
---
--- The object responsible for scaling the game
--- to the window as it's resized.
---
Engine.scaleMode = nil --- @type chip.graphics.scalemodes.BaseScaleMode?
---
--- The currently active scene.
---
Engine.currentScene = nil --- @type chip.core.Scene
---
--- The time since the last frame. (in seconds)
---
Engine.deltaTime = 0.0 --- @type number
---
--- The target framerate of the game.
---
Engine.targetFPS = 60 --- @type integer
---
--- Controls whether or not the framerate of the game
--- should match the monitor that the window
--- is currently within.
---
Engine.vsync = true
---
--- A list of every added plugin.
---
--- Plugins are responsible for running some of
--- your game related stuff in the background automatically.
---
Engine.plugins = Group:new() --- @type chip.core.Group
---
--- Controls whether or not plugins should
--- be drawn in front of the game.
---
Engine.drawPluginsInFront = true --- @type boolean
---
--- A signal that is fired before the game updates.
---
Engine.preUpdate = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired after the game updates
---
Engine.postUpdate = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired before the game
--- is drawn to the screen.
---
Engine.preDraw = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired before the current
--- scene or any plugin is drawn to the screen.
---
Engine.preSceneDraw = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired after the current
--- scene or any plugin drawn to the screen.
---
Engine.postSceneDraw = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired after the game
--- is drawn to the screen.
---
Engine.postDraw = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired before the game
--- switches scenes.
---
Engine.preSceneSwitch = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired after the game
--- switches to a new scene but before initializing it.
---
Engine.preSceneInit = Signal:new():type(Scene) --- @type chip.utils.Signal
---
--- A signal that is fired after the game
--- switches to a new scene and initializes it.
---
Engine.postSceneSwitch = Signal:new():type(Scene) --- @type chip.utils.Signal
---
--- A signal that is fired when an input
--- event is received.
---
Engine.onInputReceived = Signal:new():type(InputEvent) --- @type chip.utils.Signal
---
--- A signal that is fired when the
--- game window is resized.
---
Engine.onWindowResize = Signal:new():type("number", "number") --- @type chip.utils.Signal
---
--- A signal that is fired when the
--- game window loses focus.
---
Engine.onFocusLost = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired when the
--- game window loses focus.
---
Engine.onFocusGained = Signal:new() --- @type chip.utils.Signal
---
--- A signal that is fired when the
--- game is about to quit.
---
Engine.onQuit = Signal:new() --- @type chip.utils.Signal
---
--- Controls whether or not debug mode is enabled.
---
Engine.debugMode = false --- @type boolean
---
--- Controls whether or not the game will pause upon
--- unfocusing the window.
---
Engine.autoPause = false --- @type boolean
---
--- Controls whether or not the game will update separately
--- from the rendering, causing it to update more and render smoother.
---
Engine.parallelUpdating = true --- @type boolean
---
--- Controls whether or not the game will run at a
--- maximum of 1000 TPS, saving on CPU power.
---
--- Your FPS may be slightly lower as a result aswell.
---
Engine.lowPowerMode = true --- @type boolean
---
--- Controls the general speed of the game, as a multiplier.
---
Engine.timeScale = 1.0 --- @type number
---
--- Controls whether or not the game is paused.
---
--- This does not affect actors whose update mode is
--- set to `always`.
---
Engine.paused = false --- @type boolean
---
--- Returns the amount of frames drawn
--- to the screen in the last second.
---
--- @return integer
---
function Engine.getCurrentFPS()
return Engine._currentFPS
end
---
--- Returns the amount of ticks/update calls
--- ran in the last second.
---
--- @return integer
---
function Engine.getCurrentTPS()
-- This only works because we're not limiting FPS
-- via sleeping, since that's too inaccurate.
-- Instead we are using a simple timer and an if
-- statement to do the job, so this effectively
-- returns into a ticks per second counter.
return tmr.getFPS()
end
---
--- @param newScene chip.core.Scene
---
function Engine.switchScene(newScene)
Engine._canSwitchScene = true
Engine._requestedScene = newScene
end
function Engine.reloadScene()
Engine._canSwitchScene = true
Engine._requestedScene = require(Engine.currentScene.__path):new()
end
--- [ PRIVATE API ] ---
---
--- @protected
--- @type chip.core.Scene?
---
Engine._requestedScene = nil
---
--- @protected
--- @type boolean
---
Engine._canSwitchScene = true
---
--- @protected
--- @type integer
---
Engine._currentFPS = 0
---
--- @protected
---
function Engine._switchScene()
Engine.preSceneSwitch:emit()
if Engine.preSceneSwitch._cancelled then
Engine._canSwitchScene = false
return
end
if Engine.currentScene then
Engine.currentScene:free()
end
Camera.currentCamera = nil
Engine.currentScene = Engine._requestedScene
Engine._requestedScene = nil
Engine.preSceneInit:emit(Engine.currentScene)
Engine.currentScene:init()
Engine.postSceneSwitch:emit(Engine.currentScene)
collectgarbage("collect")
end
return Engine