enemy spawn and next wave start refactoring

This commit is contained in:
2025-06-15 11:14:12 +02:00
parent d26999ace9
commit 337a3d8213
4 changed files with 47 additions and 31 deletions

View File

@@ -1,9 +1,13 @@
import { EEnemySize } from '../data/EEnemySize'; import { EEnemySize } from '../data/EEnemySize';
import { EProjectileEffectType } from '../data/EProjectileEffectType'; import { EProjectileEffectType } from '../data/EProjectileEffectType';
import { GdEnemy } from '../data/GdEnemy';
import { GdLevel } from '../data/GdLevel';
import { GdProjectileEffect } from '../data/GdProjectileEffect'; import { GdProjectileEffect } from '../data/GdProjectileEffect';
import { Hex } from '../util/Hex'; import { Hex } from '../util/Hex';
import { PathFinding } from '../util/PathFinding';
import { Vector2 } from '../util/Vector2'; import { Vector2 } from '../util/Vector2';
import { SimLevel } from './SimLevel'; import { SimLevel } from './SimLevel';
import { SimMain } from './SimMain';
export class SimEnemy { export class SimEnemy {
index: number = -1; index: number = -1;
@@ -23,9 +27,25 @@ export class SimEnemy {
speed: number = 0; speed: number = 0;
gain: number = 0; gain: number = 0;
constructor(index: number, routeIdx: number) { constructor(index: number, routeIdx: number, step: number, simMain: SimMain) {
const level = simMain.currentLevel!;
const gdLevel = simMain.gdRoot.levels[level.index];
const gdWave = gdLevel.waves[level.currentWave];
const gdEnemy = simMain.gdRoot.enemies[gdWave.enemy];
this.index = index; this.index = index;
this.routeIdx = routeIdx; this.routeIdx = routeIdx;
this.startHex = gdLevel.enemySpawns[gdLevel.enemyRoutes[routeIdx][0]];
this.endHex = gdLevel.enemyTargets[gdLevel.enemyRoutes[routeIdx][1]];
const startIndex = level.getCellIndex(this.startHex);
this.path = [...level.cells[startIndex].pathsToTarget[routeIdx]];
this.currentPathIndex = 0;
this.currentPathStep = step;
this.speed = gdEnemy.speed;
this.hitPonts = 10; // TODO gd
this.size = EEnemySize.Tiny; // TODO gd
this.gain = 10; // TODO gd
this.onPathUpdated(level, true);
} }
public suffer(level: SimLevel, effect: GdProjectileEffect) { public suffer(level: SimLevel, effect: GdProjectileEffect) {
@@ -41,7 +61,7 @@ export class SimEnemy {
} }
} }
public onPathUpdated(level: SimLevel) { public onPathUpdated(level: SimLevel, initPosition: boolean = false) {
if (this.path == null) { if (this.path == null) {
return; return;
} }
@@ -63,6 +83,12 @@ export class SimEnemy {
this.path.unshift(this.path[0]); this.path.unshift(this.path[0]);
} }
if (initPosition)
{
this.currentPathPosition = pos1.clone();
this.prevPosition = pos1.clone();
this.position = pos1.clone();
}
this.direction = myDir.multiplyScalar(1 / myLen); this.direction = myDir.multiplyScalar(1 / myLen);
} }
} }

View File

@@ -5,6 +5,7 @@ import { PathFinding } from '../util/PathFinding';
import { ECellType } from './ECellType'; import { ECellType } from './ECellType';
import { SimCell } from './SimCell'; import { SimCell } from './SimCell';
import { SimEnemy } from './SimEnemy'; import { SimEnemy } from './SimEnemy';
import { SimMain } from './SimMain';
import { SimProjectile } from './SimProjectile'; import { SimProjectile } from './SimProjectile';
export class SimLevel { export class SimLevel {
@@ -63,6 +64,19 @@ export class SimLevel {
this.updatePaths(gdRoot); this.updatePaths(gdRoot);
} }
public startNextWave(simMain: SimMain) {
const data = simMain.gdRoot.levels[this.index];
this.currentWave += 1;
if (!data.waves[this.currentWave]) {
this.paused = true;
return;
}
this.nextWaveStep = this.currentStep + simMain.gdRoot.simulation.waveDuration * simMain.gdRoot.simulation.stepsPerSecond - 1;
this.lastEnemySpawnStep = this.currentStep;
this.enemiesLeftToSpawn = data.waves[this.currentWave].amount;
this.paused = false;
}
public getCellIndex(hex: Hex) { public getCellIndex(hex: Hex) {
const x = hex.col + this.radius; const x = hex.col + this.radius;

View File

@@ -35,29 +35,14 @@ export class SimActionSpawnEnemies implements ISimAction {
level.lastEnemySpawnStep = step; level.lastEnemySpawnStep = step;
const route = Math.floor(Math.random() * gdLevel.enemyRoutes.length); const route = Math.floor(Math.random() * gdLevel.enemyRoutes.length);
const simEnemy = new SimEnemy(gdWave.enemy, route); const simEnemy = new SimEnemy(gdWave.enemy, route, step, simMain);
simEnemy.startHex = gdLevel.enemySpawns[gdLevel.enemyRoutes[route][0]];
simEnemy.endHex = gdLevel.enemyTargets[gdLevel.enemyRoutes[route][1]];
simEnemy.position = Hex.toWorld(simEnemy.startHex);
simEnemy.currentPathPosition = simEnemy.position.clone();
simEnemy.prevPosition = simEnemy.position.clone();
const startIndex = level.getCellIndex(simEnemy.startHex);
const endIndex = level.getCellIndex(simEnemy.endHex);
const path = PathFinding.bfs(level, startIndex, endIndex);
simEnemy.path = path!;
simEnemy.currentPathIndex = 0;
simEnemy.speed = simMain.gdRoot.enemies[gdWave.enemy].speed;
simEnemy.currentPathStep = step;
simEnemy.hitPonts = 10; // TODO gd
simEnemy.size = EEnemySize.Tiny; // TODO gd
simEnemy.gain = 10; // TODO gd
simEnemy.onPathUpdated(level);
level.enemies.push(simEnemy); level.enemies.push(simEnemy);
} }
if (level.nextWaveStep == step && !!gdLevel.waves[level.currentWave + 1]) { if (level.nextWaveStep == step) {
simMain.addCommand(new SimCommandStartNextWave()); if (!!gdLevel.waves[level.currentWave + 1])
level.startNextWave(simMain);
} }
} }
} }

View File

@@ -8,16 +8,7 @@ export class SimCommandStartNextWave extends SimCommand {
return; return;
} }
const data = simMain.gdRoot.levels[level.index]; level.startNextWave(simMain);
level.currentWave += 1;
if (!data.waves[level.currentWave]) {
level.paused = true;
return;
}
level.nextWaveStep = level.currentStep + simMain.gdRoot.simulation.waveDuration * simMain.gdRoot.simulation.stepsPerSecond - 1;
level.lastEnemySpawnStep = level.currentStep;
level.enemiesLeftToSpawn = data.waves[level.currentWave].amount;
level.paused = false;
} }
public check(simMain: SimMain): boolean { public check(simMain: SimMain): boolean {