mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-04-21 22:38:52 +00:00
37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import { Vec3 } from "cc";
|
|
import { Enemy } from "./Enemy";
|
|
import { EnemyMover } from "./EnemyMover";
|
|
|
|
export class LaunchToTargetEnemyMover extends EnemyMover {
|
|
private enemyToDirection: Map<Enemy, Vec3> = new Map<Enemy, Vec3>();
|
|
private lastTargetPosition: Vec3 = new Vec3();
|
|
private lastDirection: Vec3 = new Vec3();
|
|
|
|
public addEnemy(enemy: Enemy): void {
|
|
let direction: Vec3 = new Vec3();
|
|
|
|
// if the enemy is added soon enough, move as a single group towards one direction
|
|
if (Vec3.distance(this.lastTargetPosition, this.targetNode.worldPosition) < 10) {
|
|
direction = this.lastDirection;
|
|
} else {
|
|
direction = Vec3.subtract(direction, this.targetNode.worldPosition, enemy.node.worldPosition);
|
|
this.lastDirection = direction;
|
|
this.lastTargetPosition = this.targetNode.worldPosition.clone();
|
|
}
|
|
|
|
this.enemyToDirection.set(enemy, direction.normalize());
|
|
super.addEnemy(enemy);
|
|
}
|
|
|
|
public removeEnemy(enemy: Enemy): void {
|
|
this.enemyToDirection.delete(enemy);
|
|
super.removeEnemy(enemy);
|
|
}
|
|
|
|
public gameTick(deltaTime: number): void {
|
|
for (const enemyAndDirection of this.enemyToDirection) {
|
|
enemyAndDirection[0].moveBy(enemyAndDirection[1], deltaTime);
|
|
}
|
|
}
|
|
}
|