1 decade ago by DaveVoyles
I'm trying to create object pools for my 2D shmup. I can't seem to wrap my head around the pooling though.
Once I reach the maximum number of bullets available in my pool (15), the game just stops firing. Would any of you mind taking a look at my code and seeing if there are any glaringly obvious problems with it?
ig.module(
'game.entities.BulletGenerator'
)
.requires(
'impact.entity',
'game.entities.bullet'
)
.defines(function () {
EntityBulletGenerator = ig.Entity.extend({
_wmIgnore: false,
_wmDrawBox: true,
_wmBoxColor: 'rgba(128, 28, 230, 0.7)',
_wmScalable: true,
lifetime: .5,
particles: 3,
colorOffset: 0,
size: { x: 8, y: 8 },
instances: [],
pool: [],
name: 'bulletGenerator',
maxInstances: 15,
spawnEntity: EntityBullet,
particleLifeTime: 1,
fadetime: 1,
isAlive: null,
init: function (x, y, settings) {
this.parent(x, y, settings);
this.idleTimer = new ig.Timer();
isAlive = true;
},
update: function () {
// Create a new bullet after timer expires
if (ig.input.pressed('altShoot')) {
this.createBullet();
}
},
createBullet: function () {
//console.log("create particle")
var instance;
// Sets X and Y position to spawn bullet at player
var x = ig.game.player.pos.x;
var y = ig.game.player.pos.y;
// As long as our instances is less than max, then spawn a new bullet
if (this.instances.length < this.maxInstances) {
instance = ig.game.spawnEntity(EntityBullet, x, y);
// Add new bullet to end of array, return length of array
this.instances.push(instance);
//TODO: What is the difference between these two? Ask Jesse
// Add new bullet to end of array, return length of array
this.pool.push(instance);
}
// If we've exceeded the max pool size,
else {
// Remove last bullet from array and return it
instance = this.pool.pop();
//TODO: What does this do?
// if (instance)
// instance.reset(x, y);
}
return instance;
},
/********************
* useObject
* Makes use of the object stored in the pool
********************/
useBullet: function (object, attributes){
// Set poolArr and entityType depending on which entity is being used
var poolArr = null;
var entityType = null;
var entity = null;
switch (object) {
case 'bullet':
poolArr = this.instances;
entityType = 'EntityBullet';
break;
}
// Get first entity in relevant pool
entity = this.instances[0];
// If the entity is not already in use...
if (entity.inUse === false) {
// Set any additional attributes
for (var propt in attributes) {
entity[propt] = attributes[propt];
}
// Initialize entity
entity.init();
entity.inUse = true;
// Move the now used entity to the end of the pool
this.moveArrElement(poolArr, 0, poolArr.length - 1);
}
// If the entity IS already in use, either the array is cluttered or there are no free entities left in the pool...
else {
// Loop through pool backwards
for (var i = poolArr.length - 1; i > 0; i--) {
entity = poolArr[i];
// If the entity is not in use, move it to the front of the array
if (!entity.inUse) {
this.moveArrElement(poolArr, i, 0);
}
}
//If no available entities were found, spawn a new entity.
//TODO: We don't want to spawn additional entities. Just keep re-using what we have in the pool
//if (!foundAvailableEntity) {
// ig.game.spawnEntity(entityType, 0, 0);
//}
this.useBullet(object, attributes);
}
},
/********************
* removeEntity
* Deactivates entity by setting inUse bool to false
********************/
removeEntity: function (entity) {
entity.inUse = false;
},
/********************
* clear
* Removes all entities, used to restart a level
********************/
clear: function () {
// For all bullets in the array
for (var i = 0; i < this.instances.length; i++) {
// Kill them
this.instances[i].kill();
}
// Set the total number of items in array to 0
this.instances.length = 0;
}
});
});
Once I reach the maximum number of bullets available in my pool (15), the game just stops firing. Would any of you mind taking a look at my code and seeing if there are any glaringly obvious problems with it?
ig.module(
'game.entities.BulletGenerator'
)
.requires(
'impact.entity',
'game.entities.bullet'
)
.defines(function () {
EntityBulletGenerator = ig.Entity.extend({
_wmIgnore: false,
_wmDrawBox: true,
_wmBoxColor: 'rgba(128, 28, 230, 0.7)',
_wmScalable: true,
lifetime: .5,
particles: 3,
colorOffset: 0,
size: { x: 8, y: 8 },
instances: [],
pool: [],
name: 'bulletGenerator',
maxInstances: 15,
spawnEntity: EntityBullet,
particleLifeTime: 1,
fadetime: 1,
isAlive: null,
init: function (x, y, settings) {
this.parent(x, y, settings);
this.idleTimer = new ig.Timer();
isAlive = true;
},
update: function () {
// Create a new bullet after timer expires
if (ig.input.pressed('altShoot')) {
this.createBullet();
}
},
createBullet: function () {
//console.log("create particle")
var instance;
// Sets X and Y position to spawn bullet at player
var x = ig.game.player.pos.x;
var y = ig.game.player.pos.y;
// As long as our instances is less than max, then spawn a new bullet
if (this.instances.length < this.maxInstances) {
instance = ig.game.spawnEntity(EntityBullet, x, y);
// Add new bullet to end of array, return length of array
this.instances.push(instance);
//TODO: What is the difference between these two? Ask Jesse
// Add new bullet to end of array, return length of array
this.pool.push(instance);
}
// If we've exceeded the max pool size,
else {
// Remove last bullet from array and return it
instance = this.pool.pop();
//TODO: What does this do?
// if (instance)
// instance.reset(x, y);
}
return instance;
},
/********************
* useObject
* Makes use of the object stored in the pool
********************/
useBullet: function (object, attributes){
// Set poolArr and entityType depending on which entity is being used
var poolArr = null;
var entityType = null;
var entity = null;
switch (object) {
case 'bullet':
poolArr = this.instances;
entityType = 'EntityBullet';
break;
}
// Get first entity in relevant pool
entity = this.instances[0];
// If the entity is not already in use...
if (entity.inUse === false) {
// Set any additional attributes
for (var propt in attributes) {
entity[propt] = attributes[propt];
}
// Initialize entity
entity.init();
entity.inUse = true;
// Move the now used entity to the end of the pool
this.moveArrElement(poolArr, 0, poolArr.length - 1);
}
// If the entity IS already in use, either the array is cluttered or there are no free entities left in the pool...
else {
// Loop through pool backwards
for (var i = poolArr.length - 1; i > 0; i--) {
entity = poolArr[i];
// If the entity is not in use, move it to the front of the array
if (!entity.inUse) {
this.moveArrElement(poolArr, i, 0);
}
}
//If no available entities were found, spawn a new entity.
//TODO: We don't want to spawn additional entities. Just keep re-using what we have in the pool
//if (!foundAvailableEntity) {
// ig.game.spawnEntity(entityType, 0, 0);
//}
this.useBullet(object, attributes);
}
},
/********************
* removeEntity
* Deactivates entity by setting inUse bool to false
********************/
removeEntity: function (entity) {
entity.inUse = false;
},
/********************
* clear
* Removes all entities, used to restart a level
********************/
clear: function () {
// For all bullets in the array
for (var i = 0; i < this.instances.length; i++) {
// Kill them
this.instances[i].kill();
}
// Set the total number of items in array to 0
this.instances.length = 0;
}
});
});