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 henonchesser

http://chub.henonchesser.com/

Ok so here's the early stages of my game engine. My Character knows if he's holding something and what it is, and the objects know if they're being held and by whom. My problem is that on the death of either entity they both seem to remember this things. I've even overridden the Kill function to forget, but it doesn't. It comes out as some glitchy mess. Death sometimes seems to toggle being able to pick things up... I'm lost.

The BOX
ig.module (
	'game.entities.crate'
	)
.requires (
	'impact.entity'
	)
.defines (function() {

	EntityCrate = ig.Entity.extend ( {

	//LOCAL VARIABLES/////////////////////////////////////////////////////////

		//Size, Bounds, Orientation
		size: { x:16, y:16 },


		//Movement, Gravity, Speed
		maxVel : { x: 100, y: 150}, //max speed the enitity can move on this axis
		friction: { x: 600, y: 0 }, //friction agianst collision layer on an axis

		//Collision, C Type, Checks
		type: ig.Entity.TYPE.A,  //Type A Collider
		checkAgainst: ig.Entity.TYPE.NONE, //Don't check for collisions
		collides: ig.Entity.COLLIDES.ACTIVE, //Get's Pushed

		//custom game variables
		pickedUp: false,
		holder: null,

		/////////////////////////////////////////////////////////////////////////

		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/basegreen.png',  16, 16 ),

		init: function( x, y, settings) {
			this.addAnim('idle',1, [15]);
			//defualt settings, overides anything before it
			this.parent( x, y, settings);

		}, //INITIALIZE 

		kill: function(){
			
			this.pickedUp = false;
			this.holder = null;
			this.parent();

		},

		update: function() {
			this.parent();

		

			this.currentAnim = this.anims.idle;

			if (this.standing) {
				this.friction.y = 1000;
				this.maxVel.y = 0;
			}
			else {
				this.friction.y = 200;
				this.maxVel.y = 150;
			}

			if (this.holder != null && this.pickedUp == true) {

				this.pos.y = this.holder.pos.y;

				//and

				if (this.holder.flip == true){
					this.pos.x = this.holder.pos.x - 14;
					this.collides = ig.Entity.COLLIDES.NEVER;
				}
				else {
					this.pos.x = this.holder.pos.x + 10;
					this.collides = ig.Entity.COLLIDES.NEVER;
				}

			}
			else {
					this.collides = ig.Entity.COLLIDES.ACTIVE
			}
		},

		beACrate: function() {

		},

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

	});
});

--------------------------------------------------------------------------------------------------------------
THE PLAYER
ig.module (
	'game.entities.player'
	)
.requires (
	'impact.entity'
	)
