Enemy type refactoring

This commit is contained in:
Martin 2022-12-12 12:46:17 +01:00
parent 6cced4ecfc
commit 07996fdd6a
10 changed files with 70 additions and 51 deletions

View File

@ -60,12 +60,30 @@
"maxRegenerationUpgrades": 5
},
"enemyManager": {
"enemies": [
{
"id": "Basic",
"moveType": "Follow",
"health": 1,
"damage": 1,
"speed": 50
}
],
"waveEnemySpawner": {
"cooldown": 5,
"enemiesPerWave": 30,
"waveLifetime": 20,
"enemyMoveType": "Launch",
"enemyType": "Basic"
"enemyId": "Basic"
},
"waveEnemySpawners": [
{
"cooldown": 0,
"enemiesPerWave": 0,
"waveLifetime": 0,
"enemyName": "",
"enemyMoveType": "",
"enemyType": ""
}
]
}
}

View File

@ -49,13 +49,22 @@ export class UpgradeSettings {
}
export class EnemyManagerSettings {
public enemies: EnemySettings[] = [new EnemySettings()];
public waveEnemySpawner = new WaveEnemySpawnerSettings();
public waveEnemySpawners: WaveEnemySpawnerSettings[] = [new WaveEnemySpawnerSettings()];
}
export class WaveEnemySpawnerSettings {
public cooldown = 0;
public enemiesPerWave = 0;
public waveLifetime = 0;
public enemyMoveType = "";
public enemyType = "";
public enemyId = "";
}
export class EnemySettings {
public id = "";
public moveType = "";
public health = 0;
public damage = 0;
public speed = 0;
}

View File

@ -1,6 +1,7 @@
import { BoxCollider2D, Component, randomRange, Vec3, _decorator } from "cc";
import { ISignal } from "../../../Services/EventSystem/ISignal";
import { Signal } from "../../../Services/EventSystem/Signal";
import { EnemySettings } from "../../Data/GameSettings";
import { UnitHealth } from "../UnitHealth";
import { EnemyMovementType } from "./EnemyMovementType";
@ -10,17 +11,21 @@ const { ccclass, property } = _decorator;
export class Enemy extends Component {
@property(BoxCollider2D) public collider: BoxCollider2D;
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
private movementType: EnemyMovementType;
private health: UnitHealth = new UnitHealth(1);
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
private damage: number;
private speedX: number;
private speedY: number;
public setup(position: Vec3, movementType: EnemyMovementType): void {
this.movementType = movementType;
this.health = new UnitHealth(1);
this.speedX = randomRange(40, 90);
this.speedY = randomRange(40, 90);
public setup(position: Vec3, settings: EnemySettings): void {
this.movementType = <EnemyMovementType>settings.moveType;
this.health = new UnitHealth(settings.health);
this.damage = settings.damage;
this.speedX = randomRange(settings.speed / 2, settings.speed);
this.speedY = randomRange(settings.speed / 2, settings.speed);
this.node.setWorldPosition(position);
this.node.active = true;
}
@ -34,7 +39,7 @@ export class Enemy extends Component {
}
public get Damage(): number {
return 3;
return this.damage;
}
public get Health(): UnitHealth {

View File

@ -11,7 +11,6 @@ import { CircularEnemySpawner } from "./EnemySpawner/CircularEnemySpawner";
import { EnemySpawner } from "./EnemySpawner/EnemySpawner";
import { IndividualEnemySpawner } from "./EnemySpawner/IndividualEnemySpawner";
import { WaveEnemySpawner } from "./EnemySpawner/WaveEnemySpawner";
import { EnemyType } from "./EnemyType";
const { ccclass, property } = _decorator;
@ -27,12 +26,12 @@ export class EnemyManager extends Component {
private waveEnemySpawner: WaveEnemySpawner;
public init(targetNode: Node, settings: EnemyManagerSettings): void {
this.enemySpawner.init(targetNode);
this.enemySpawner.init(targetNode, settings.enemies);
this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this);
this.enemySpawner.enemyRemovedEvent.on(this.onRemoveEnemy, this);
this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, EnemyMovementType.Follow, EnemyType.Basic);
this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, EnemyMovementType.Follow, EnemyType.Basic);
this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, "Basic");
this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, "Basic");
this.waveEnemySpawner = new WaveEnemySpawner(this.enemySpawner, settings.waveEnemySpawner);
this.movementTypeToMover.set(EnemyMovementType.Follow, new FollowTargetEnemyMover(targetNode));

View File

