caa-game/src/entities/chest.js

95 lines
2.1 KiB
JavaScript
Raw Normal View History

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"),
obtain: new Sound(__dirname + "/../../res/sounds/obtain.wav", { volume: 0.1 })
};
const Pointer = require("./pointer.js");
const state = {
open: 0,
closed: 1
};
const frames = {
closed: { x: 2, y: 6 },
open: { x: 3, y: 6 }
};
class Chest extends TileSprite {
constructor(pos, player, keys, level, action) {
super(texture, 32, 32);
this.pos = pos;
this.scale = { x: 1, y: 1 };
this.frame = frames.closed;
this.state = state.closed;
this.player = player;
this.keys = keys;
this.action = action;
this.level = level;
this.pointer = false;
this.pressed = false;
this.hitBox = {
x: 1,
y: 1,
w: 28,
h: 28
};
}
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.state == state.closed) {
this.pointer.anims.play("white");
} else {
this.pointer.anims.play("red");
}
if (this.keys.action) {
if (this.state == state.closed) {
this.state = state.open;
this.pressed = true;
this.action();
sounds.obtain.play();
} else {
if (!sounds.forbidden.playing && !this.pressed) {
sounds.forbidden.play();
this.pressed = true;
}
}
} else {
this.pressed = false;
}
} else {
this.level.entities.remove(this.pointer);
this.pointer = false;
}
if (this.state == state.closed) {
this.frame = frames.closed;
} else if (this.state == state.open) {
this.frame = frames.open;
}
}
}
module.exports = Chest;