.defines (function() {

	EntityPlayer = ig.Entity.extend ( {

		//LOCAL VARIABLES/////////////////////////////////////////////////////////

		//Size, Bounds, Orientation
		size: { x:12, y:16 },
		offset: { x:2, y:0 },
		flip: false,
		zIndex:50,

		//Movement, Gravity, Speed
		maxVel : { x: 100, y: 150}, //max speed the enitity can move on this axis
		friction: { x: 600, y: 0 }, //friction agianst collision layer on an axis
		accelGround: 400, //movement acceleration on ground
		accelAir: 200,       // ditto only in air
		jump: 120,           // jump  speed, only it's used as a negative later on

		//Collision, C Type, Checks
		type: ig.Entity.TYPE.A,  //Type A Collider
		checkAgainst: ig.Entity.TYPE.NONE, //Don't check for collisions
		collides: ig.Entity.COLLIDES.PASSIVE, //Get's Pushed

		//Custom Game Variables
		flapPower: 5,
		attacking:false,
		blocking:false,
		skin: 0,

		//picking up stuff
		holding: false,
		holdingWhat:null,
		
		/////////////////////////////////////////////////////////////////////////

		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/player.png',  16, 16 ),


		init: function( x, y, settings) {
			//Start the animation
			this.setupAnimation(0);
			//defualt settings, overides anything before it
			this.parent( x, y, settings);

			if(!ig.global.wm) {
				ig.game.spawnEntity(EntityShield,this.pos.x, this.pos.y, {flip: this.flip} );
				ig.game.spawnEntity(EntitySword,this.pos.x, this.pos.y, {flip: this.flip} );
			}
		}, //INITIALIZE 

		setupAnimation: function(skin) { 
				
				this.addAnim( 'idle', 1,  [0+skin]);
				this.addAnim('run', .07, [ 0+skin, 1+skin]);
				this.addAnim('jump', 0.1, [2+skin, 3+skin]);
				this.addAnim('fall', 0.1, [2+skin, 3+skin]);
		}, //SETUP ANIMATION 

		kill: function(){
			this.holding = false;
			this.holdingWhat = null;
			this.parent();
		},

		update: function () {


			//MOVEMENT//////////////////////////////////////////////////////////

			
			if (this.blocking == true) {
				this.maxVel.x = 50;
				this.jump = 60;
			}
			else {
				this.maxVel.x = 100;
				this.jump = 120;
			}

			//movement - left + right
			var accel = this.standing ? this.accelGround : this.accelAir;
			if ( ig.input.state ('p1left') ) {
					this.accel.x = -accel;  //moving left is negative
					this.flip = true;
					}
			else if ( ig.input.state ('p1right') ) {
					this.accel.x = accel;
					this.flip = false;
					}
			else {
					 this.accel.x = 0;
					}
			// jump
			if ( this.standing && ig.input.pressed ('p1jump') ) {
			
					this.vel.y = -this.jump;

					}
			else if ( ig.input.pressed('p1jump')  && this.flapPower != 0 ) {
			
						this.vel.y = -this.jump / 2 ;
						this.flapPower--;
					}

			
			//reset flying
			if ( this.standing) {

				this.flapPower = 5;
			}

				//ANIMATION INIT//
					//sets animation based on velocity both X and Y
					if ( this.vel.y < 0 ) {
						this.currentAnim = this.anims.jump;
						}
					else if ( this.vel.y > 0 ) {
						this.currentAnim = this.anims.fall;
						}
					else if ( this.vel.x != 0 ) {
						this.currentAnim = this.anims.run;
						}
					else{
						this.currentAnim = this.anims.idle;
						}
				//END ANIM INIT// 

				
			//END MOVEMENT///////////////////////////////////////////////////////////
			this.parent();

			//flips animation if entity is flipped
			this.currentAnim.flip.x = this.flip;


		}, //UPDATE METHOD

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

	} );

	EntityShield = ig.Entity.extend ({

		//variables
		owner: null,

		//Size, Bounds, Orientation
		size: { x:12, y:16 },
		offset: { x:2, y:0 },
		zIndex:100,

		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/base.png',  16, 16 ),

		init: function( x, y, settings) {
			this.addAnim('idle',1, [170]);
			//defualt settings, overides anything before it
			this.parent ();


		}, //INITIALIZE 

		update: function (x, y, settings) {
			
			this.parent(x,y,settings);

			//this.pos.x = this.parent.x;
			//this.pos.y = this.parent.y;
			var master = ig.game.getEntitiesByType( EntityPlayer )[0];
			if( !master){
				this.kill();
			}
			else {

				//FLIP THE SHIELD IF THE PLAYER IS FLIPPED////////////////
				this.pos.y = master.pos.y +2; //POS X

				if (master.flip == true) {
					this.pos.x = master.pos.x +4; //POS Y FLIPPED
				}
				else {
					this.pos.x = master.pos.x -4; //POS Y NONFLIPPED
				}
				

				//USE SHILED/////////////////////////////////////////////

				if ( ig.input.state ('p1shield')  && master.attacking == false) {
					if (master.flip == true) {
						this.pos.x -= 8; //POS Y FLIPPED
						master.blocking = true;
					}
					else {
						this.pos.x += 8; //POS Y NONFLIPPED
						master.blocking = true;
					}
				}
				else {
					master.blocking = false;
				}
			}
		},

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

	});

	EntitySword = ig.Entity.extend ({

		//variables
		owner: null,

		//Size, Bounds, Orientation
		size: { x:12, y:16 },
		offset: { x:2, y:0 },
		zIndex:1,
		flip: null,

		//Attack Vars
		attackSpeed: 2,
		attackRange: 8,
		attackTimer: null,
		attacking: false,

		//Collision, C Type, Checks
		type: ig.Entity.TYPE.A,  //Type A Collider
		checkAgainst: ig.Entity.TYPE.A, //Don't check for collisions
		collides: ig.Entity.COLLIDES.PASSIVE, //Get's Pushed

		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/base.png',  16, 16 ),

		init: function( x, y, settings) {
			this.addAnim('idle',1, [256]);
			//defualt settings, overides anything before it
			this.parent ();

			this.attackTimer = new ig.Timer();
		}, //INITIALIZE 

		check: function( other ) {
			var master = ig.game.getEntitiesByType( EntityPlayer )[0];

			if (typeof(other.beACrate) == 'function' && master.holding == false && master.attacking == true){
				master.holding = true;
				master.holdingWhat = other;
				other.pickedUp = true;
				other.holder = this;
				//other.flip = this.flip;
			}
		}, //INITIALIZE 


		update: function (x, y, settings) {
			
			this.parent(x,y,settings);

			//this.pos.x = this.parent.x;
			//this.pos.y = this.parent.y;
			var master = ig.game.getEntitiesByType( EntityPlayer )[0];
			if( !master){
				this.kill();
			}
			else {
				//FLIP THE  SWORD IF THE PLAYER IS FLIPPED////////////////
				this.pos.y = master.pos.y; //POS X

				if (master.flip == true) {
					this.flip = true;
					this.pos.x = master.pos.x -6; //POS Y FLIPPED
				}
				else {
					this.flip = false;
					this.pos.x = master.pos.x + 6; //POS Y NONFLIPPED
				}

				//USE SHILED/////////////////////////////////////////////

				if ( ig.input.pressed ('p1sword') && master.attacking == false && master.blocking == false && master.holding == false) {

					//now we're attacking
					master.attacking = true;

					this.attackTimer.set(.2);

				}
				else if (ig.input.pressed ('p1sword') && master.attacking == false && master.blocking == false && master.holding == true){

					//now we're throwing
					if (master.holdingWhat !=null){
							//drop anything
							master.holding = false;

							//shoot the held item

							if (ig.input.state ('p1down')) {

								//lay it down gently
							}
							else {
								if (master.flip == true){
									master.holdingWhat.vel.x = -500;
								}
								else {
									master.holdingWhat.vel.x = 500;	
								}
							}

							//tell the held item what to think
							master.holdingWhat.pickedUp = false;
							master.holdingWhat.holder = null;

							//remove our reference to it
							master.holdingWhat = null;
						}

				}

				if( master.blocking == true) {
					if (master.flip == true) {
					this.pos.x = master.pos.x +2; //POS Y FLIPPED
				}
				else {
					this.pos.x = master.pos.x -2; //POS Y NONFLIPPED
				}
				}

				if( master.attacking == true) {

					this.collides = ig.Entity.COLLIDES.ACTIVE;


					if (master.flip == true) {
						this.pos.x -= this.attackRange - this.attackTimer.delta()/this.attackRange; //POS Y FLIPPED
					}
					else {
						this.pos.x += this.attackRange - this.attackTimer.delta()/this.attackRange; //POS Y NONFLIPPED
					}

					if (this.attackTimer.delta() >= 0){


						master.attacking = false;
						//alert("Attack");
					}
				}

				else {
					this.collides = ig.Entity.COLLIDES.NEVER;
				}
				this.currentAnim.flip.x = master.flip;
			}
		},

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

	});

} );

And my re spawning the player in main
	if(this.player1._killed || !this.player1 && this.spawnTimer.delta() >= 1 ){
						  this.player1 = ig.game.spawnEntity(EntityPlayer,this.p1StartPosX,this.p1StartPosY);

						  this.spawnTimer.reset();
						
				}

1 decade ago by stahlmanDesign

The way I do it is to have an inventory object in main.js that handles all this logic, because when you kill() your player, it and its variables are gone, and even the reference to ig.game.getEntitiesByType("EntityPlayer")[0] is null

in main.js:

{
  "inventory": {
    "player": [
      "shield",
      "sword",
      "potion"
    ],
    "enemy": [
      "map",
      "axe"
    ]
  }
}

When your player is killed and respawned, the inventory is still intact. You can of course empty it when the player is spawned in the player.init() method:

init: function(){
ig.game.inventory.player = []
…
}
Page 1 of 1
« first « previous next › last »