From 370e960f954357d031adc90054e9fa4a57f6777f Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 20 Dec 2022 14:37:26 +0100 Subject: [PATCH] Projectile Launcher refactor --- assets/Scenes/Game.scene | 2 +- .../Unit/Enemy/ProjectileLauncher.cs.meta | 12 ++++++++++ .../EnemyProjectileLauncher.ts | 5 +++++ .../EnemyProjectileLauncher.ts.meta | 9 ++++++++ .../HaloProjectileLauncher.ts | 15 ++++++++----- .../ProjectileLauncher/ProjectileLauncher.ts | 22 +++++++------------ .../WaveProjectileLauncher.ts | 8 +++---- 7 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs.meta create mode 100644 assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts create mode 100644 assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts.meta diff --git a/assets/Scenes/Game.scene b/assets/Scenes/Game.scene index 3cadb2a..dda83d9 100644 --- a/assets/Scenes/Game.scene +++ b/assets/Scenes/Game.scene @@ -196,7 +196,7 @@ "_enabled": true, "__prefab": null, "startTime": 0, - "startXP": 0, + "startXP": 200, "maxHpLevel": 0, "bonusDamageLevel": 0, "projectilePiercingLevel": 0, diff --git a/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs.meta b/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs.meta new file mode 100644 index 0000000..3816619 --- /dev/null +++ b/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs.meta @@ -0,0 +1,12 @@ +{ + "ver": "1.1.0", + "importer": "directory", + "imported": true, + "uuid": "d360ea8e-7bcb-4c85-bb9e-4a4bcfb800ae", + "files": [], + "subMetas": {}, + "userData": { + "compressionType": {}, + "isRemoteBundle": {} + } +} diff --git a/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts b/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts new file mode 100644 index 0000000..425dbe0 --- /dev/null +++ b/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts @@ -0,0 +1,5 @@ +import { ProjectileLauncher } from "../../Player/ProjectileLauncher/ProjectileLauncher"; + +export class EnemyProjectileLauncher { + public constructor(private projectileLauncher: ProjectileLauncher) {} +} diff --git a/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts.meta b/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts.meta new file mode 100644 index 0000000..8bebbb8 --- /dev/null +++ b/assets/Scripts/Game/Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "057f6d42-fe19-45af-befe-8d665f8a201f", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/Game/Unit/Player/ProjectileLauncher/HaloProjectileLauncher.ts b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/HaloProjectileLauncher.ts index 1965136..840e69d 100644 --- a/assets/Scripts/Game/Unit/Player/ProjectileLauncher/HaloProjectileLauncher.ts +++ b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/HaloProjectileLauncher.ts @@ -10,21 +10,26 @@ export class HaloProjectileLauncher implements IProjectileCollisionSignaler { private currentUpgrade = 0; private defaultCooldown = 0; private cooldownDivisorPerUpgrade = 0; + private directions: Vec2[] = []; - public constructor(private launcher: ProjectileLauncher, playerNode: Node, settings: HaloLauncherSettings, projectileData: ProjectileData) { + public constructor( + private launcher: ProjectileLauncher, + private playerNode: Node, + settings: HaloLauncherSettings, + projectileData: ProjectileData + ) { this.defaultCooldown = settings.launcher.cooldown; this.cooldownDivisorPerUpgrade = settings.cooldownDivisorPerUpgrade; - const directions: Vec2[] = []; const angle: number = (2 * Math.PI) / settings.projectilesToSpawn; for (let i = 0; i < settings.projectilesToSpawn; i++) { const x: number = roundToOneDecimal(Math.sin(angle * i)); const y: number = roundToOneDecimal(Math.cos(angle * i)); - directions.push(new Vec2(x, y).normalize()); + this.directions.push(new Vec2(x, y).normalize()); } - launcher.init(playerNode, directions, settings.launcher, projectileData); + launcher.init(settings.launcher, projectileData); } public get ProjectileCollisionEvent(): ISignal { @@ -34,7 +39,7 @@ export class HaloProjectileLauncher implements IProjectileCollisionSignaler { public gameTick(deltaTime: number): void { if (this.currentUpgrade == 0) return; - this.launcher.gameTick(deltaTime); + this.launcher.gameTick(deltaTime, this.playerNode.worldPosition, this.directions); } public upgrade(): void { diff --git a/assets/Scripts/Game/Unit/Player/ProjectileLauncher/ProjectileLauncher.ts b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/ProjectileLauncher.ts index e5167b1..da81fe5 100644 --- a/assets/Scripts/Game/Unit/Player/ProjectileLauncher/ProjectileLauncher.ts +++ b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/ProjectileLauncher.ts @@ -24,15 +24,11 @@ export class ProjectileLauncher extends Component implements IProjectileCollisio private wavesDelayMs: number; private cooldown: number; - private fireDirections: Vec2[]; - private projectiles: Projectile[] = []; private directions: Vec2[] = []; private expireTimes: number[] = []; private currentTime = 0; - private playerNode: Node; - public get WavesToShoot(): number { return this.wavesToShoot; } @@ -54,7 +50,7 @@ export class ProjectileLauncher extends Component implements IProjectileCollisio return this.projectileCollisionEvent; } - public init(playerNode: Node, fireDirections: Vec2[], settings: ProjectileLauncherSettings, projectileData: ProjectileData): void { + public init(settings: ProjectileLauncherSettings, projectileData: ProjectileData): void { this.projectileData = projectileData; this.projectileLifetime = settings.projectileLifetime; this.speed = settings.projectileSpeed; @@ -62,37 +58,35 @@ export class ProjectileLauncher extends Component implements IProjectileCollisio this.wavesDelayMs = settings.wavesDelayMs; this.cooldown = settings.cooldown; - this.playerNode = playerNode; - this.fireDirections = fireDirections; this.projectilePool = new ObjectPool(this.projectilePrefab, this.node, 6, "Projectile"); this.fireTimer = new GameTimer(this.cooldown); } - public gameTick(deltaTime: number): void { + public gameTick(deltaTime: number, startPosition: Vec3, fireDirections: Vec2[]): void { this.currentTime += deltaTime; this.fireTimer.gameTick(deltaTime); if (this.fireTimer.tryFinishPeriod()) { - this.fireProjectiles(); + this.fireProjectiles(startPosition, fireDirections); } this.tryRemoveExpiredProjectiles(); this.moveAllProjectiles(deltaTime); } - private async fireProjectiles(): Promise { + private async fireProjectiles(startPosition: Vec3, fireDirections: Vec2[]): Promise { for (let i = 0; i < this.wavesToShoot; i++) { - for (const direction of this.fireDirections) { - this.fireProjectile(direction); + for (const direction of fireDirections) { + this.fireProjectile(startPosition, direction); } await delay(this.wavesDelayMs); } } - private fireProjectile(direction: Vec2): void { + private fireProjectile(startPosition: Vec3, direction: Vec2): void { const projectile: Projectile = this.projectilePool.borrow(); projectile.init(this.projectileData.damage, this.projectileData.pierces, getDegreeAngleFromDirection(direction.x, direction.y)); - projectile.node.setWorldPosition(this.playerNode.worldPosition); + projectile.node.setWorldPosition(startPosition); projectile.node.active = true; projectile.ContactBeginEvent.on(this.onProjectileCollision, this); projectile.PiercesDepletedEvent.on(this.onPiercesDepleted, this); diff --git a/assets/Scripts/Game/Unit/Player/ProjectileLauncher/WaveProjectileLauncher.ts b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/WaveProjectileLauncher.ts index b52df9c..7184899 100644 --- a/assets/Scripts/Game/Unit/Player/ProjectileLauncher/WaveProjectileLauncher.ts +++ b/assets/Scripts/Game/Unit/Player/ProjectileLauncher/WaveProjectileLauncher.ts @@ -11,13 +11,13 @@ export class WaveProjectileLauncher implements IProjectileCollisionSignaler { public constructor( private launcher: ProjectileLauncher, - playerNode: Node, - directions: Vec2[], + private playerNode: Node, + private directions: Vec2[], settings: WaveLauncherSettings, projectileData: ProjectileData ) { this.wavesToShootPerUpgrade = settings.wavesToShootPerUpgrade; - launcher.init(playerNode, directions, settings.launcher, projectileData); + launcher.init(settings.launcher, projectileData); } public get ProjectileCollisionEvent(): ISignal { @@ -27,7 +27,7 @@ export class WaveProjectileLauncher implements IProjectileCollisionSignaler { public gameTick(deltaTime: number): void { if (this.currentUpgrade == 0) return; - this.launcher.gameTick(deltaTime); + this.launcher.gameTick(deltaTime, this.playerNode.worldPosition, this.directions); } public upgrade(): void {