mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2024-12-26 11:48:54 +00:00
Wave lifetime
This commit is contained in:
parent
d109519190
commit
8e38cbfb27
@ -30,7 +30,7 @@ export class EnemyManager extends Component {
|
|||||||
|
|
||||||
this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, EnemyType.Basic);
|
this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, EnemyType.Basic);
|
||||||
this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, EnemyType.Basic);
|
this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, EnemyType.Basic);
|
||||||
this.waveEnemySpawner = new WaveEnemySpawner(this.enemySpawner, 30, EnemyType.Basic);
|
this.waveEnemySpawner = new WaveEnemySpawner(this.enemySpawner, 30, 10, EnemyType.Basic);
|
||||||
|
|
||||||
this.movementTypeToMover.set(EnemyMovementType.Follow, new FollowTargetEnemyMover(targetNode));
|
this.movementTypeToMover.set(EnemyMovementType.Follow, new FollowTargetEnemyMover(targetNode));
|
||||||
this.movementTypeToMover.set(EnemyMovementType.Launch, new LaunchToTargetEnemyMover(targetNode));
|
this.movementTypeToMover.set(EnemyMovementType.Launch, new LaunchToTargetEnemyMover(targetNode));
|
||||||
|
@ -7,7 +7,9 @@ import { EnemyType } from "./EnemyType";
|
|||||||
|
|
||||||
export class InvididualEnemySpawner {
|
export class InvididualEnemySpawner {
|
||||||
private spawnTimer: GameTimer = new GameTimer(1);
|
private spawnTimer: GameTimer = new GameTimer(1);
|
||||||
public constructor(private enemySpawner: EnemySpawner, enemyType: EnemyType) {}
|
|
||||||
|
public constructor(private enemySpawner: EnemySpawner, private enemyType: EnemyType) {}
|
||||||
|
|
||||||
public gameTick(deltaTime: number): void {
|
public gameTick(deltaTime: number): void {
|
||||||
this.spawnTimer.gameTick(deltaTime);
|
this.spawnTimer.gameTick(deltaTime);
|
||||||
if (this.spawnTimer.tryFinishPeriod()) {
|
if (this.spawnTimer.tryFinishPeriod()) {
|
||||||
|
@ -1,27 +1,64 @@
|
|||||||
|
import { randomRange } from "cc";
|
||||||
import { GameTimer } from "../../../Services/GameTimer";
|
import { GameTimer } from "../../../Services/GameTimer";
|
||||||
import { randomPositiveOrNegative } from "../../../Services/Utils/MathUtils";
|
import { randomPositiveOrNegative } from "../../../Services/Utils/MathUtils";
|
||||||
|
import { Enemy } from "./Enemy";
|
||||||
import { EnemyMovementType } from "./EnemyMovementType";
|
import { EnemyMovementType } from "./EnemyMovementType";
|
||||||
import { EnemySpawner } from "./EnemySpawner";
|
import { EnemySpawner } from "./EnemySpawner";
|
||||||
import { EnemyType } from "./EnemyType";
|
import { EnemyType } from "./EnemyType";
|
||||||
|
|
||||||
export class WaveEnemySpawner {
|
export class WaveEnemySpawner {
|
||||||
private spawnTimer: GameTimer = new GameTimer(5);
|
private spawnTimer: GameTimer = new GameTimer(5);
|
||||||
public constructor(private enemySpawner: EnemySpawner, private enemiesToSpawn: number, private enemyType: EnemyType) {}
|
private groups: EnemyGroupWithLifetime[] = [];
|
||||||
|
public constructor(
|
||||||
|
private enemySpawner: EnemySpawner,
|
||||||
|
private enemiesToSpawn: number,
|
||||||
|
private enemyLifeTime: number,
|
||||||
|
private enemyType: EnemyType
|
||||||
|
) {}
|
||||||
|
|
||||||
public gameTick(deltaTime: number): void {
|
public gameTick(deltaTime: number): void {
|
||||||
this.spawnTimer.gameTick(deltaTime);
|
this.spawnTimer.gameTick(deltaTime);
|
||||||
|
|
||||||
if (this.spawnTimer.tryFinishPeriod()) {
|
this.tryRemoveExpiredEnemies(deltaTime);
|
||||||
const angle: number = (2 * Math.PI) / this.enemiesToSpawn;
|
this.trySpawnNewGroup();
|
||||||
|
}
|
||||||
|
|
||||||
|
private tryRemoveExpiredEnemies(deltaTime: number): void {
|
||||||
|
for (let i = this.groups.length - 1; 0 <= i; i--) {
|
||||||
|
const group: EnemyGroupWithLifetime = this.groups[i];
|
||||||
|
group.lifeTimeLeft -= deltaTime;
|
||||||
|
if (group.lifeTimeLeft <= 0) {
|
||||||
|
for (const enemy of group.enemies) {
|
||||||
|
this.enemySpawner.returnEnemy(enemy);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.groups.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private trySpawnNewGroup(): void {
|
||||||
|
if (this.spawnTimer.tryFinishPeriod()) {
|
||||||
const defaultPosX: number = 200 * randomPositiveOrNegative();
|
const defaultPosX: number = 200 * randomPositiveOrNegative();
|
||||||
const defaultPosY: number = 200 * randomPositiveOrNegative();
|
const defaultPosY: number = 200 * randomPositiveOrNegative();
|
||||||
|
|
||||||
|
const enemies: Enemy[] = [];
|
||||||
|
const side: number = Math.ceil(Math.sqrt(this.enemiesToSpawn));
|
||||||
for (let i = 0; i < this.enemiesToSpawn; i++) {
|
for (let i = 0; i < this.enemiesToSpawn; i++) {
|
||||||
const posX: number = defaultPosX + 10 * i;
|
const randomOffsetX: number = randomRange(-20, 20);
|
||||||
const posY: number = defaultPosY + 10 * (i % 2);
|
const randomOffsetY: number = randomRange(-20, 20);
|
||||||
this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Launch);
|
const posX: number = defaultPosX + randomOffsetX + 50 * (i % side);
|
||||||
|
const posY: number = defaultPosY + randomOffsetY + 50 * Math.floor(i / side);
|
||||||
|
const enemy = this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Launch);
|
||||||
|
enemies.push(enemy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.groups.push({ enemies, lifeTimeLeft: this.enemyLifeTime });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class EnemyGroupWithLifetime {
|
||||||
|
public enemies: Enemy[];
|
||||||
|
public lifeTimeLeft: number;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user