-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEngine.as
349 lines (291 loc) · 8.66 KB
/
Engine.as
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package net.flashpunk
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import flash.text.TextFormat;
import flash.utils.getTimer;
import flash.utils.Timer;
import net.flashpunk.utils.Draw;
import net.flashpunk.utils.Input;
/**
* Main game Sprite class, added to the Flash Stage. Manages the game loop.
*/
public class Engine extends MovieClip
{
/**
* If the game should stop updating/rendering.
*/
public var paused:Boolean = false;
/**
* Cap on the elapsed time (default at 30 FPS). Raise this to allow for lower framerates (eg. 1 / 10).
*/
public var maxElapsed:Number = 0.0333;
/**
* The max amount of frames that can be skipped in fixed framerate mode.
*/
public var maxFrameSkip:uint = 5;
/**
* The amount of milliseconds between ticks in fixed framerate mode.
*/
public var tickRate:uint = 4;
/**
* Constructor. Defines startup information about your game.
* @param width The width of your game.
* @param height The height of your game.
* @param frameRate The game framerate, in frames per second.
* @param fixed If a fixed-framerate should be used.
*/
public function Engine(width:uint, height:uint, frameRate:Number = 60, fixed:Boolean = false)
{
// global game properties
FP.width = width;
FP.height = height;
FP.halfWidth = width/2;
FP.halfHeight = height/2;
FP.assignedFrameRate = frameRate;
FP.fixed = fixed;
// global game objects
FP.engine = this;
FP.screen = new Screen;
FP.bounds = new Rectangle(0, 0, width, height);
FP._world = new World;
// miscellanious startup stuff
if (FP.randomSeed == 0) FP.randomizeSeed();
FP.entity = new Entity;
FP._time = getTimer();
// on-stage event listener
addEventListener(Event.ADDED_TO_STAGE, onStage);
}
/**
* Override this, called after Engine has been added to the stage.
*/
public function init():void
{
}
/**
* Updates the game, updating the World and Entities.
*/
public function update():void
{
if (FP.tweener.active && FP.tweener._tween) FP.tweener.updateTweens();
if (FP._world.active)
{
Tween.update();
if (FP._world._tween) FP._world.updateTweens();
FP._world.update();
}
FP._world.updateLists();
if (FP._goto) checkWorld();
}
/**
* Renders the game, rendering the World and Entities.
*/
public function render():void
{
// timing stuff
var t:Number = getTimer();
if (!_frameLast) _frameLast = t;
// render loop
FP.screen.swap();
Draw.resetTarget();
FP.screen.refresh();
if (FP._world.visible) FP._world.render();
FP.screen.redraw();
// more timing stuff
t = getTimer();
_frameListSum += (_frameList[_frameList.length] = t - _frameLast);
if (_frameList.length > 10) _frameListSum -= _frameList.shift();
FP.frameRate = 1000 / (_frameListSum / _frameList.length);
_frameLast = t;
FP.screen._debug.text = "NUM: " + FP.world._renderer.numChildren + " FPS: " + FP.frameRate.toFixed();
}
/**
* Override this; called when game gains focus.
*/
public function focusGained():void
{
}
/**
* Override this; called when game loses focus.
*/
public function focusLost():void
{
}
/**
* Sets the game's stage properties. Override this to set them differently.
*/
public function setStageProperties():void
{
stage.frameRate = FP.assignedFrameRate;
stage.fullScreenSourceRect = new Rectangle(0, 0, FP.width, FP.height);
//stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
//stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.LOW;
//stage.scaleMode = StageScaleMode.NO_SCALE;
// stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
/** @private Event handler for stage entry. */
private function onStage(e:Event = null):void
{
// remove event listener
removeEventListener(Event.ADDED_TO_STAGE, onStage);
// add focus change listeners
stage.addEventListener(Event.ACTIVATE, onActivate);
stage.addEventListener(Event.DEACTIVATE, onDeactivate);
// set stage properties
FP.stage = stage;
setStageProperties();
// enable input
Input.enable();
// switch worlds
if (FP._goto) checkWorld();
// game start
init();
// start game loop
_rate = 1000 / FP.assignedFrameRate;
if (FP.fixed)
{
// fixed framerate
_skip = _rate * (maxFrameSkip + 1);
_last = _prev = getTimer();
_timer = new Timer(tickRate);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
}
else
{
// nonfixed framerate
_last = getTimer();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
stage.addChild(FP.screen._debug);
FP.screen._debug.defaultTextFormat = new TextFormat("retro", 32, 0xf9966cc);
FP.screen._debug.filters = [new GlowFilter(0,1,4,4,200,1)]
FP.screen._debug.width = 800;
//FP.screen._debug.height = 100;
FP.screen._debug.multiline = true;
FP.screen._debug.text = "asdfasdfrdfghadfgha\nasdfgasdfg";
}
/** @private Framerate independent game loop. */
private function onEnterFrame(e:Event):void
{
// update timer
_time = _gameTime = getTimer();
FP._flashTime = _time - _flashTime;
_updateTime = _time;
FP.elapsed = (_time - _last) / 1000;
if (FP.elapsed > maxElapsed) FP.elapsed = maxElapsed;
FP.elapsed *= FP.rate;
_last = _time;
// update console
if (FP._console) FP._console.update();
// update loop
if (!paused) update();
// update input
Input.update();
// update timer
_time = _renderTime = getTimer();
FP._updateTime = _time - _updateTime;
// render loop
if (!paused) render();
// update timer
_time = _flashTime = getTimer();
FP._renderTime = _time - _renderTime;
FP._gameTime = _time - _gameTime;
}
/** @private Fixed framerate game loop. */
private function onTimer(e:TimerEvent):void
{
// update timer
_time = getTimer();
_delta += (_time - _last);
_last = _time;
// quit if a frame hasn't passed
if (_delta < _rate) return;
// update timer
_gameTime = _time;
FP._flashTime = _time - _flashTime;
// update console
if (FP._console) FP._console.update();
// update loop
if (_delta > _skip) _delta = _skip;
while (_delta >= _rate)
{
// update timer
_updateTime = _time;
_delta -= _rate;
FP.elapsed = (_time - _prev) / 1000;
if (FP.elapsed > maxElapsed) FP.elapsed = maxElapsed;
FP.elapsed *= FP.rate;
_prev = _time;
// update loop
if (!paused) update();
// update input
Input.update();
// update timer
_time = getTimer();
FP._updateTime = _time - _updateTime;
}
// update timer
_renderTime = _time;
// render loop
if (!paused) render();
// update timer
_time = _flashTime = getTimer();
FP._renderTime = _time - _renderTime;
FP._gameTime = _time - _gameTime;
}
/** @private Switch Worlds if they've changed. */
private function checkWorld():void
{
if (!FP._goto) return;
FP._world.end();
FP._world.updateLists();
if (FP._world && FP._world.autoClear && FP._world._tween) FP._world.clearTweens();
FP._world = FP._goto;
FP._goto = null;
FP.camera = FP._world.camera;
FP._world.updateLists();
FP._world.begin();
FP._world.updateLists();
}
private function onActivate (e:Event):void
{
FP.focused = true;
focusGained();
FP.world.focusGained();
}
private function onDeactivate (e:Event):void
{
FP.focused = false;
focusLost();
FP.world.focusLost();
}
// Timing information.
/** @private */ private var _delta:Number = 0;
/** @private */ private var _time:Number;
/** @private */ private var _last:Number;
/** @private */ private var _timer:Timer;
/** @private */ private var _rate:Number;
/** @private */ private var _skip:Number;
/** @private */ private var _prev:Number;
// Debug timing information.
/** @private */ private var _updateTime:uint;
/** @private */ private var _renderTime:uint;
/** @private */ private var _gameTime:uint;
/** @private */ private var _flashTime:uint;
// FrameRate tracking.
/** @private */ private var _frameLast:uint = 0;
/** @private */ private var _frameListSum:uint = 0;
/** @private */ private var _frameList:Vector.<uint> = new Vector.<uint>;
}
}