87 lines
2.4 KiB
JavaScript
87 lines
2.4 KiB
JavaScript
var asdf = require("asdf-games");
|
|
const { Texture, TileSprite, AnimManager, wallslide } = 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.level = level;
|
|
|
|
this.keys = keys;
|
|
|
|
this.hitBox = {
|
|
x: 4,
|
|
y: 0,
|
|
w: 30,
|
|
h: 48
|
|
};
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
// Speed
|
|
const xo = this.keys.x * dt * (600 * (1 - (this.keys.action) ?
|
|
this.rate.running : this.rate.walking ));
|
|
const yo = this.keys.y * dt * (600 * (1 - (this.keys.action) ?
|
|
this.rate.running : this.rate.walking ));
|
|
|
|
// Move
|
|
const r = wallslide.wallslide(this, this.level, xo, yo);
|
|
this.pos.x += r.x;
|
|
this.pos.y += r.y;
|
|
}
|
|
}
|
|
|
|
module.exports = Player; |