2020-03-31 10:33:44 +02:00
|
|
|
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");
|
2020-04-11 20:58:55 +02:00
|
|
|
var Pointer = require("./pointer.js");
|
2020-03-31 10:33:44 +02:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
open: 0,
|
|
|
|
closed: 1
|
|
|
|
};
|
|
|
|
|
2020-04-11 20:58:55 +02:00
|
|
|
const frames = {
|
|
|
|
closed: { x: 2, y: 6 },
|
|
|
|
open: { x: 3, y: 6 }
|
|
|
|
};
|
|
|
|
|
2020-03-31 10:33:44 +02:00
|
|
|
class Chest extends TileSprite {
|
2020-04-11 20:58:55 +02:00
|
|
|
constructor(pos, player, keys, level, action) {
|
2020-03-31 10:33:44 +02:00
|
|
|
super(texture, 32, 32);
|
|
|
|
this.pos = pos;
|
|
|
|
this.scale = { x: 1, y: 1 };
|
|
|
|
|
2020-04-11 20:58:55 +02:00
|
|
|
this.frame = frames.closed;
|
2020-03-31 10:33:44 +02:00
|
|
|
this.state = state.closed;
|
|
|
|
this.player = player;
|
|
|
|
this.keys = keys;
|
|
|
|
this.action = action;
|
2020-04-11 20:58:55 +02:00
|
|
|
this.level = level;
|
|
|
|
|
|
|
|
this.pointer = false;
|
2020-03-31 10:33:44 +02:00
|
|
|
|
|
|
|
this.hitBox = {
|
|
|
|
x: 1,
|
|
|
|
y: 1,
|
|
|
|
w: 28,
|
|
|
|
h: 28
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
update(dt) {
|
|
|
|
super.update(dt);
|
2020-04-11 20:58:55 +02:00
|
|
|
|
|
|
|
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;
|
2020-03-31 10:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state == state.closed) {
|
2020-04-11 20:58:55 +02:00
|
|
|
this.frame = frames.closed;
|
2020-03-31 10:33:44 +02:00
|
|
|
} else if (this.state == state.open) {
|
2020-04-11 20:58:55 +02:00
|
|
|
this.frame = frames.open;
|
2020-03-31 10:33:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Chest;
|