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 would like for my enemy to idle in the last direction he was facing, but i'm not exactly sure where to place that code and what verbage to use. Here is the code i have so far...
ig.module(
	'game.entities.smartguy'
)
.requires(
	'impact.entity',
	'plugins.ai'
)
.defines(function(){

EntitySmartguy = ig.Entity.extend({
    animSheet: new ig.AnimationSheet( 'media/enemy_infantry_sprite_sheet.png', 50, 50 ),
    size: {x: 25, y:38},
    offset: {x: 12, y: 8},
    speed: 30,
    type: ig.Entity.TYPE.B,
    checkAgainst: ig.Entity.TYPE.A,
    collides: ig.Entity.COLLIDES.PASSIVE,
    init: function( x, y, settings ) {
    	this.addAnim('idle', 1, [27]);
	this.addAnim('idleleft', 1, [25]);
	this.addAnim('idleright', 1, [24]);
	this.addAnim('idleup', 1, [26]);
	this.addAnim('down', 0.15, [27,22,23]);
	this.addAnim('left', 0.15, [25,16,17]);
	this.addAnim('right', 0.15, [24,18,19]);
	this.addAnim('up', 0.15, [26,20,21]);
	this.addAnim('shootidledown', 0.05, [14,15]);
	this.addAnim('shootidleleft', 0.05, [25,10,11]);
	this.addAnim('shootidleright', 0.05, [24,8,9]);
	this.addAnim('shootidleup', 0.05, [26,12,13]);
	
	ai = new ig.ai(this);
	
	this.parent( x, y, settings );
    },
    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.Attack:
		  this.currentAnim = this.anims.shootidledown;
		  this.vel.x = 0;
		  this.vel.y = 0;
		  ig.game.getEntitiesByType('EntityPlayer')[0].receiveDamage(0,this);
		  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();
    }
});
});

i know i want something like "if last movement was ig.ai.ACTION.MoveLeft then use this.current.Anim = this.anims.idleleft;" I'm just not sure where to put it and what exact words i would use.

1 decade ago by collinhover

Keep state properties such as a facing vec2, and whenever your AI / character moves, set it to the direction of the movement. Ex: when ig.ai.ACTION.MoveLeft then this.facing.x = -1. Don&039;t set your current anim in the switch statement you have for actions, but set them in a separate block that checks for current states instead of actions. Ex: when #this.facing.x < 0 && this.moving === false then this.currentAnim = this.anims.left.

1 decade ago by wytshadow

i tried changing my code to match what you said but i dont think i did something right because now impact wont load. firebug tells me this.facing is not defined. Am i supposed to define it in a different way?

ig.module(
	'game.entities.smartguy'
)
.requires(
	'impact.entity',
	'plugins.ai'
)
.defines(function(){

EntitySmartguy = ig.Entity.extend({
    animSheet: new ig.AnimationSheet( 'media/enemy_infantry_sprite_sheet.png', 50, 50 ),
    size: {x: 25, y:38},
    offset: {x: 12, y: 8},
    speed: 30,
    type: ig.Entity.TYPE.B,
    checkAgainst: ig.Entity.TYPE.A,
    collides: ig.Entity.COLLIDES.PASSIVE,
    init: function( x, y, settings ) {
    	this.addAnim('idle', 1, [27]);
	this.addAnim('idleleft', 1, [25]);
	this.addAnim('idleright', 1, [24]);
	this.addAnim('idleup', 1, [26]);
	this.addAnim('down', 0.15, [27,22,23]);
	this.addAnim('left', 0.15, [25,16,17]);
	this.addAnim('right', 0.15, [24,18,19]);
	this.addAnim('up', 0.15, [26,20,21]);
	this.addAnim('shootidledown', 0.05, [14,15]);
	this.addAnim('shootidleleft', 0.05, [25,10,11]);
	this.addAnim('shootidleright', 0.05, [24,8,9]);
	this.addAnim('shootidleup', 0.05, [26,12,13]);
	
	ai = new ig.ai(this);
	
	this.parent( x, y, settings );
    },
    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;
		  this.facing.x = -1;
		  break;
	       case ig.ai.ACTION.MoveRight : 
		  //this.currentAnim = this.anims.right;
		  this.vel.x = this.speed;
		  this.facing.x = 1;
		  break;
	       case ig.ai.ACTION.MoveUp	: 
		  //this.currentAnim = this.anims.up;
		  this.vel.y = -this.speed;
		  this.facing.y = 1;
		  break;
	       case ig.ai.ACTION.MoveDown	: 
		  //this.currentAnim = this.anims.down;
		  this.vel.y = this.speed;
		  this.facing.y = -1;
		  break;
	       case ig.ai.ACTION.Attack:
		  this.currentAnim = this.anims.shootidledown;
		  this.vel.x = 0;
		  this.vel.y = 0;
		  ig.game.getEntitiesByType('EntityPlayer')[0].receiveDamage(0,this);
		  break;
               /* use the defaults if no command is send*/
	       default	: 
		  this.currentAnim =  this.anims.idle;
		  this.vel.x = 0;
		  this.vel.y = 0;	
		  break;
	    }
	    if (this.facing.x < 0 && this.moving === false) {
		this.currentAnim = this.anims.left
	    }
	    if (this.facing.x > 0 && this.moving === false) {
		this.currentAnim = this.anims.right
	    }
	    if (this.facing.y < 0 && this.moving === false) {
		this.currentAnim = this.anims.idle
	    }
	    if (this.facing.y > 0 && this.moving === false) {
		this.currentAnim = this.anims.up
	    }
	    this.parent();
    },

1 decade ago by wytshadow

Actually, I'm looking to make the idle animation change based on where the character is so it looks like the enemy is always looking at my player character. How would i go about doing that?

1 decade ago by wytshadow

So what my end goal here is to have my enemy walk up to the player when he is 250 away then have the enemy wait about 100 away until the player crosses the enemy's x or y axis then I want the enemy to shoot 3 times and wait 1 second * random number before he fires another 3 round burst at the player. My question is how do I get the enemy to shoot at the player when he crosses the enemies x or y axis and how do I get the enemy to shoot 3 times then wait to fire again? here is the code for the AI that i have so far.
ig.module(
    'plugins.ai'
)
.defines(function(){
    ig.ai = ig.Class.extend({
        init: function(entity){
  		  ig.ai.ACTION 		= { Rest:0,MoveLeft:1,MoveRight:2,MoveUp:3,MoveDown:4,AttackDown:5,AttackLeft:6,AttackRight:7,AttackUp:8,RestLeft:9,RestRight:10,RestUp:11 };
  		  this.entity  = entity;
        },
        doAction: function(action){
                this.lastAction = action;
                return action;
            },
        //tell the caller what his best action is
  	    getAction: function(entity){
  		this.entity = entity;
  		//by default do nothing
  		var playerList 	  = ig.game.getEntitiesByType('EntityPlayer');
  		var player        = playerList[0];
		var distance      = this.entity.distanceTo(player);
		var angle         = this.entity.angleTo(player);
		var x_dist        = distance * Math.cos(angle);
		var y_dist        = distance * Math.sin(angle);
		var collision     = ig.game.collisionMap ;
		//if collision between the player and the enemy occurs
		var res = collision.trace( this.entity.pos.x,this.entity.pos.y,x_dist,y_dist, 
		this.entity.size.x,this.entity.size.y);
                
                if( res.collision.x){
                    if(angle > 0){return this.doAction(ig.ai.ACTION.MoveUp);}else{return this.doAction(ig.ai.ACTION.MoveDown);}
                }
                if(res.collision.y){
                    if(Math.abs(angle) > Math.PI / 2){return this.doAction(ig.ai.ACTION.MoveLeft)}else{return this.doAction(ig.ai.ACTION.MoveRight);}
                }
                if(distance < 100){
                //Attack when close enough //
                    var decide = Math.random();
                    if(decide < 0.6){return this.doAction(ig.ai.ACTION.AttackDown);}
                    return this.doAction(ig.ai.ACTION.Rest);
                }
		if( distance > 80 && distance < 250) {
			//if you can walk in a straight line: go for it
			if(Math.abs(angle) < Math.PI / 4){ return this.doAction(ig.ai.ACTION.MoveRight); }
                        if(Math.abs(angle) >  3 * Math.PI / 4) {return this.doAction(ig.ai.ACTION.MoveLeft);}
                        if(angle < 0){return this.doAction(ig.ai.ACTION.MoveUp);}
                        return this.doAction(ig.ai.ACTION.MoveDown);
		} 
                return this.doAction(ig.ai.ACTION.Rest);
  	    }
    })
})
Page 1 of 1
« first « previous next › last »