Advertisement
DaveVoyles

Object Pooling - ImpactJS

Apr 29th, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************************************************
  2.  * BulletGenerator.js
  3.  * Creates an array (pool) of bullets to be used by the player
  4.  * with the intention of improved performance.
  5.  *
  6.  * The base of this code can be attributed to Liza Shulyayeva's object
  7.  * pooling tutorial: http://liza.io/a-first-try-at-object-pooling/
  8.  * @copyright (c) 2013 Dave Voyles, under The MIT License (see LICENSE)
  9.  *********************************************************************/
  10.  
  11. ig.module(
  12.     'game.entities.EnemyBullet01Generator'
  13. )
  14.     .requires(
  15.     'impact.entity'
  16. )
  17.     .defines(function () {
  18.  
  19.         EntityEnemyBullet01Generator = ig.Entity.extend({
  20.             _wmIgnore: true,
  21.             _wmDrawBox: true,
  22.             _wmBoxColor: 'rgba(128, 28, 230, 0.7)',
  23.             _wmScalable: true,
  24.             size: { x: 8, y: 8 },
  25.             instances: [],
  26.             activeInstances: [],
  27.             name: 'bulletGenerator',
  28.             maxInstances: 20,
  29.  
  30.             /********************************************************
  31.             * useObject
  32.             * Makes use of the object stored in the pool
  33.             ********************************************************/
  34.             useBullet: function (object, parent, attributes, opt_xOffset, opt_yOffSet) {
  35.  
  36.                 var entity = null;
  37.                 var parentObject = null;
  38.  
  39.                 if (this.instances.length > 0) {
  40.                     // check to see if there is a spare one
  41.                     entity = this.instances.pop();
  42.                     console.log('if');
  43.                 }
  44.  
  45.                     // Spawn new entity
  46.                 else {
  47.                     entity = ig.game.spawnEntity(object, 0, 0);
  48.                     console.log('else');
  49.                 }
  50.  
  51.                 this.activeInstances.push();
  52.  
  53.                 // Get the parent object (the enemy the bullets will spawn from)
  54.                 var parentObject = parent;
  55.  
  56.                 // Sets optional X and Y position offset from parent
  57.                 // Workaround for firing multiple projectiles for SpinningShip
  58.                 var xPos = opt_xOffset;
  59.                 var yPos = opt_yOffSet;
  60.  
  61.                 // Set bullet position, based on position of parent
  62.                 entity.pos.x = parentObject.pos.x + xPos;
  63.                 entity.pos.y = parentObject.pos.y + yPos;
  64.  
  65.  
  66.                 // Initialize entity
  67.                 //    entity.init();
  68.                 entity.inUse = true;
  69.  
  70.                 //  Set any additional attributes
  71.                 for (var propt in attributes) {
  72.                     entity[propt] = attributes[propt];
  73.                 }
  74.             },
  75.  
  76.             /********************************************************
  77.             * removeEntity
  78.             * Deactivates entity by setting inUse bool to false
  79.             ********************************************************/
  80.             removeEntity: function (entity) {
  81.                 entity.inUse = false;
  82.  
  83.                 // find the active bullet and remove it
  84.                 // NOTE: Not using indexOf since it wont work in IE8 and below
  85.                 for (var i = 0, l = this.activeInstances.length; i < l; i++)
  86.                     if (this.activeInstances[i] == entity)
  87.                         array.slice(i, l);
  88.  
  89.                 // return the bullet back into the pool
  90.                 this.instances.push(entity);
  91.             },
  92.  
  93.  
  94.             /********************************************************
  95.             * clear
  96.             * Removes all entities, used to restart a level
  97.             ********************************************************/
  98.             clear: function () {
  99.                 // For all bullets in the array
  100.                 for (var i = 0; i < this.instances.length; i++) {
  101.                     // Kill them
  102.                     this.instances[i].kill();
  103.                 }
  104.                 // Set the total number of items in array to 0
  105.                 this.instances.length = 0;
  106.             },
  107.         });
  108.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement