caa-game/src/entities/portal.js

85 lines
1.8 KiB
JavaScript

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileSprite, entity } = asdf;
var texture = new Texture("./res/tilemap.png");
class Portal extends TileSprite {
constructor(pos, player, keys, type, action, key) {
super(texture, 32, 32);
this.pos = pos;
this.scale = { x: 1, y: 1 };
this.player = player;
this.action = action;
this.keys = keys;
this.key = key;
switch (type) {
case "Door_n":
this.frame = { x: 1, y: 7 };
this.hitBox = {
x: 1,
y: 1,
w: 30,
h: 45
};
break;
case "Door_s":
this.frame = { x: 2, y: 9 };
this.hitBox = {
x: 0,
y: -13,
w: 32,
h: 45
};
break;
case "Ladder":
this.frame = { x: 2, y: 7 };
this.hitBox = {
x: 4,
y: 0,
w: 24,
h: 32
};
break;
case "Stairs":
this.frame = { x: 4, y: 7 };
this.hitBox = {
x: 1,
y: 1,
w: 30,
h: 30
};
break;
}
}
update(dt) {
super.update(dt);
if (entity.hit(this, this.player) && this.keys.action) {
if (this.key != "") {
if (this.player.items.keys.length > 0) {
for (let index = 0; index < this.player.items.keys.length; index++) {
const element = this.player.items.keys[index];
if (element == this.key) {
this.action();
this.player.items.key = false;
delete this.player.items.keys[index];
} else {
// Not the correct key
console.log("correct keyn't");
}
}
} else {
// No keys at all
console.log("keyn't");
}
} else {
this.action();
}
}
}
}
module.exports = Portal;