Skip to content

Instantly share code, notes, and snippets.

@photonstorm
Created May 2, 2012 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save photonstorm/2572728 to your computer and use it in GitHub Desktop.
Save photonstorm/2572728 to your computer and use it in GitHub Desktop.
Demo style
/*
Bullet Manager
*/
GAME.BulletManager = function () {
this.data;
this.container;
this.bulletSpeed = 500;
};
GAME.BulletManager.prototype = {
init: function () {
this.data = new Array();
this.container = new CAAT.ActorContainer(CAAT.ActorContainer.AddHint.CONFORM);
this.container.setBounds(0, 0, 640, 480);
this.container.enableEvents(false);
for (var i = 0; i < 150; i++)
{
// trace("i: " + i + " = " + i % 3);
var bullet = new GAME.Bullet( this.container, (i % 3) + 1 );
this.data.push(bullet);
}
},
fire: function ( x, y ) {
var bullet = this.getAvailableBullet(game.loop.player.weaponType);
if (bullet === null)
{
return;
}
if (game.loop.player.weaponType === 1)
{
bullet.fire(x + 12, y, this.bulletSpeed);
}
else if (game.loop.player.weaponType == 2)
{
bullet.fire(x + 9, y - 8, this.bulletSpeed);
}
else if (game.loop.player.weaponType == 3)
{
bullet.fire(x - 8, y - 20, this.bulletSpeed);
}
},
update: function () {
this.collide();
},
collide: function () {
game.loop.enemies.populateCollisionHash();
for (var i = 0; i < this.data.length; i++)
{
var bullet = this.data[i];
if (bullet.alive === true)
{
game.loop.hash.collide( bullet.actor.x, bullet.actor.y, bullet.actor.width, bullet.actor.height, function (obj) { bullet.collide(obj); return true; } );
}
}
},
getAvailableBullet: function ( type ) {
for (var i = 0; i < this.data.length; i++)
{
if (this.data[i].alive === false && this.data[i].bulletType === type)
{
return this.data[i];
}
}
return null;
}
};
/*
A Bullet
*/
GAME.Bullet = function ( parent, type ) {
this.container = parent;
this.actor = new CAAT.Actor().setBackgroundImage(game.director.getImage('bulletType' + type));
this.actor.self = this;
this.alive = false;
this.bulletType = type;
};
GAME.Bullet.prototype = {
constructor: GAME.Bullet,
fire: function ( x, y, speed ) {
this.actor.setPosition(x, y);
this.container.addChild(this.actor);
this.actor.visible = true;
this.alive = true;
var path = new CAAT.LinearPath().setInitialPosition(x, y).setFinalPosition(x, -64);
var time = (speed / 480) * y;
var behavior = this.actor.addBehavior(new CAAT.PathBehavior().setValues(path).setDelayTime(0, time).addListener({ behaviorExpired: this.reset }));
},
collide: function ( alien ) {
this.reset();
game.loop.enemies.kill(alien.id);
},
reset: function ( behavior, time, actor ) {
actor.visible = false;
actor.parent.removeChild(actor);
actor.self.alive = false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment