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

9 years ago by bufk1n

Hey i am working right now on a school Project and me and my friend are trying to move the enemy AI to our position if we get close to him.

The Enemy is a Ghost and should kill us if he touches ther Player.
Also he should move around if the player isnt close to him.

Here ist some Code.

ig.module(
    'game.entities.NPC'
)
.requires(
    'impact.entity'
)
.defines(function(){
    
    
    EntityNPC = ig.Entity.extend({
           size: {x:16 , y:16},
            offset: {x: 16, y: 20},
           
        type: ig.Entity.TYPE.B,
        checkAgainst: ig.Entity.TYPE.B,
        collides: ig.Entity.COLLIDES.PASSIVE,
        friction: {x:0, y:0},
        formerpressed: "down",
        sfxScream: new ig.Sound( 'media/sounds/scream.mp3' ),
    
       
    animSheet: new ig.AnimationSheet ('media/darkgoddess_b.png', 48, 48),
    flip: false,
    
    init: function( x, y, settings ) {
            
        // Add the animations
        this.addAnim( 'idle', 1, [0] );
        this.addAnim( 'walkleft', 0.1, [3,4,5] );
        this.addAnim( 'walkright', 0.1, [6,7,8] );
        this.addAnim( 'walkup', 0.1, [9,10,11] );
        this.addAnim( 'walkdown', 0.1, [0,1,2] );
        this.addAnim( 'lookleft', 1, [4] );    
        this.addAnim( 'lookright', 1, [7] );
        this.addAnim( 'lookup', 1, [10] );    
        this.addAnim( 'lookdown', 1, [0] );    

        this.parent( x, y, settings );
        //MOVEMENT CODE
        movementtimer = new ig.Timer();
        //MOVEMENT CODE 


    //END INIT     
    },
 charactermovement: function(){
          
            var randomdirection=  Math.floor(Math.random()*5)+1;
                    
            if( randomdirection == 1  ) {
                this.vel.x = -64;
                this.vel.y = 0;
                this.currentAnim = this.anims.walkleft;
                this.formerpressed = 'left';
                
            }
            else if( randomdirection == 3  ) {
                this.vel.x = 64;
                this.vel.y = 0;
                this.currentAnim = this.anims.walkright;
                this.formerpressed = 'right';
            }
            else if( randomdirection == 2  ) {
                this.vel.y = -64;
                this.vel.x = 0;
                this.currentAnim = this.anims.walkup;
            
                this.formerpressed = 'up';
            }    
            else if( randomdirection == 4 ) {
                this.vel.y = 64;
                this.vel.x = 0;
                this.currentAnim = this.anims.walkdown;
                
                this.formerpressed = 'down';            
            }
            else 
            { 
                this.vel.y = 0;
                this.vel.x = 0;
                if(this.formerpressed == 'left')
                {            
                    this.currentAnim = this.anims.lookleft;
                }
                else if (this.formerpressed == 'right')
                {            
                    this.currentAnim = this.anims.lookright;
                }
                else if (this.formerpressed == 'up')
                {            
                    this.currentAnim = this.anims.lookup;
                }
                else if (this.formerpressed == 'down')
                {
                    this.currentAnim = this.anims.lookdown;    
                }
            }
        },
        //NO MOVEMENT
        nomovement: function(formerpressed){
             this.vel.y = 0;
             this.vel.x = 0;
             if(this.formerpressed == 'left')
                 {            
                     this.currentAnim = this.anims.lookleft;
                 
                 }
                 else if (this.formerpressed == 'right')
                 {            
                     this.currentAnim = this.anims.lookright;
                 
                 }
                 else if (this.formerpressed == 'up')
                 {            
                     this.currentAnim = this.anims.lookup;
                 
                 }
                 else if (this.formerpressed == 'down')
                 {
                     this.currentAnim = this.anims.lookdown;
                 } 
        },
            //END MOVEMENT CODE 
       
        update: function() {
            if (movementtimer.delta() > 5 && this.vel.y == 0 && this.vel.x == 0  )
            {
                 this.charactermovement();      
            }
           if (movementtimer.delta() > 6 && (this.vel.y > 0 || this.vel.x > 0) )
            {   
                this.nomovement(this.formerpressed);
                movementtimer = new ig.Timer();
            }
           //END MOVEMENT CODE
        this.parent();
       //Demon code
        var deltax = (Math.round(ig.game.player.pos.x-this.pos.x));
        var deltay = (Math.round(ig.game.player.pos.y-this.pos.y));
        
        if(this.distanceTo(ig.game.player)<130){
            
            
            
       //if (Math.sqrt((deltax*deltax)+(deltay*deltay))<130) {
            //if (deltax>0) {
            //    this.vel.x = 72;
            //    this.currentAnim=this.anims.walkdown;
            //    
            //}
            //else if (deltax<0) {
            //    this.vel.x = -72;
            //    this.currentAnim=this.anims.walkdown;
            //}
            //else if (deltax==0) {
            //    this.vel.x = 0
            //    this.currentAnim=this.anims.walkdown;
            //}
            //
            //if (deltay>0) {
            //    this.vel.y = 72;
            //    this.currentAnim=this.anims.walkdown;
            //}
            //else if (deltay<0) {
            //    this.vel.y = -72;
            //    this.currentAnim=this.anims.walkdown;
            //}
            //else if (deltay==0) {
            //    this.vel.y = 0;
            //    this.currentAnim=this.anims.walkdown;
            //}
        }
                 
        if ((Math.abs(deltax)<=3) && (Math.abs(deltay)<=3)){
            console.log('Ich hab dich gefangen');
            if (ig.game.player.key==1) {
                key=1;
            }
            var hearts=ig.game.player.health;
            var xPlayer=ig.game.player.pos.x;
            var yPlayer=ig.game.player.pos.y;
            ig.game.player.kill();
            ig.game.circle.kill();
            var rand = Math.ceil(Math.random()*4);
            switch (rand) {
                case 1:
                    ig.game.spawnEntity('EntityPlayer',350,160);
                    ig.game.spawnEntity('EntityCircle',350,160);
                    ig.game.player.health=hearts -1;
                    this.sfxScream.play();
                    break;
               case 2:
                    ig.game.spawnEntity('EntityPlayer',150,840);
                    ig.game.spawnEntity('EntityCircle',150,840);
                    ig.game.player.health=hearts -1;
                    this.sfxScream.play();
                    break;
                case 3:
                    ig.game.spawnEntity('EntityPlayer',1880,860);
                    ig.game.spawnEntity('EntityCircle',1880,860);
                    ig.game.player.health=hearts -1;
                    this.sfxScream.play();
                    break;
                case 4:
                    ig.game.spawnEntity('EntityPlayer',1760,160);
                   ig.game.spawnEntity('EntityCircle',1760,160);
                   ig.game.player.health=hearts -1;
                   this.sfxScream.play();
                    break;
           }
            if (key==1) {
                ig.game.spawnEntity('EntityKey',xPlayer,yPlayer);
            }
            
        }
       

       
    },
draw: function() {
    this.parent();
    }
            
})
    
});

9 years ago by bufk1n

We tried it with this part of the code

       if (Math.sqrt((deltax*deltax)+(deltay*deltay))<130) {
            if (deltax>0) {
                this.vel.x = 72;
                this.currentAnim=this.anims.walkdown;
                
            }
            else if (deltax<0) {
                this.vel.x = -72;
                this.currentAnim=this.anims.walkdown;
            }
            else if (deltax==0) {
                this.vel.x = 0
                this.currentAnim=this.anims.walkdown;
            }
            
            if (deltay>0) {
                this.vel.y = 72;
                this.currentAnim=this.anims.walkdown;
            }
            else if (deltay<0) {
                this.vel.y = -72;
                this.currentAnim=this.anims.walkdown;
            }
            else if (deltay==0) {
                this.vel.y = 0;
                this.currentAnim=this.anims.walkdown;
            }

But the Animation just freaked out. Maybe someone knows a better Way to let the Entity move to the Pos of the Player ????

It would help a lot!

9 years ago by stahlmanDesign

In my game I had bugs that were attracted to rotting apples (pommes) if they were close enough. Here&039;s the code where the bug Entity is #this.
for (var i in ig.game.pommes){
	var pomme = ig.game.pommes[i];
	var angle = this.angleTo( pomme );

	if (this.distanceTo(pomme) < 100 && pomme.gravityFactor != 0){
		this.vel.x = (Math.cos(angle) * this.speed);
		this.vel.y = (Math.sin(angle) * this.speed);
	}
}
Page 1 of 1
« first « previous next › last »