caa-game/src/entities/mage.js

139 lines
3.3 KiB
JavaScript

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileSprite, AnimManager, wallslide, entity } = asdf;
const texture = new Texture("../res/images/mage.png");
const Bullet = require("./bullet.js");
const states = {
idle: 0,
attack: 1,
move: 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.15);
this.anims.add("attack_n", [{ x: 4, y: 0 }], 0.1);
// East
this.anims.add("move_e", [0, 1, 2, 3].map(x => ({ x, y: 1 })), 0.15);
this.anims.add("attack_e", [{ x: 0, y: 1 }], 0.1);
// South
this.anims.add("move_s", [0, 1, 2, 3].map(x => ({ x, y: 0 })), 0.15);
this.anims.add("attack_s", [{ x: 0, y: 0 }], 0.1);
// West
this.anims.add("move_w", [4, 5, 6, 7].map(x => ({ x, y: 1 })), 0.15);
this.anims.add("attack_w", [{ x: 4, y: 1 }], 0.1);
// Inactive
this.anims.add("idle", [{ x: 0, y: 2 }], 0.1);
this.anims.play("idle");
this.state = states.idle;
this.bullet = false;
this.shootrate = {
current: 0.5,
max: 2
};
this.level = level;
this.player = player;
this.hitBox = {
x: 4,
y: 1,
w: 8,
h: 14
};
}
update(dt) {
super.update(dt);
const angle = entity.angle(this.player, this);
if (entity.distance(this.player, this) <= 200) {
if (entity.distance(this.player, this) <= 100) {
this.state = states.attack;
} else {
this.state = states.move;
}
} else {
this.state = states.idle;
}
const xo = Math.cos(angle) * 75 * dt;
const yo = Math.sin(angle) * 75 * dt;
const r = wallslide.wallslide(this, this.level, xo, yo);
if (this.state == states.move && !this.bullet) {
if (this.player.items.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 if (this.state == states.attack) {
if (this.shootrate.current <= 0 && !this.bullet) {
this.shoot(angle);
this.shootrate.current = this.shootrate.max;
} else {
this.shootrate.current -= dt;
}
if (Math.abs(r.x) > Math.abs(r.y)) {
// Animation x axis
if (r.x >= 0) {
this.anims.play("attack_e");
} else {
this.anims.play("attack_w");
}
} else {
// Animation y axis
if (r.y >= 0) {
this.anims.play("attack_s");
} else {
this.anims.play("attack_n");
}
}
} else {
this.anims.play("idle");
}
}
shoot(angle) {
this.bullet = true;
var pos = {
x: this.pos.x + 8,
y: this.pos.y + 8
};
var bullet = new Bullet(pos, angle, this, this.player, this.level);
this.level.entities.add(bullet);
}
}
module.exports = Mage;