-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkontra.js
957 lines (844 loc) · 25.4 KB
/
kontra.js
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/*
* Kontra.js v3.0.0 (Custom Build on 2017-08-22) | MIT
* Build: https://straker.github.io/kontra/download?files=gameLoop+keyboard+sprite+pool
*/
this.kontra = {
/**
* Initialize the canvas.
* @memberof kontra
*
* @param {string|HTMLCanvasElement} canvas - Main canvas ID or Element for the game.
*/
init: function init(canvas) {
// check if canvas is a string first, an element next, or default to getting
// first canvas on page
var canvasEl = this.canvas = document.getElementById(canvas) ||
canvas ||
document.querySelector('canvas');
if (!this._isCanvas(canvasEl)) {
throw Error('You must provide a canvas element for the game');
}
this.context = canvasEl.getContext('2d');
},
/**
* Noop function.
* @see https://stackoverflow.com/questions/21634886/what-is-the-javascript-convention-for-no-operation#comment61796464_33458430
* @memberof kontra
* @private
*
* The new operator is required when using sinon.stub to replace with the noop.
*/
_noop: new Function,
/*
* Determine if a value is a String.
* @see https://github.com/jed/140bytes/wiki/Byte-saving-techniques#coercion-to-test-for-types
* @memberof kontra
* @private
*
* @param {*} value - Value to test.
*
* @returns {boolean}
*/
_isString: function isString(value) {
return ''+value === value;
},
/**
* Determine if a value is a Number.
* @see https://github.com/jed/140bytes/wiki/Byte-saving-techniques#coercion-to-test-for-types
* @memberof kontra
* @private
*
* @param {*} value - Value to test.
*
* @returns {boolean}
*/
_isNumber: function isNumber(value) {
return +value === value;
},
/**
* Determine if a value is a Function.
* @memberof kontra
* @private
*
* @param {*} value - Value to test.
*
* @returns {boolean}
*/
_isFunc: function isFunction(value) {
return typeof value === 'function';
},
/**
* Determine if a value is an Image. An image can also be a canvas element for
* the purposes of drawing using drawImage().
* @memberof kontra
* @private
*
* @param {*} value - Value to test.
*
* @returns {boolean}
*/
_isImage: function isImage(value) {
return !!value && value.nodeName === 'IMG' || this._isCanvas(value);
},
/**
* Determine if a value is a Canvas.
* @memberof kontra
* @private
*
* @param {*} value - Value to test.
*
* @returns {boolean}
*/
_isCanvas: function isCanvas(value) {
return !!value && value.nodeName === 'CANVAS';
}
};
(function(kontra, requestAnimationFrame, performance) {
/**
* Game loop that updates and renders the game every frame.
* @memberof kontra
*
* @param {object} properties - Properties of the game loop.
* @param {number} [properties.fps=60] - Desired frame rate.
* @param {boolean} [properties.clearCanvas=true] - Clear the canvas every frame.
* @param {function} properties.update - Function called to update the game.
* @param {function} properties.render - Function called to render the game.
*/
kontra.gameLoop = function(properties) {
properties = properties || {};
// check for required functions
if ( !(kontra._isFunc(properties.update) && kontra._isFunc(properties.render)) ) {
throw Error('You must provide update() and render() functions');
}
// animation variables
var fps = properties.fps || 60;
var accumulator = 0;
var delta = 1E3 / fps; // delta between performance.now timings (in ms)
var step = 1 / fps;
var clear = (properties.clearCanvas === false ?
kontra._noop :
function clear() {
kontra.context.clearRect(0,0,kontra.canvas.width,kontra.canvas.height);
});
var last, rAF, now, dt;
/**
* Called every frame of the game loop.
*/
function frame() {
rAF = requestAnimationFrame(frame);
now = performance.now();
dt = now - last;
last = now;
// prevent updating the game with a very large dt if the game were to lose focus
// and then regain focus later
if (dt > 1E3) {
return;
}
accumulator += dt;
while (accumulator >= delta) {
gameLoop.update(step);
accumulator -= delta;
}
clear();
gameLoop.render();
}
// game loop object
var gameLoop = {
update: properties.update,
render: properties.render,
isStopped: true,
/**
* Start the game loop.
* @memberof kontra.gameLoop
*/
start: function start() {
last = performance.now();
this.isStopped = false;
requestAnimationFrame(frame);
},
/**
* Stop the game loop.
*/
stop: function stop() {
this.isStopped = true;
cancelAnimationFrame(rAF);
},
// expose properties for testing
_frame: frame,
set _last(value) {
last = value;
}
};
return gameLoop;
};
})(kontra, requestAnimationFrame, performance);
(function() {
var callbacks = {};
var pressedKeys = {};
var keyMap = {
// named keys
8: 'backspace',
9: 'tab',
13: 'enter',
16: 'shift',
17: 'ctrl',
18: 'alt',
20: 'capslock',
27: 'esc',
32: 'space',
33: 'pageup',
34: 'pagedown',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
45: 'insert',
46: 'delete',
91: 'leftwindow',
92: 'rightwindow',
93: 'select',
144: 'numlock',
145: 'scrolllock',
// special characters
106: '*',
107: '+',
109: '-',
110: '.',
111: '/',
186: ';',
187: '=',
188: ',',
189: '-',
190: '.',
191: '/',
192: '`',
219: '[',
220: '\\',
221: ']',
222: '\''
};
// alpha keys
// @see https://stackoverflow.com/a/43095772/2124254
for (var i = 0; i < 26; i++) {
keyMap[65+i] = (10 + i).toString(36);
}
// numeric keys and keypad
for (i = 0; i < 10; i++) {
keyMap[48+i] = ''+i;
keyMap[96+i] = 'numpad'+i;
}
// f keys
for (i = 1; i < 20; i++) {
keyMap[111+i] = 'f'+i;
}
var addEventListener = window.addEventListener;
addEventListener('keydown', keydownEventHandler);
addEventListener('keyup', keyupEventHandler);
addEventListener('blur', blurEventHandler);
/**
* Execute a function that corresponds to a keyboard key.
* @private
*
* @param {Event} e
*/
function keydownEventHandler(e) {
var key = keyMap[e.which];
pressedKeys[key] = true;
if (callbacks[key]) {
callbacks[key](e);
}
}
/**
* Set the released key to not being pressed.
* @private
*
* @param {Event} e
*/
function keyupEventHandler(e) {
var key = keyMap[e.which];
pressedKeys[key] = false;
}
/**
* Reset pressed keys.
* @private
*
* @param {Event} e
*/
function blurEventHandler(e) {
pressedKeys = {};
}
/**
* Object for using the keyboard.
*/
kontra.keys = {
/**
* Register a function to be called on a key press.
* @memberof kontra.keys
*
* @param {string|string[]} keys - key or keys to bind.
*/
bind: function bindKey(keys, callback) {
keys = (Array.isArray(keys) ? keys : [keys]);
for (var i = 0, key; key = keys[i]; i++) {
callbacks[key] = callback;
}
},
/**
* Remove the callback function for a key.
* @memberof kontra.keys
*
* @param {string|string[]} keys - key or keys to unbind.
*/
unbind: function unbindKey(keys) {
keys = (Array.isArray(keys) ? keys : [keys]);
for (var i = 0, key; key = keys[i]; i++) {
callbacks[key] = null;
}
},
/**
* Returns whether a key is pressed.
* @memberof kontra.keys
*
* @param {string} key - Key to check for press.
*
* @returns {boolean}
*/
pressed: function keyPressed(key) {
return !!pressedKeys[key];
}
};
})();
(function(kontra, Math, Infinity) {
/**
* A vector for 2D space.
*
* Because each sprite has 3 vectors and there could possibly be hundreds of
* sprites at once, we can't return a new object with new functions every time
* (which saves on having to use `this` everywhere). Instead, we'll use a
* prototype so vectors only take up an x and y value and share functions.
* @memberof kontra
*
* @param {number} [x=0] - X coordinate.
* @param {number} [y=0] - Y coordinate.
*/
kontra.vector = function(x, y) {
var vector = Object.create(kontra.vector.prototype);
vector._init(x, y);
return vector;
};
kontra.vector.prototype = {
/**
* Initialize the vectors x and y position.
* @memberof kontra.vector
* @private
*
* @param {number} [x=0] - X coordinate.
* @param {number} [y=0] - Y coordinate.
*
* @returns {vector}
*/
_init: function init(x, y) {
this._x = x || 0;
this._y = y || 0;
},
/**
* Add a vector to this vector.
* @memberof kontra.vector
*
* @param {vector} vector - Vector to add.
* @param {number} dt=1 - Time since last update.
*/
add: function add(vector, dt) {
this._x += (vector.x || 0) * (dt || 1);
this._y += (vector.y || 0) * (dt || 1);
},
/**
* Clamp the vector between two points that form a rectangle.
* @memberof kontra.vector
*
* @param {number} [xMin=-Infinity] - Min x value.
* @param {number} [yMin=Infinity] - Min y value.
* @param {number} [xMax=-Infinity] - Max x value.
* @param {number} [yMax=Infinity] - Max y value.
*/
clamp: function clamp(xMin, yMin, xMax, yMax) {
this._clamp = true;
this._xMin = (xMin !== undefined ? xMin : -Infinity);
this._yMin = (yMin !== undefined ? yMin : -Infinity);
this._xMax = (xMax !== undefined ? xMax : Infinity);
this._yMax = (yMax !== undefined ? yMax : Infinity);
},
/**
* Vector x
* @memberof kontra.vector
*
* @property {number} x
*/
get x() {
return this._x;
},
/**
* Vector y
* @memberof kontra.vector
*
* @property {number} y
*/
get y() {
return this._y;
},
set x(value) {
this._x = (this._clamp ? Math.min( Math.max(this._xMin, value), this._xMax ) : value);
},
set y(value) {
this._y = (this._clamp ? Math.min( Math.max(this._yMin, value), this._yMax ) : value);
}
};
/**
* A sprite with a position, velocity, and acceleration.
* @memberof kontra
* @requires kontra.vector
*
* @param {object} properties - Properties of the sprite.
* @param {number} properties.x - X coordinate of the sprite.
* @param {number} properties.y - Y coordinate of the sprite.
* @param {number} [properties.dx] - Change in X position.
* @param {number} [properties.dy] - Change in Y position.
* @param {number} [properties.ddx] - Change in X velocity.
* @param {number} [properties.ddy] - Change in Y velocity.
*
* @param {number} [properties.ttl=0] - How may frames the sprite should be alive.
* @param {Context} [properties.context=kontra.context] - Provide a context for the sprite to draw on.
*
* @param {Image|Canvas} [properties.image] - Image for the sprite.
*
* @param {object} [properties.animations] - Animations for the sprite instead of an image.
*
* @param {string} [properties.color] - If no image or animation is provided, use color to draw a rectangle for the sprite.
* @param {number} [properties.width] - Width of the sprite for drawing a rectangle.
* @param {number} [properties.height] - Height of the sprite for drawing a rectangle.
*
* @param {function} [properties.update] - Function to use to update the sprite.
* @param {function} [properties.render] - Function to use to render the sprite.
*/
kontra.sprite = function(properties) {
var sprite = Object.create(kontra.sprite.prototype);
sprite.init(properties);
return sprite;
};
kontra.sprite.prototype = {
/**
* Initialize properties on the sprite.
* @memberof kontra.sprite
*
* @param {object} properties - Properties of the sprite.
* @param {number} properties.x - X coordinate of the sprite.
* @param {number} properties.y - Y coordinate of the sprite.
* @param {number} [properties.dx] - Change in X position.
* @param {number} [properties.dy] - Change in Y position.
* @param {number} [properties.ddx] - Change in X velocity.
* @param {number} [properties.ddy] - Change in Y velocity.
*
* @param {number} [properties.ttl=0] - How may frames the sprite should be alive.
* @param {Context} [properties.context=kontra.context] - Provide a context for the sprite to draw on.
*
* @param {Image|Canvas} [properties.image] - Image for the sprite.
*
* @param {object} [properties.animations] - Animations for the sprite instead of an image.
*
* @param {string} [properties.color] - If no image or animation is provided, use color to draw a rectangle for the sprite.
* @param {number} [properties.width] - Width of the sprite for drawing a rectangle.
* @param {number} [properties.height] - Height of the sprite for drawing a rectangle.
*
* @param {function} [properties.update] - Function to use to update the sprite.
* @param {function} [properties.render] - Function to use to render the sprite.
*
* If you need the sprite to live forever, or just need it to stay on screen until you
* decide when to kill it, you can set <code>ttl</code> to <code>Infinity</code>.
* Just be sure to set <code>ttl</code> to 0 when you want the sprite to die.
*/
init: function init(properties) {
var temp, animation, firstAnimation, self = this;
properties = properties || {};
// create the vectors if they don't exist or use the existing ones if they do
self.position = (self.position || kontra.vector());
self.velocity = (self.velocity || kontra.vector());
self.acceleration = (self.acceleration || kontra.vector());
self.position._init(properties.x, properties.y);
self.velocity._init(properties.dx, properties.dy);
self.acceleration._init(properties.ddx, properties.ddy);
// default width and height to 0 if not passed in
self.width = self.height = 0;
// loop through properties before overrides
for (var prop in properties) {
self[prop] = properties[prop];
}
self.ttl = properties.ttl || 0;
self.context = properties.context || kontra.context;
// default to rect sprite
self.advance = self._advance;
self.draw = self._draw;
// image sprite
if (kontra._isImage(temp = properties.image)) {
self.image = temp;
self.width = temp.width;
self.height = temp.height;
self.draw = self._drawImg;
}
// animation sprite
else if (temp = properties.animations) {
self.animations = {};
// clone each animation so no sprite shares an animation
for (var name in temp) {
animation = temp[name];
self.animations[name] = (animation.clone ? animation.clone() : animation);
// default the current animation to the first one in the list
if (!firstAnimation) {
firstAnimation = self.animations[name];
}
}
self.currentAnimation = firstAnimation;
self.width = firstAnimation.width;
self.height = firstAnimation.height;
self.advance = self._advanceAnim;
self.draw = self._drawAnim;
}
},
// define getter and setter shortcut functions to make it easier to work with the
// position, velocity, and acceleration vectors.
/**
* Sprite position.x
* @memberof kontra.sprite
*
* @property {number} x
*/
get x() {
return this.position.x;
},
/**
* Sprite position.y
* @memberof kontra.sprite
*
* @property {number} y
*/
get y() {
return this.position.y;
},
/**
* Sprite velocity.x
* @memberof kontra.sprite
*
* @property {number} dx
*/
get dx() {
return this.velocity.x;
},
/**
* Sprite velocity.y
* @memberof kontra.sprite
*
* @property {number} dy
*/
get dy() {
return this.velocity.y;
},
/**
* Sprite acceleration.x
* @memberof kontra.sprite
*
* @property {number} ddx
*/
get ddx() {
return this.acceleration.x;
},
/**
* Sprite acceleration.y
* @memberof kontra.sprite
*
* @property {number} ddy
*/
get ddy() {
return this.acceleration.y;
},
set x(value) {
this.position.x = value;
},
set y(value) {
this.position.y = value;
},
set dx(value) {
this.velocity.x = value;
},
set dy(value) {
this.velocity.y = value;
},
set ddx(value) {
this.acceleration.x = value;
},
set ddy(value) {
this.acceleration.y = value;
},
/**
* Determine if the sprite is alive.
* @memberof kontra.sprite
*
* @returns {boolean}
*/
isAlive: function isAlive() {
return this.ttl > 0;
},
/**
* Simple bounding box collision test.
* @memberof kontra.sprite
*
* @param {object} object - Object to check collision against.
*
* @returns {boolean} True if the objects collide, false otherwise.
*/
collidesWith: function collidesWith(object) {
return this.x < object.x + object.width &&
this.x + this.width > object.x &&
this.y < object.y + object.height &&
this.y + this.height > object.y;
},
/**
* Update the sprites velocity and position.
* @memberof kontra.sprite
* @abstract
*
* @param {number} dt - Time since last update.
*
* This function can be overridden on a per sprite basis if more functionality
* is needed in the update step. Just call <code>this.advance()</code> when you need
* the sprite to update its position.
*
* @example
* sprite = kontra.sprite({
* update: function update(dt) {
* // do some logic
*
* this.advance(dt);
* }
* });
*/
update: function update(dt) {
this.advance(dt);
},
/**
* Render the sprite.
* @memberof kontra.sprite.
* @abstract
*
* This function can be overridden on a per sprite basis if more functionality
* is needed in the render step. Just call <code>this.draw()</code> when you need the
* sprite to draw its image.
*
* @example
* sprite = kontra.sprite({
* render: function render() {
* // do some logic
*
* this.draw();
* }
* });
*/
render: function render() {
this.draw();
},
/**
* Play an animation.
* @memberof kontra.sprite
*
* @param {string} name - Name of the animation to play.
*/
playAnimation: function playAnimation(name) {
this.currentAnimation = this.animations[name];
},
/**
* Move the sprite by its velocity.
* @memberof kontra.sprite
* @private
*
* @param {number} dt - Time since last update.
*/
_advance: function advanceSprite(dt) {
this.velocity.add(this.acceleration, dt);
this.position.add(this.velocity, dt);
this.ttl--;
},
/**
* Update the currently playing animation. Used when animations are passed to the sprite.
* @memberof kontra.sprite
* @private
*
* @param {number} dt - Time since last update.
*/
_advanceAnim: function advanceAnimation(dt) {
this._advance(dt);
this.currentAnimation.update(dt);
},
/**
* Draw a simple rectangle. Useful for prototyping.
* @memberof kontra.sprite
* @private
*/
_draw: function drawRect() {
this.context.fillStyle = this.color;
this.context.fillRect(this.x, this.y, this.width, this.height);
},
/**
* Draw the sprite.
* @memberof kontra.sprite
* @private
*/
_drawImg: function drawImage() {
this.context.drawImage(this.image, this.x, this.y);
},
/**
* Draw the currently playing animation. Used when animations are passed to the sprite.
* @memberof kontra.sprite
* @private
*/
_drawAnim: function drawAnimation() {
this.currentAnimation.render({
context: this.context,
x: this.x,
y: this.y
});
}
};
})(kontra, Math, Infinity);
(function(kontra) {
/**
* Object pool. The pool will grow in size to accommodate as many objects as are needed.
* Unused items are at the front of the pool and in use items are at the of the pool.
* @memberof kontra
*
* @param {object} properties - Properties of the pool.
* @param {function} properties.create - Function that returns the object to use in the pool.
* @param {number} properties.maxSize - The maximum size that the pool will grow to.
*/
kontra.pool = function(properties) {
properties = properties || {};
var lastIndex = 0;
var inUse = 0;
var obj;
// check for the correct structure of the objects added to pools so we know that the
// rest of the pool code will work without errors
if (!kontra._isFunc(properties.create) ||
( !( obj = properties.create() ) ||
!( kontra._isFunc(obj.update) && kontra._isFunc(obj.init) &&
kontra._isFunc(obj.isAlive) )
)) {
throw Error('Must provide create() function which returns an object with init(), update(), and isAlive() functions');
}
return {
create: properties.create,
// start the pool with an object
objects: [obj],
size: 1,
maxSize: properties.maxSize || Infinity,
/**
* Get an object from the pool.
* @memberof kontra.pool
*
* @param {object} properties - Properties to pass to object.init().
*/
get: function get(properties) {
properties = properties || {};
// the pool is out of objects if the first object is in use and it can't grow
if (this.objects[0].isAlive()) {
if (this.size === this.maxSize) {
return;
}
// double the size of the array by filling it with twice as many objects
else {
for (var x = 0; x < this.size && this.objects.length < this.maxSize; x++) {
this.objects.unshift(this.create());
}
this.size = this.objects.length;
lastIndex = this.size - 1;
}
}
// save off first object in pool to reassign to last object after unshift
var obj = this.objects[0];
obj.init(properties);
// unshift the array
for (var i = 1; i < this.size; i++) {
this.objects[i-1] = this.objects[i];
}
this.objects[lastIndex] = obj;
inUse++;
},
/**
* Return all objects that are alive from the pool.
* @memberof kontra.pool
*
* @returns {object[]}
*/
getAliveObjects: function getAliveObjects() {
return this.objects.slice(this.objects.length - inUse);
},
/**
* Clear the object pool.
* @memberof kontra.pool
*/
clear: function clear() {
inUse = lastIndex = this.objects.length = 0;
this.size = 1;
this.objects.push(this.create());
},
/**
* Update all alive pool objects.
* @memberof kontra.pool
*
* @param {number} dt - Time since last update.
*/
update: function update(dt) {
var i = lastIndex;
var obj;
// If the user kills an object outside of the update cycle, the pool won't know of
// the change until the next update and inUse won't be decremented. If the user then
// gets an object when inUse is the same size as objects.length, inUse will increment
// and this statement will evaluate to -1.
//
// I don't like having to go through the pool to kill an object as it forces you to
// know which object came from which pool. Instead, we'll just prevent the index from
// going below 0 and accept the fact that inUse may be out of sync for a frame.
var index = Math.max(this.objects.length - inUse, 0);
// only iterate over the objects that are alive
while (i >= index) {
obj = this.objects[i];
obj.update(dt);
// if the object is dead, move it to the front of the pool
if (!obj.isAlive()) {
// push an object from the middle of the pool to the front of the pool
// without returning a new array through Array#splice to avoid garbage
// collection of the old array
// @see http://jsperf.com/object-pools-array-vs-loop
for (var j = i; j > 0; j--) {
this.objects[j] = this.objects[j-1];
}
this.objects[0] = obj;
inUse--;
index++;
}
else {
i--;
}
}
},
/**
* render all alive pool objects.
* @memberof kontra.pool
*/
render: function render() {
var index = Math.max(this.objects.length - inUse, 0);
for (var i = lastIndex; i >= index; i--) {
this.objects[i].render && this.objects[i].render();
}
}
};
};
})(kontra);