1 decade ago by wytshadow
I'm trying to get one of my enemy entities to shoot in all 4 directions but no matter what I do, the bullets only shoot to the right. Can someone please help? I don't know if the problem is when I'm spawning the bullet entity or if its in the bullets code when I define it.
update: function() {
/* let the artificial intelligence engine tell us what to do */
var action = ai.getAction(this);
/* listen to the commands with an appropriate animation and velocity */
switch(action){
case ig.ai.ACTION.Rest:
this.currentAnim = this.anims.idle;
this.vel.x = 0;
this.vel.y = 0;
break;
case ig.ai.ACTION.MoveLeft :
this.currentAnim = this.anims.left;
this.vel.x = -this.speed;
break;
case ig.ai.ACTION.MoveRight :
this.currentAnim = this.anims.right;
this.vel.x = this.speed;
break;
case ig.ai.ACTION.MoveUp :
this.currentAnim = this.anims.up;
this.vel.y = -this.speed;
break;
case ig.ai.ACTION.MoveDown :
this.currentAnim = this.anims.down;
this.vel.y = this.speed;
break;
case ig.ai.ACTION.AttackDown:
this.currentAnim = this.anims.shootidledown;
this.vel.x = 0;
this.vel.y = 0;
ig.game.spawnEntity('EntityAibullet',this.pos.x,this.pos.y).receiveDamage(0,this);
break;
case ig.ai.ACTION.AttackLeft:
this.currentAnim = this.anims.shootidleleft;
this.vel.x = 0;
this.vel.y = 0;
ig.game.spawnEntity('EntityAibullet',this.pos.x,this.pos.y).receiveDamage(0,this);
break;
case ig.ai.ACTION.AttackRight:
this.currentAnim = this.anims.shootidleright;
this.vel.x = 0;
this.vel.y = 0;
ig.game.spawnEntity('EntityAibullet',this.pos.x,this.pos.y).receiveDamage(0,this);
break;
case ig.ai.ACTION.AttackUp:
this.currentAnim = this.anims.shootidleup;
this.vel.x = 0;
this.vel.y = 0;
ig.game.spawnEntity('EntityAibullet',this.pos.x,this.pos.y).receiveDamage(0,this);
break;
case ig.ai.ACTION.RestLeft:
this.currentAnim = this.anims.idleleft;
this.vel.x = 0;
this.vel.y = 0;
break;
case ig.ai.ACTION.RestRight:
this.currentAnim = this.anims.idleright;
this.vel.x = 0;
this.vel.y = 0;
break;
case ig.ai.ACTION.RestUp:
this.currentAnim = this.anims.idleup;
this.vel.x = 0;
this.vel.y = 0;
break;
/* use the defaults if no command is send*/
default :
this.currentAnim = this.anims.idle;
this.vel.x = 0;
this.vel.y = 0;
break;
}
this.parent();
},
receiveDamage: function(value){
this.parent(value);
if(this.health > 0)
ig.game.spawnEntity(EntityDeathExplosion, this.pos.x, this.pos.y, {particles: 4, colorOffset: 0});
},
kill: function(){
this.parent();
ig.game.spawnEntity(EntityDeathExplosion, this.pos.x, this.pos.y, {colorOffset: 0});
}
});
EntityAibullet = ig.Entity.extend({
size: {x: 5, y: 3},
animSheetX: new ig.AnimationSheet('media/bullet_x.png',5,3),
animSheetY: new ig.AnimationSheet('media/bullet_y.png',3,5),
velocity: 1000,
maxVel: {x: 200, y: 200},
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.A,
collides: ig.Entity.COLLIDES.PASSIVE,
init: function(x, y , settings){
this.parent( x + (settings.flip ? +4 : 32) , y+12, settings );
//this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
this.anims.xaxis = new ig.Animation(this.animSheetX,1,[0]);
this.anims.yaxis = new ig.Animation(this.animSheetY,1,[0]);
this.currentAnim = this.anims.xaxis;
// this code here doesnt seem to be working at all
if (ig.ai.ACTION.AttackRight){
this.vel.x = this.velocity;
this.vel.y = 0;
this.currentAnim = this.anims.xaxis;
this.anims.xaxis.flip.x = false;
}
else if (ig.ai.ACTION.AttackLeft){
this.vel.x = -this.velocity;
this.vel.y = 0;
this.currentAnim = this.anims.xaxis;
this.anims.xaxis.flip.x = true;
}
else if (ig.ai.ACTION.AttackUp){
this.vel.x = 0;
this.vel.y = -this.velocity;
this.currentAnim = this.anims.yaxis;
this.anims.yaxis.flip.y = false;
}
else if (ig.ai.ACTION.AttackDown){
this.vel.x = 0;
this.vel.y = this.velocity;
this.currentAnim = this.anims.yaxis;
this.anims.yaxis.flip.y = true;
}
},
handleMovementTrace: function( res ) {
this.parent( res );
if( res.collision.x || res.collision.y ){
this.kill();
}
},
check: function( other ) {
other.receiveDamage( 0, this );
this.kill();
}
});
});
