-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathengine.coffee
More file actions
274 lines (211 loc) · 5.67 KB
/
engine.coffee
File metadata and controls
274 lines (211 loc) · 5.67 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
( ->
###*
The Engine controls the game world and manages game state. Once you
set it up and let it run it pretty much takes care of itself.
You can use the engine to add or remove objects from the game world.
There are several modules that can include to add additional capabilities
to the engine.
The engine fires events that you may bind listeners to. Event listeners
may be bound with <code>engine.bind(eventName, callback)</code>
@name Engine
@constructor
@param {Object} I Instance variables of the engine
###
###*
Observe or modify the
entity data before it is added to the engine.
@name beforeAdd
@methodOf Engine#
@event
@param {Object} entityData
###
###*
Observe or configure a <code>gameObject</code> that has been added
to the engine.
@name afterAdd
@methodOf Engine#
@event
@param {GameObject} object The object that has just been added to the
engine.
###
###*
Called when the engine updates all the game objects.
@name update
@methodOf Engine#
@event
@param {Number} elapsedTime The time in seconds that has elapsed since the last update.
###
###*
Called after the engine completes an update. Here it is
safe to modify the game objects array.
@name afterUpdate
@methodOf Engine#
@event
###
###*
Called before the engine draws the game objects on the canvas. The current camera transform is applied.
@name beforeDraw
@methodOf Engine#
@event
@params {PixieCanvas} canvas A reference to the canvas to draw on.
###
###*
Called after the engine draws on the canvas. The current camera transform is applied.
engine.bind "draw", (canvas) ->
# print some directions for the player
canvas.drawText
text: "Go this way =>"
x: 200
y: 200
@name draw
@methodOf Engine#
@event
@params {PixieCanvas} canvas A reference to the canvas to draw on.
###
###*
Called after the engine draws.
The current camera transform is not applied. This is great for
adding overlays.
engine.bind "overlay", (canvas) ->
# print the player's health. This will be
# positioned absolutely according to the viewport.
canvas.drawText
text: "HEALTH:"
position: Point(20, 20)
canvas.drawText
text: player.health()
position: Point(50, 20)
@name overlay
@methodOf Engine#
@event
@params {PixieCanvas} canvas A reference to the canvas to draw on.
###
Engine = (I={}) ->
Object.reverseMerge I,
FPS: 60
paused: false
frameAdvance = false
running = false
startTime = +new Date()
lastStepTime = -Infinity
animLoop = (timestamp) ->
timestamp ||= +new Date()
msPerFrame = (1000 / I.FPS)
delta = timestamp - lastStepTime
remainder = delta - msPerFrame
if remainder > 0
lastStepTime = timestamp - Math.min(remainder, msPerFrame)
step()
if running
window.requestAnimationFrame(animLoop)
update = (elapsedTime) ->
self.trigger "beforeUpdate", elapsedTime
self.trigger "update", elapsedTime
self.trigger "afterUpdate", elapsedTime
draw = ->
return unless canvas = I.canvas
self.trigger "beforeDraw", canvas
self.trigger "draw", canvas
self.trigger "overlay", canvas
step = ->
if !I.paused || frameAdvance
elapsedTime = (1 / I.FPS)
update(elapsedTime)
draw()
self = Core(I).extend {
###*
Start the game simulation.
engine.start()
@methodOf Engine#
@name start
###
start: ->
unless running
running = true
window.requestAnimationFrame(animLoop)
###*
Stop the simulation.
engine.stop()
@methodOf Engine#
@name stop
###
stop: ->
running = false
###*
Pause the game and step through 1 update of the engine.
engine.frameAdvance()
@methodOf Engine#
@name frameAdvance
###
frameAdvance: ->
I.paused = true
frameAdvance = true
step()
frameAdvance = false
###*
Resume the game.
engine.play()
@methodOf Engine#
@name play
###
play: ->
I.paused = false
###*
Toggle the paused state of the simulation.
engine.pause()
@methodOf Engine#
@name pause
@param {Boolean} [setTo] Force to pause by passing true or unpause by passing false.
###
pause: (setTo) ->
if setTo?
I.paused = setTo
else
I.paused = !I.paused
###*
Query the engine to see if it is paused.
engine.pause()
engine.paused()
# true
engine.play()
engine.paused()
# false
@methodOf Engine#
@name paused
###
paused: ->
I.paused
###*
Change the framerate of the game. The default framerate is 30 fps.
engine.setFramerate(60)
@methodOf Engine#
@name setFramerate
###
setFramerate: (newFPS) ->
I.FPS = newFPS
self.stop()
self.start()
update: update
draw: draw
}
self.include Ageable
Engine.defaultModules.each (moduleName) ->
fullModuleName = "Engine.#{moduleName}"
throw "##{fullModuleName} is not a valid engine module" unless Engine[moduleName]
self.include fullModuleName
self.trigger "init"
return self
Engine.defaultModules = [
"Data"
"Keyboard"
"Mouse"
"Background"
"Delay"
"GameState"
"Selector"
"Collision"
"Tilemap"
"Levels"
]
(exports ? this)["Engine"] = Engine
)()