caa-game/src/entities/mage.js

93 lines
2.1 KiB
JavaScript

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileSprite, AnimManager, wallslide, entity } = asdf;
var texture = new Texture("./res/mage.png");
const states = {
idle: 0,
attack: 1,
evade: 2
};
class Mage extends TileSprite {
constructor(pos, player, level) {
super(texture, 16, 16);
this.pos = pos;
this.scale = { x: 3, y: 3 };
this.anims = new AnimManager(this);
// North
this.anims.add("move_n", [4, 5, 6, 7].map(x => ({ x, y: 0 })), 0.1);
// East
this.anims.add("move_e", [0, 1, 2, 3].map(x => ({ x, y: 1 })), 0.1);
// South
this.anims.add("move_s", [0, 1, 2, 3].map(x => ({ x, y: 0 })), 0.1);
// West
this.anims.add("move_w", [4, 5, 6, 7].map(x => ({ x, y: 1 })), 0.1);
// Inactive
this.anims.add("idle", [{ x: 0, y: 2 }], 0.1);
this.anims.play("idle");
this.state = states.idle;
this.level = level;
this.player = player;
this.hitBox = {
x: 4,
y: 1,
w: 8,
h: 14
};
}
update(dt) {
super.update(dt);
if (entity.distance(this.player, this) < 100) {
this.state = states.attack;
} else {
this.state = states.idle;
}
if (this.state != states.idle) {
// Move
const angle = entity.angle(this.player, this);
const xo = Math.cos(angle) * 100 * dt;
const yo = Math.sin(angle) * 100 * dt;
const r = wallslide.wallslide(this, this.level, xo, yo);
if (this.player.repellant) {
this.pos.x -= r.x;
this.pos.y -= r.y;
} else {
this.pos.x += r.x;
this.pos.y += r.y;
}
if (Math.abs(r.x) > Math.abs(r.y)) {
// Animation x axis
if (r.x >= 0) {
this.anims.play("move_e");
} else {
this.anims.play("move_w");
}
} else {
// Animation y axis
if (r.y >= 0) {
this.anims.play("move_s");
} else {
this.anims.play("move_n");
}
}
} else {
this.anims.play("idle");
}
}
}
module.exports = Mage;