2020-03-02 17:13:27 +01:00
|
|
|
var asdf = require("asdf-games");
|
2020-03-23 20:55:39 +01:00
|
|
|
const { Texture, TileSprite, AnimManager, wallslide } = asdf;
|
2020-03-02 17:13:27 +01:00
|
|
|
|
2020-04-13 11:48:48 +02:00
|
|
|
const texture = new Texture("../res/images/player.png");
|
2020-03-02 17:13:27 +01:00
|
|
|
|
|
|
|
class Player extends TileSprite {
|
2020-03-29 15:40:57 +02:00
|
|
|
constructor(keys, window) {
|
2020-03-15 17:31:32 +01:00
|
|
|
super(texture, 24, 24);
|
2020-03-25 17:06:26 +01:00
|
|
|
this.scale = { x: 2, y: 2 };
|
2020-03-15 17:31:32 +01:00
|
|
|
this.window = window;
|
2020-03-29 15:40:57 +02:00
|
|
|
this.keys = keys;
|
2020-03-02 17:13:27 +01:00
|
|
|
|
2020-03-22 15:54:55 +01:00
|
|
|
this.rate = {
|
|
|
|
walking: 0.2,
|
2020-03-26 20:09:30 +01:00
|
|
|
running: 0.1
|
2020-03-22 15:54:55 +01:00
|
|
|
};
|
2020-03-02 17:13:27 +01:00
|
|
|
|
2020-03-29 15:40:57 +02:00
|
|
|
this.speed = {
|
|
|
|
walking: 75,
|
|
|
|
running: 150
|
|
|
|
};
|
|
|
|
|
2020-03-22 15:54:55 +01:00
|
|
|
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");
|
2020-03-02 17:13:27 +01:00
|
|
|
|
2020-03-29 15:40:57 +02:00
|
|
|
this.lives = 5;
|
2020-04-07 12:14:22 +02:00
|
|
|
|
2020-04-08 12:36:03 +02:00
|
|
|
this.items = {
|
2020-04-11 20:58:55 +02:00
|
|
|
keys: [ ],
|
2020-04-08 12:36:03 +02:00
|
|
|
repellant: false
|
|
|
|
};
|
2020-04-07 12:14:22 +02:00
|
|
|
|
2020-03-29 15:40:57 +02:00
|
|
|
this.stamina = {
|
|
|
|
current: 5,
|
|
|
|
max: 5
|
|
|
|
};
|
2020-03-23 20:55:39 +01:00
|
|
|
|
2020-04-08 10:14:24 +02:00
|
|
|
this.refocus = false;
|
|
|
|
|
2020-03-23 20:55:39 +01:00
|
|
|
this.hitBox = {
|
|
|
|
x: 4,
|
|
|
|
y: 0,
|
2020-03-25 17:06:26 +01:00
|
|
|
w: 30,
|
|
|
|
h: 48
|
2020-03-23 20:55:39 +01:00
|
|
|
};
|
2020-03-15 17:31:32 +01:00
|
|
|
}
|
2020-03-02 22:06:16 +01:00
|
|
|
|
2020-03-15 17:31:32 +01:00
|
|
|
update(dt) {
|
2020-03-22 15:54:55 +01:00
|
|
|
super.update(dt);
|
2020-03-02 17:13:27 +01:00
|
|
|
|
2020-03-22 15:54:55 +01:00
|
|
|
// Animate
|
|
|
|
if (this.keys.x == -1) {
|
|
|
|
// Left
|
2020-03-31 10:33:44 +02:00
|
|
|
(this.keys.ctrl && !(this.stamina.current <= 0)) ?
|
2020-03-22 15:54:55 +01:00
|
|
|
this.anims.play("run_w") : this.anims.play("walk_w");
|
|
|
|
} else if (this.keys.x == 1) {
|
|
|
|
// Right
|
2020-03-31 10:33:44 +02:00
|
|
|
(this.keys.ctrl && !(this.stamina.current <= 0)) ?
|
2020-03-22 15:54:55 +01:00
|
|
|
this.anims.play("run_e") : this.anims.play("walk_e");
|
|
|
|
} else if (this.keys.y == -1) {
|
|
|
|
// Up
|
2020-03-31 10:33:44 +02:00
|
|
|
(this.keys.ctrl && !(this.stamina.current <= 0)) ?
|
2020-03-22 15:54:55 +01:00
|
|
|
this.anims.play("run_n") : this.anims.play("walk_n");
|
|
|
|
} else if (this.keys.y == 1) {
|
|
|
|
// Down
|
2020-03-31 10:33:44 +02:00
|
|
|
(this.keys.ctrl && !(this.stamina.current <= 0)) ?
|
2020-03-22 15:54:55 +01:00
|
|
|
this.anims.play("run_s") : this.anims.play("walk_s");
|
2020-03-15 17:31:32 +01:00
|
|
|
} else {
|
2020-03-22 15:54:55 +01:00
|
|
|
// Idle
|
|
|
|
this.anims.stop();
|
2020-03-15 17:31:32 +01:00
|
|
|
}
|
2020-03-02 17:13:27 +01:00
|
|
|
|
2020-03-29 15:40:57 +02:00
|
|
|
// Lose stamina
|
2020-03-31 10:33:44 +02:00
|
|
|
if (this.keys.ctrl) {
|
2020-03-29 15:40:57 +02:00
|
|
|
if (!(this.stamina.current <= 0)) {
|
|
|
|
this.stamina.current -= dt;
|
|
|
|
} else {
|
|
|
|
this.stamina.current = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(this.stamina.current >= this.stamina.max)) {
|
|
|
|
this.stamina.current += dt;
|
|
|
|
} else {
|
|
|
|
this.stamina.current = this.stamina.max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 20:55:39 +01:00
|
|
|
// Speed
|
2020-03-31 10:33:44 +02:00
|
|
|
const xo = this.keys.x * ((this.keys.ctrl && !(this.stamina.current <= 0)) ?
|
2020-03-29 15:40:57 +02:00
|
|
|
this.speed.running : this.speed.walking) * dt;
|
2020-03-31 10:33:44 +02:00
|
|
|
const yo = this.keys.y * ((this.keys.ctrl && !(this.stamina.current <= 0)) ?
|
2020-03-29 15:40:57 +02:00
|
|
|
this.speed.running : this.speed.walking) * dt;
|
2020-03-23 20:55:39 +01:00
|
|
|
|
2020-03-22 15:54:55 +01:00
|
|
|
// Move
|
2020-03-23 20:55:39 +01:00
|
|
|
const r = wallslide.wallslide(this, this.level, xo, yo);
|
|
|
|
this.pos.x += r.x;
|
|
|
|
this.pos.y += r.y;
|
2020-03-15 17:31:32 +01:00
|
|
|
}
|
2020-03-02 17:13:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Player;
|