caa-game/src/entities/portal.js

133 lines
3.2 KiB
JavaScript

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, Sound, TileSprite, entity } = asdf;
const texture = new Texture(__dirname + "/../../res/images/tilemap.png");
const sounds = {
forbidden: new Sound(__dirname + "/../../res/sounds/forbidden.wav")
};
var Pointer = require("./pointer.js");
class Portal extends TileSprite {
constructor(pos, player, keys, level, type, action, key) {
super(texture, 32, 32);
this.pos = pos;
this.scale = { x: 1, y: 1 };
this.player = player;
this.level = level;
this.action = action;
this.pointer = false;
this.pressed = false;
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)) {
if (!this.pointer) {
var pointer = new Pointer({
x: (this.pos.x + this.tileW / 2) - 8,
y: this.pos.y - 16
});
this.level.entities.add(pointer);
this.pointer = pointer;
}
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];
console.log(this.key);
console.log(element);
if (element == this.key) {
// Correct key
this.pointer.anims.play("white");
if (this.keys.action) {
this.action();
console.warn("action");
}
break;
} else {
// Not the correct key
console.log("correct keyn't");
this.pointer.anims.play("red");
if (this.keys.action) {
if (!sounds.forbidden.playing && !this.pressed) {
sounds.forbidden.play();
this.pressed = true;
}
} else {
this.pressed = false;
}
}
}
} else {
// No keys at all
console.log("You have no keys");
this.pointer.anims.play("red");
if (this.keys.action) {
if (!sounds.forbidden.playing && !this.pressed) {
sounds.forbidden.play();
this.pressed = true;
}
} else {
this.pressed = false;
}
}
} else {
if (this.keys.action) {
this.action();
}
}
} else {
this.level.entities.remove(this.pointer);
this.pointer = false;
}
}
}
module.exports = Portal;