58 lines
2.0 KiB
JavaScript
58 lines
2.0 KiB
JavaScript
import { EEnemySize } from "../GameData/index.js";
|
|
import { Vector2 } from "../Util/index.js";
|
|
export class VisEnemy {
|
|
_positions;
|
|
_directions;
|
|
_image;
|
|
_context;
|
|
_simEnemy;
|
|
_angle = null;
|
|
_gdRoot;
|
|
constructor(gdRoot, simEnemy, width, height) {
|
|
this._gdRoot = gdRoot;
|
|
this._simEnemy = simEnemy;
|
|
this._image = document.createElement("canvas");
|
|
this._image.width = width;
|
|
this._image.height = height;
|
|
this._context = this._image.getContext("2d");
|
|
this._positions = [simEnemy.position, simEnemy.position];
|
|
this._directions = [simEnemy.direction, simEnemy.direction];
|
|
}
|
|
get positions() {
|
|
return this._positions;
|
|
}
|
|
get image() {
|
|
return this._image;
|
|
}
|
|
advanceStep() {
|
|
const prevPos = this._positions[1];
|
|
const prevDir = this._directions[1];
|
|
this._positions = [prevPos, this._simEnemy.position];
|
|
this._directions = [prevDir, this._simEnemy.direction];
|
|
}
|
|
update(t) {
|
|
const directions = this._directions;
|
|
const dir = Vector2.lerp(directions[0], directions[1], t);
|
|
const angle = Math.atan2(dir.x, -dir.y) - Math.PI / 2;
|
|
if (this._angle == angle) {
|
|
return;
|
|
}
|
|
this._angle = angle;
|
|
const ctx = this._context;
|
|
ctx.clearRect(0, 0, this._image.width, this._image.height);
|
|
ctx.save();
|
|
ctx.translate(this._image.width / 2, this._image.height / 2);
|
|
ctx.rotate(angle);
|
|
switch (this._simEnemy.size) {
|
|
case EEnemySize.Tiny:
|
|
ctx.scale(0.75, 0.75);
|
|
break;
|
|
case EEnemySize.Huge:
|
|
ctx.scale(2, 2);
|
|
break;
|
|
}
|
|
ctx.drawImage(this._gdRoot.image("enemy-" + (this._simEnemy.index | 0) + ".svg"), -this._image.width / 2, -this._image.height / 2, this._image.width, this._image.height);
|
|
ctx.restore();
|
|
}
|
|
}
|
|
//# sourceMappingURL=VisEnemy.js.map
|