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 mdkess

I'm implementing doors in my game, and the logic is basically "if the player presses 'e' and they are facing and reasonably close to a door, open that door."

Right now I am using getEntitiesByType(EntityDoor), and doing the check by hand. I would like to be able to do something like CollisionMap.trace, and extend say a 1xN pixel "hand" in front of the character, but does such a thing exist?

After this, I'm going to implement an EntityChest, which is similar - player presses 'e', and if they are overlapping a chest, the chest opens. So I think it'll be another "getEntitiesByType(EntityChest)" and checking each one for overlap.

So anyway, I have it working, I'm just not sure if it's optimal. Any tips?

// in player.js
var doors = ig.game.getEntitiesByType(EntityDoor);
if (doors) {
	for (var i = 0; i < doors.length; ++i)  {
		var dist = this.distanceTo(doors[i]);
		if (dist <= this.size.x + 0.0001)  {
			// logic for opening the door, depending on direction player is facing etc.
		}
	}
}
var chests = ig.game.getEntitiesByType(EntityChest);
// ...

1 decade ago by Cavalier

You may want to allow for a bigger distance between the player and the door.
As fas as I know, that is the only way to check if an entity is close to another, getting all entities of said type and doing a this.distanceTo()

Now, if all doors always open from the same side (for instance, always with the player going up), all you'll need is a simple animation check
if (player.currentAnim == player.anim['up']) {
 //open door
}

Now, if each door has a different, or multiple directions from which it can be opened, you'll have to define it among its variables:
door = ig.Entity.extend({
opensFromSouth = true,
opensFromEast = false,
opensFromWest = false,
opensFromNorth = false,
// could also be an array
opensFrom: ['left','right'],

Personally, I'd do it like this:
distanceToOpen: 5,
...
if (ig.input.pressed('E')) {
var doors = ig.game.getEntitiesByType(EntityDoor);
  for (var i = 0; i < doors.length; i++)  {
      if (this.distanceTo(doors[i] < this.distanceToOpen) )  {
          if (doors[i].opensFromSouth) { //repeat for other directions
            if (this.currentAnim = this.anims['up'])  {
              //open up!!
            }
          }
      }
  }
}

EDIT: You can also try using this.touches(door) if you want to make sure the player is actually touching the door instead of checking the distance

1 decade ago by Xatruch

you could also just add a boolean to open the door when the player collides with the door.

// door.js
EntityDoor = ig.Entity.extend({

 checkAgainst: ig.Entity.TYPE.A,

 check: function( other ) {
  if( other instanceof EntityPlayer ) {
   other.canOpenDoor = true;
  }
 }
});

// on player.js
EntityPlayer = ig.Entity.extend({

  type: ig.Entity.TYPE.A,
  update: function() {
   if( this.canOpenDoor && ig.input.pressed( 'E' ) {
    // opening door logic.
   }
   this.canOpenDoor = false;
  }
});

1 decade ago by mdkess

Apologies for the delay in replying, life took over! Thank you all for your responses.

I think my way is right. The distance feels natural, and I think that the collision check is good - but I want the player to be able to open it while standing still next to the door. Context is, this is a platformer. I'll post a demo in a few weeks.
Page 1 of 1
« first « previous next › last »