caa-game/src/entities/chest.js

77 lines
1.6 KiB
JavaScript
Raw Normal View History

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileSprite, AnimManager, entity } = asdf;
var texture = new Texture("./res/tilemap.png");
var 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.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 && this.state == state.closed) {
this.state = state.open;
this.action();
}
} 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;