2020-03-31 10:33:44 +02:00
|
|
|
var asdf = require("asdf-games");
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-04-13 11:48:48 +02:00
|
|
|
const { Texture, Sound, TileSprite, entity } = asdf;
|
2020-03-31 10:33:44 +02:00
|
|
|
|
2020-04-13 11:48:48 +02:00
|
|
|
const texture = new Texture(__dirname + "/../../res/images/tilemap.png");
|
|
|
|
const sounds = {
|
2020-04-13 13:18:24 +02:00
|
|
|
forbidden: new Sound(__dirname + "/../../res/sounds/forbidden.wav"),
|
2020-04-14 17:50:53 +02:00
|
|
|
obtain: new Sound(__dirname + "/../../res/sounds/obtain.wav", { volume: 0.1 })
|
2020-04-13 11:48:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const 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-04-13 11:48:48 +02:00
|
|
|
this.pressed = 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");
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:48:48 +02:00
|
|
|
if (this.keys.action) {
|
|
|
|
if (this.state == state.closed) {
|
|
|
|
this.state = state.open;
|
|
|
|
this.pressed = true;
|
2020-04-14 17:50:53 +02:00
|
|
|
this.action();
|
2020-04-13 13:18:24 +02:00
|
|
|
sounds.obtain.play();
|
2020-04-13 11:48:48 +02:00
|
|
|
} else {
|
|
|
|
if (!sounds.forbidden.playing && !this.pressed) {
|
|
|
|
sounds.forbidden.play();
|
|
|
|
this.pressed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.pressed = false;
|
2020-04-11 20:58:55 +02:00
|
|
|
}
|
2020-04-13 11:48:48 +02:00
|
|
|
|
2020-04-11 20:58:55 +02:00
|
|
|
} 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;
|