Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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();
        }
    }); 
});

1 decade ago by stillen

I think a much simpler approach would be to pass a direction parameter to the bullet entity. Then depending on the direction parameter, set the velocity of the bullet accordingly.

My guess is that something is not being set properly with the ig.ai.ACTION variables

1 decade ago by wytshadow

So I figured it out a different way. I made 2 bullet entities an X and a Y. Then used the flip settings for each one.


Updated Actions
case ig.ai.ACTION.AttackDown:
		  this.currentAnim = this.anims.shootidledown;
		  this.vel.x = 0;
		  this.vel.y = 0;
		  ig.game.spawnEntity('EntityAibullety',this.pos.x,this.pos.y,{flip:this.flip});
		  this.flip = false;
		  break;
	       case ig.ai.ACTION.AttackLeft:
		  this.currentAnim = this.anims.shootidleleft;
		  this.vel.x = 0;
		  this.vel.y = 0;
		  ig.game.spawnEntity('EntityAibulletx',this.pos.x,this.pos.y,{flip:this.flip});
		  this.flip = true;
		  break;
	       case ig.ai.ACTION.AttackRight:
		  this.currentAnim = this.anims.shootidleright;
		  this.vel.x = 0;
		  this.vel.y = 0;
		  ig.game.spawnEntity('EntityAibulletx',this.pos.x,this.pos.y, {flip:this.flip});
		  this.flip = false;
		  break;
	       case ig.ai.ACTION.AttackUp:
		  this.currentAnim = this.anims.shootidleup;
		  this.vel.x = 0;
		  this.vel.y = 0;
		  ig.game.spawnEntity('EntityAibullety',this.pos.x,this.pos.y,{flip:this.flip});
		  this.flip = true;
		  break;

Updated Bullet entities
EntityAibulletx = ig.Entity.extend({
        size: {x: 5, y: 3},
        animSheet: new ig.AnimationSheet('media/bullet_x.png',5,3),
        maxVel: {x: 200, y: 0},
        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 ? -9 : 31) , y+12, settings );
		  this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
		  this.addAnim('shootidlex', 1, [0]);
            }, 
        handleMovementTrace: function( res ) {
            this.parent( res );
            if( res.collision.x || res.collision.y ){
                this.kill();
            }
        },
        check: function( other ) {
            other.receiveDamage( 0, this );
            this.kill();
        }
    });
    EntityAibullety = ig.Entity.extend({
        size: {x: 3, y: 5},
	animSheet: new ig.AnimationSheet('media/bullet_y.png',3,5),
        maxVel: {x: 0, y: 180},
        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 ? +16 : 6) , y + (settings.flip ? -8 : 12), settings );
		  this.vel.y = this.accel.y = (settings.flip ? -this.maxVel.y : this.maxVel.y);
		  this.addAnim('shootidley', 1, [0]);
            }, 
        handleMovementTrace: function( res ) {
            this.parent( res );
            if( res.collision.x || res.collision.y ){
                this.kill();
            }
        },
        check: function( other ) {
            other.receiveDamage( 0, this );
            this.kill();
        }
    }); 

Thanks for the help stillen!!
Page 1 of 1
« first « previous next › last »