@ -1,17 +1,12 @@
import { GameTimer } from "../../../../Services/GameTimer";
import { roundToOneDecimal } from "../../../../Services/Utils/MathUtils";
import { EnemyMovementType } from "../EnemyMovementType";
import { EnemyType } from "../EnemyType";
import { EnemySpawner } from "./EnemySpawner";
export class CircularEnemySpawner {
private spawnTimer: GameTimer = new GameTimer(10);
public constructor(
private enemySpawner: EnemySpawner,
private enemiesToSpawn: number,
private movementType: EnemyMovementType,
private enemyType: EnemyType
) {}
public constructor(private enemySpawner: EnemySpawner, private enemiesToSpawn: number, private enemyId: string) {}
public gameTick(deltaTime: number): void {
this.spawnTimer.gameTick(deltaTime);
@ -21,7 +16,7 @@ export class CircularEnemySpawner {
for (let i = 0; i < this.enemiesToSpawn; i++) {
const posX: number = roundToOneDecimal(Math.sin(angle * i)) * 500;
const posY: number = roundToOneDecimal(Math.cos(angle * i)) * 500;
this.enemySpawner.spawnNewEnemy(posX, posY, this.movementType);
this.enemySpawner.spawnNewEnemy(posX, posY, this.enemyId);
}
}
}

View File

@ -2,6 +2,7 @@ import { _decorator, Component, Prefab, Vec3, Node } from "cc";
import { ISignal } from "../../../../Services/EventSystem/ISignal";
import { Signal } from "../../../../Services/EventSystem/Signal";
import { ObjectPool } from "../../../../Services/ObjectPool";
import { EnemySettings } from "../../../Data/GameSettings";
import { Enemy } from "../Enemy";
import { EnemyMovementType } from "../EnemyMovementType";
@ -18,9 +19,15 @@ export class EnemySpawner extends Component {
private targetNode: Node;
public init(targetNode: Node): void {
private idToSettings = new Map<string, EnemySettings>();
public init(targetNode: Node, enemiesSettings: EnemySettings[]): void {
this.targetNode = targetNode;
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 50, "Enemy");
for (const enemySettings of enemiesSettings) {
this.idToSettings.set(enemySettings.id, enemySettings);
}
}
public get EnemyAddedEvent(): ISignal<Enemy> {
@ -31,12 +38,16 @@ export class EnemySpawner extends Component {
return this.enemyRemovedEvent;
}
public spawnNewEnemy(positionX: number, positionY: number, movementType: EnemyMovementType): Enemy {
public spawnNewEnemy(positionX: number, positionY: number, id: string): Enemy {
if (!this.idToSettings.has(id)) {
throw new Error("Does not have setting for enemy " + id);
}
const enemy = this.enemyPool.borrow();
const spawnPosition = new Vec3();
spawnPosition.x = this.targetNode.worldPosition.x + positionX;
spawnPosition.y = this.targetNode.worldPosition.y + positionY;
enemy.setup(spawnPosition, movementType);
enemy.setup(spawnPosition, this.idToSettings.get(id));
enemy.DeathEvent.on(this.returnEnemy, this);

View File

@ -1,21 +1,19 @@
import { randomRange } from "cc";
import { GameTimer } from "../../../../Services/GameTimer";
import { randomPositiveOrNegative } from "../../../../Services/Utils/MathUtils";
import { EnemyMovementType } from "../EnemyMovementType";
import { EnemyType } from "../EnemyType";
import { EnemySpawner } from "./EnemySpawner";
export class IndividualEnemySpawner {
private spawnTimer: GameTimer = new GameTimer(1);
public constructor(private enemySpawner: EnemySpawner, private movementType: EnemyMovementType, private enemyType: EnemyType) {}
public constructor(private enemySpawner: EnemySpawner, private enemyId: string) {}
public gameTick(deltaTime: number): void {
this.spawnTimer.gameTick(deltaTime);
if (this.spawnTimer.tryFinishPeriod()) {
const posX: number = randomRange(300, 600) * randomPositiveOrNegative();
const posY: number = randomRange(300, 600) * randomPositiveOrNegative();
this.enemySpawner.spawnNewEnemy(posX, posY, this.movementType);
this.enemySpawner.spawnNewEnemy(posX, posY, this.enemyId);
}
}
}

View File

@ -3,15 +3,12 @@ import { GameTimer } from "../../../../Services/GameTimer";
import { randomPositiveOrNegative } from "../../../../Services/Utils/MathUtils";
import { WaveEnemySpawnerSettings } from "../../../Data/GameSettings";
import { Enemy } from "../Enemy";
import { EnemyMovementType } from "../EnemyMovementType";
import { EnemyType } from "../EnemyType";
import { EnemySpawner } from "./EnemySpawner";
export class WaveEnemySpawner {
private enemiesPerWave: number;
private waveLifetime: number;
private moveType: EnemyMovementType;
private enemyType: EnemyType;
private enemyId: string;
private spawnTimer: GameTimer;
private waves: EnemyWave[] = [];
@ -20,8 +17,7 @@ export class WaveEnemySpawner {
this.spawnTimer = new GameTimer(settings.cooldown);
this.enemiesPerWave = settings.enemiesPerWave;
this.waveLifetime = settings.waveLifetime;
this.moveType = <EnemyMovementType>settings.enemyMoveType;
this.enemyType = <EnemyType>settings.enemyType;
this.enemyId = settings.enemyId;
}
public gameTick(deltaTime: number): void {
@ -60,7 +56,7 @@ export class WaveEnemySpawner {
const randomOffsetY: number = randomRange(-20, 20);
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, this.moveType);
const enemy = this.enemySpawner.spawnNewEnemy(posX, posY, this.enemyId);
enemies.push(enemy);
}

View File

@ -1,3 +0,0 @@
export enum EnemyType {
Basic = "Basic"
}

View File

@ -1,9 +0,0 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "8db8a72b-543d-4566-8900-7701372634e3",
"files": [],
"subMetas": {},
"userData": {}
}