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?
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);
// ...
