caa-game/src/entities/portal.js

126 lines
2.8 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")
};
const TextBox = require(__dirname + "/../helpers/textbox.js");
const text = require(__dirname + "/../../res/lang/default.js");
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;
this.type = type;
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;
case "Door_hidden":
this.frame = { x: 0, y: 4 };
this.hitBox = {
x: 1,
y: 1,
w: 30,
h: 45
};
break;
}
}
update(dt) {
super.update(dt);
if (entity.hit(this, this.player)) {
if (!this.pointer && this.type !== "Door_hidden") {
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 != "" && this.type !== "Door_hidden") {
if (this.player.items.keys.includes(this.key)) {
// Correct key
this.pointer.anims.play("white");
if (this.keys.action) {
this.action();
}
} else {
// No key or not the correct key
this.pointer.anims.play("red");
if (this.keys.action) {
if (!sounds.forbidden.playing && !this.pressed) {
this.level.textbox.add(new TextBox(text.game.keyMissing));
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;