From 07996fdd6abd6811fc69665dd43b1a9e566f797b Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 12 Dec 2022 12:46:17 +0100 Subject: [PATCH] Enemy type refactoring --- assets/Data/GameSettings.json | 24 ++++++++++++++++--- assets/Scripts/Game/Data/GameSettings.ts | 13 ++++++++-- assets/Scripts/Game/Unit/Enemy/Enemy.ts | 19 +++++++++------ .../Scripts/Game/Unit/Enemy/EnemyManager.ts | 7 +++--- .../EnemySpawner/CircularEnemySpawner.ts | 13 ++++------ .../Unit/Enemy/EnemySpawner/EnemySpawner.ts | 17 ++++++++++--- .../EnemySpawner/IndividualEnemySpawner.ts | 6 ++--- .../Enemy/EnemySpawner/WaveEnemySpawner.ts | 10 +++----- assets/Scripts/Game/Unit/Enemy/EnemyType.ts | 3 --- .../Scripts/Game/Unit/Enemy/EnemyType.ts.meta | 9 ------- 10 files changed, 70 insertions(+), 51 deletions(-) delete mode 100644 assets/Scripts/Game/Unit/Enemy/EnemyType.ts delete mode 100644 assets/Scripts/Game/Unit/Enemy/EnemyType.ts.meta diff --git a/assets/Data/GameSettings.json b/assets/Data/GameSettings.json index fd1b2ab..1a2def9 100644 --- a/assets/Data/GameSettings.json +++ b/assets/Data/GameSettings.json @@ -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": "" + } + ] } } \ No newline at end of file diff --git a/assets/Scripts/Game/Data/GameSettings.ts b/assets/Scripts/Game/Data/GameSettings.ts index 948bc32..f8d99a3 100644 --- a/assets/Scripts/Game/Data/GameSettings.ts +++ b/assets/Scripts/Game/Data/GameSettings.ts @@ -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; } diff --git a/assets/Scripts/Game/Unit/Enemy/Enemy.ts b/assets/Scripts/Game/Unit/Enemy/Enemy.ts index 11d82d7..e7285f6 100644 --- a/assets/Scripts/Game/Unit/Enemy/Enemy.ts +++ b/assets/Scripts/Game/Unit/Enemy/Enemy.ts @@ -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 = new Signal(); + private movementType: EnemyMovementType; private health: UnitHealth = new UnitHealth(1); - private deathEvent: Signal = new Signal(); + 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 = 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 { diff --git a/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts b/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts index 8f3c77b..a06c764 100644 --- a/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts +++ b/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts @@ -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)); diff --git a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/CircularEnemySpawner.ts b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/CircularEnemySpawner.ts index aacf25d..1e9c89b 100644 --- a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/CircularEnemySpawner.ts +++ b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/CircularEnemySpawner.ts @@ -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); } } } diff --git a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/EnemySpawner.ts b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/EnemySpawner.ts index 8403eb1..9396bcb 100644 --- a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/EnemySpawner.ts +++ b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/EnemySpawner.ts @@ -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(); + + 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 { @@ -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); diff --git a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/IndividualEnemySpawner.ts b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/IndividualEnemySpawner.ts index f9daa53..2c1b7a4 100644 --- a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/IndividualEnemySpawner.ts +++ b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/IndividualEnemySpawner.ts @@ -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); } } } diff --git a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/WaveEnemySpawner.ts b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/WaveEnemySpawner.ts index 01e1e52..57d7174 100644 --- a/assets/Scripts/Game/Unit/Enemy/EnemySpawner/WaveEnemySpawner.ts +++ b/assets/Scripts/Game/Unit/Enemy/EnemySpawner/WaveEnemySpawner.ts @@ -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 = settings.enemyMoveType; - this.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); } diff --git a/assets/Scripts/Game/Unit/Enemy/EnemyType.ts b/assets/Scripts/Game/Unit/Enemy/EnemyType.ts deleted file mode 100644 index 1b1b752..0000000 --- a/assets/Scripts/Game/Unit/Enemy/EnemyType.ts +++ /dev/null @@ -1,3 +0,0 @@ -export enum EnemyType { - Basic = "Basic" -} diff --git a/assets/Scripts/Game/Unit/Enemy/EnemyType.ts.meta b/assets/Scripts/Game/Unit/Enemy/EnemyType.ts.meta deleted file mode 100644 index d7cbeac..0000000 --- a/assets/Scripts/Game/Unit/Enemy/EnemyType.ts.meta +++ /dev/null @@ -1,9 +0,0 @@ -{ - "ver": "4.0.23", - "importer": "typescript", - "imported": true, - "uuid": "8db8a72b-543d-4566-8900-7701372634e3", - "files": [], - "subMetas": {}, - "userData": {} -}