Files
HexTowerDefense3/src/app/components/game/sim/SimEnemy.ts
2025-05-17 15:55:26 +02:00

72 lines
1.8 KiB
TypeScript

import { EEnemySize } from '../data/EEnemySize';
import { EProjectileEffectType } from '../data/EProjectileEffectType';
import { GdProjectileEffect } from '../data/GdProjectileEffect';
import { Hex } from '../util/Hex';
import { Vector2 } from '../util/Vector2';
import { SimLevel } from './SimLevel';
export class SimEnemy {
index: number = -1;
routeIdx: number = -1;
startHex: Hex = new Hex(0, 0);
endHex: Hex = new Hex(0, 0);
path: number[] = [];
prevPosition: Vector2 = new Vector2(0, 0);
position: Vector2 = new Vector2(0, 0);
direction: Vector2 = new Vector2(0, 0);
currentPathIndex: number = -1;
currentPathStep: number = -1;
currentPathPosition: Vector2 = new Vector2(0, 0);
dead: boolean = false;
hitPonts: number = 0;
size: EEnemySize = EEnemySize.Tiny;
speed: number = 0;
gain: number = 0;
constructor(index: number, routeIdx: number) {
this.index = index;
this.routeIdx = routeIdx;
}
public suffer(level: SimLevel, effect: GdProjectileEffect) {
switch (effect.type) {
case EProjectileEffectType.Damage: {
this.hitPonts -= effect.amount;
if (this.hitPonts <= 0) {
level.currency += this.gain;
this.dead = true;
}
break;
}
}
}
public onPathUpdated(level: SimLevel) {
const myPos = this.position;
this.currentPathPosition = myPos;
if (this.path == null) {
return;
}
const current = this.path[this.currentPathIndex];
const next = this.path[this.currentPathIndex + 1];
if (!next) {
return;
}
const cells = level.cells;
const pos1 = cells[current].hex.toWorld();
const pos2 = cells[next].hex.toWorld();
const myDir = pos2.subtract(myPos);
const myLen = myDir.magnitude();
const hexDir = pos2.subtract(pos1);
const hexLen = hexDir.magnitude();
if (myLen > hexLen) {
this.path.unshift(this.path[0]);
}
this.direction = myDir.multiplyScalar(1 / myLen);
}
}