89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
var asdf = require("asdf-games");
|
|
const { Texture, TileSprite, AnimManager } = asdf;
|
|
|
|
var texture = new Texture("./res/player.png");
|
|
|
|
class Player extends TileSprite {
|
|
constructor(keys, window, level) {
|
|
super(texture, 24, 24);
|
|
this.scale = { x: 2, y: 2 };
|
|
|
|
this.window = window;
|
|
|
|
this.rate = {
|
|
walking: 0.2,
|
|
running: 0.08
|
|
};
|
|
|
|
this.anims = new AnimManager(this);
|
|
// North
|
|
this.anims.add("walk_n", [0, 1, 2, 3].map(x => ({ x, y: 0 })), this.rate.walking);
|
|
this.anims.add("run_n", [0, 1, 2, 3].map(x => ({ x, y: 0 })), this.rate.running);
|
|
// East
|
|
this.anims.add("walk_e", [0, 1, 2, 3].map(x => ({ x, y: 2 })), this.rate.walking);
|
|
this.anims.add("run_e", [0, 1, 2, 3].map(x => ({ x, y: 2 })), this.rate.running);
|
|
// South
|
|
this.anims.add("walk_s", [0, 1, 2, 3].map(x => ({ x, y: 1 })), this.rate.walking);
|
|
this.anims.add("run_s", [0, 1, 2, 3].map(x => ({ x, y: 1 })), this.rate.running);
|
|
// West
|
|
this.anims.add("walk_w", [0, 1, 2, 3].map(x => ({ x, y: 3 })), this.rate.walking);
|
|
this.anims.add("run_w", [0, 1, 2, 3].map(x => ({ x, y: 3 })), this.rate.running);
|
|
|
|
this.anims.play("walk_s");
|
|
|
|
this.pos.x = (level.w / 2) - (24 * this.scale.x / 2);
|
|
this.pos.y = (level.h / 2) - (24 * this.scale.y / 2);
|
|
|
|
this.keys = keys;
|
|
}
|
|
|
|
update(dt) {
|
|
super.update(dt);
|
|
|
|
// Animate
|
|
if (this.keys.x == -1) {
|
|
// Left
|
|
(this.keys.action) ?
|
|
this.anims.play("run_w") : this.anims.play("walk_w");
|
|
} else if (this.keys.x == 1) {
|
|
// Right
|
|
(this.keys.action) ?
|
|
this.anims.play("run_e") : this.anims.play("walk_e");
|
|
} else if (this.keys.y == -1) {
|
|
// Up
|
|
(this.keys.action) ?
|
|
this.anims.play("run_n") : this.anims.play("walk_n");
|
|
} else if (this.keys.y == 1) {
|
|
// Down
|
|
(this.keys.action) ?
|
|
this.anims.play("run_s") : this.anims.play("walk_s");
|
|
} else {
|
|
// Idle
|
|
this.anims.stop();
|
|
}
|
|
|
|
// Move
|
|
if (this.keys.x == -1) {
|
|
// Left
|
|
this.pos.x += this.keys.x * dt * (600 * (1 - (this.keys.action) ?
|
|
this.rate.running : this.rate.walking ));
|
|
}
|
|
if (this.keys.x == 1) {
|
|
// Right
|
|
this.pos.x += this.keys.x * dt * (600 * (1 - (this.keys.action) ?
|
|
this.rate.running : this.rate.walking ));
|
|
}
|
|
if (this.keys.y == -1) {
|
|
// Up
|
|
this.pos.y += this.keys.y * dt * (600 * (1 - (this.keys.action) ?
|
|
this.rate.running : this.rate.walking ));
|
|
}
|
|
if (this.keys.y == 1) {
|
|
// Down
|
|
this.pos.y += this.keys.y * dt * (600 * (1 - (this.keys.action) ?
|
|
this.rate.running : this.rate.walking ));
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Player; |