enemy spawn and next wave start refactoring
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import { EEnemySize } from '../data/EEnemySize';
|
||||
import { EProjectileEffectType } from '../data/EProjectileEffectType';
|
||||
import { GdEnemy } from '../data/GdEnemy';
|
||||
import { GdLevel } from '../data/GdLevel';
|
||||
import { GdProjectileEffect } from '../data/GdProjectileEffect';
|
||||
import { Hex } from '../util/Hex';
|
||||
import { PathFinding } from '../util/PathFinding';
|
||||
import { Vector2 } from '../util/Vector2';
|
||||
import { SimLevel } from './SimLevel';
|
||||
import { SimMain } from './SimMain';
|
||||
|
||||
export class SimEnemy {
|
||||
index: number = -1;
|
||||
@@ -23,9 +27,25 @@ export class SimEnemy {
|
||||
speed: 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.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) {
|
||||
@@ -41,7 +61,7 @@ export class SimEnemy {
|
||||
}
|
||||
}
|
||||
|
||||
public onPathUpdated(level: SimLevel) {
|
||||
public onPathUpdated(level: SimLevel, initPosition: boolean = false) {
|
||||
if (this.path == null) {
|
||||
return;
|
||||
}
|
||||
@@ -63,6 +83,12 @@ export class SimEnemy {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { PathFinding } from '../util/PathFinding';
|
||||
import { ECellType } from './ECellType';
|
||||
import { SimCell } from './SimCell';
|
||||
import { SimEnemy } from './SimEnemy';
|
||||
import { SimMain } from './SimMain';
|
||||
import { SimProjectile } from './SimProjectile';
|
||||
|
||||
export class SimLevel {
|
||||
@@ -64,6 +65,19 @@ export class SimLevel {
|
||||
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) {
|
||||
const x = hex.col + this.radius;
|
||||
const y = hex.row + this.radius;
|
||||
|
||||
@@ -35,29 +35,14 @@ export class SimActionSpawnEnemies implements ISimAction {
|
||||
level.lastEnemySpawnStep = step;
|
||||
|
||||
const route = Math.floor(Math.random() * gdLevel.enemyRoutes.length);
|
||||
const simEnemy = new SimEnemy(gdWave.enemy, route);
|
||||
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
|
||||
const simEnemy = new SimEnemy(gdWave.enemy, route, step, simMain);
|
||||
|
||||
simEnemy.onPathUpdated(level);
|
||||
level.enemies.push(simEnemy);
|
||||
}
|
||||
|
||||
if (level.nextWaveStep == step && !!gdLevel.waves[level.currentWave + 1]) {
|
||||
simMain.addCommand(new SimCommandStartNextWave());
|
||||
if (level.nextWaveStep == step) {
|
||||
if (!!gdLevel.waves[level.currentWave + 1])
|
||||
level.startNextWave(simMain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,16 +8,7 @@ export class SimCommandStartNextWave extends SimCommand {
|
||||
return;
|
||||
}
|
||||
|
||||
const data = simMain.gdRoot.levels[level.index];
|
||||
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;
|
||||
level.startNextWave(simMain);
|
||||
}
|
||||
|
||||
public check(simMain: SimMain): boolean {
|
||||
|
||||
Reference in New Issue
Block a user