mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2024-12-26 03:38:58 +00:00
Projectile launcher refactoring
This commit is contained in:
parent
370e960f95
commit
2708c119f3
@ -1,5 +1,30 @@
|
|||||||
|
import { Vec3, Node, Vec2 } from "cc";
|
||||||
|
import { GameTimer } from "../../../../Services/GameTimer";
|
||||||
import { ProjectileLauncher } from "../../Player/ProjectileLauncher/ProjectileLauncher";
|
import { ProjectileLauncher } from "../../Player/ProjectileLauncher/ProjectileLauncher";
|
||||||
|
import { Enemy } from "../Enemy";
|
||||||
|
import { EnemyManager } from "../EnemyManager";
|
||||||
|
|
||||||
export class EnemyProjectileLauncher {
|
export class EnemyProjectileLauncher {
|
||||||
public constructor(private projectileLauncher: ProjectileLauncher) {}
|
private enemies: Enemy[] = [];
|
||||||
|
private shootTimer: GameTimer = new GameTimer(3);
|
||||||
|
|
||||||
|
public constructor(private playerNode: Node, private projectileLauncher: ProjectileLauncher, enemyManager: EnemyManager) {
|
||||||
|
enemyManager.EnemyAddedEvent.on(this.tryAddEnemy, this);
|
||||||
|
enemyManager.EnemyRemovedEvent.on(this.tryRemoveEnemy, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private tryAddEnemy(enemy: Enemy): void {
|
||||||
|
if (enemy.Id == "BasicEnemy") {
|
||||||
|
this.enemies.push();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private tryRemoveEnemy(enemy: Enemy): void {
|
||||||
|
if (enemy.Id == "BasicEnemy") {
|
||||||
|
const index = this.enemies.indexOf(enemy);
|
||||||
|
this.enemies.splice(index, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public gameTick(deltaTime: number): void {}
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,14 @@ import { HaloLauncherSettings } from "../../../Data/GameSettings";
|
|||||||
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
|
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
|
||||||
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
|
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
|
||||||
import { ProjectileData, ProjectileLauncher } from "./ProjectileLauncher";
|
import { ProjectileData, ProjectileLauncher } from "./ProjectileLauncher";
|
||||||
|
import { GameTimer } from "../../../../Services/GameTimer";
|
||||||
|
|
||||||
export class HaloProjectileLauncher implements IProjectileCollisionSignaler {
|
export class HaloProjectileLauncher implements IProjectileCollisionSignaler {
|
||||||
private currentUpgrade = 0;
|
private currentUpgrade = 0;
|
||||||
private defaultCooldown = 0;
|
private defaultCooldown = 0;
|
||||||
private cooldownDivisorPerUpgrade = 0;
|
private cooldownDivisorPerUpgrade = 0;
|
||||||
private directions: Vec2[] = [];
|
private directions: Vec2[] = [];
|
||||||
|
private fireTimer = new GameTimer(0);
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private launcher: ProjectileLauncher,
|
private launcher: ProjectileLauncher,
|
||||||
@ -29,7 +31,7 @@ export class HaloProjectileLauncher implements IProjectileCollisionSignaler {
|
|||||||
this.directions.push(new Vec2(x, y).normalize());
|
this.directions.push(new Vec2(x, y).normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
launcher.init(settings.launcher, projectileData);
|
launcher.init(settings.launcher.projectileLifetime, settings.launcher.projectileSpeed, projectileData.damage, projectileData.pierces);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
|
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
|
||||||
@ -39,11 +41,16 @@ export class HaloProjectileLauncher implements IProjectileCollisionSignaler {
|
|||||||
public gameTick(deltaTime: number): void {
|
public gameTick(deltaTime: number): void {
|
||||||
if (this.currentUpgrade == 0) return;
|
if (this.currentUpgrade == 0) return;
|
||||||
|
|
||||||
this.launcher.gameTick(deltaTime, this.playerNode.worldPosition, this.directions);
|
this.launcher.gameTick(deltaTime);
|
||||||
|
this.fireTimer.gameTick(deltaTime);
|
||||||
|
|
||||||
|
if (this.fireTimer.tryFinishPeriod()) {
|
||||||
|
this.launcher.fireProjectiles(this.playerNode.worldPosition, this.directions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public upgrade(): void {
|
public upgrade(): void {
|
||||||
this.currentUpgrade++;
|
this.currentUpgrade++;
|
||||||
this.launcher.Cooldown = this.defaultCooldown / (this.cooldownDivisorPerUpgrade * this.currentUpgrade);
|
this.fireTimer = new GameTimer(this.defaultCooldown / (this.cooldownDivisorPerUpgrade * this.currentUpgrade));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
import { _decorator, Component, Node, Prefab, Vec2, Vec3 } from "cc";
|
import { _decorator, Component, Prefab, Vec2, Vec3 } from "cc";
|
||||||
import { ISignal } from "../../../../Services/EventSystem/ISignal";
|
import { ISignal } from "../../../../Services/EventSystem/ISignal";
|
||||||
import { Signal } from "../../../../Services/EventSystem/Signal";
|
import { Signal } from "../../../../Services/EventSystem/Signal";
|
||||||
import { GameTimer } from "../../../../Services/GameTimer";
|
|
||||||
import { ObjectPool } from "../../../../Services/ObjectPool";
|
import { ObjectPool } from "../../../../Services/ObjectPool";
|
||||||
import { delay } from "../../../../Services/Utils/AsyncUtils";
|
|
||||||
import { getDegreeAngleFromDirection } from "../../../../Services/Utils/MathUtils";
|
import { getDegreeAngleFromDirection } from "../../../../Services/Utils/MathUtils";
|
||||||
import { ProjectileLauncherSettings } from "../../../Data/GameSettings";
|
|
||||||
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
|
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
|
||||||
import { Projectile } from "../../../Projectile/Projectile";
|
import { Projectile } from "../../../Projectile/Projectile";
|
||||||
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
|
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
|
||||||
@ -15,77 +12,47 @@ const { ccclass, property } = _decorator;
|
|||||||
export class ProjectileLauncher extends Component implements IProjectileCollisionSignaler {
|
export class ProjectileLauncher extends Component implements IProjectileCollisionSignaler {
|
||||||
@property(Prefab) private projectilePrefab: Prefab;
|
@property(Prefab) private projectilePrefab: Prefab;
|
||||||
private projectileCollisionEvent: Signal<ProjectileCollision> = new Signal<ProjectileCollision>();
|
private projectileCollisionEvent: Signal<ProjectileCollision> = new Signal<ProjectileCollision>();
|
||||||
private projectileData: ProjectileData;
|
|
||||||
private projectilePool: ObjectPool<Projectile>;
|
private projectileDamage: number;
|
||||||
private fireTimer: GameTimer;
|
private projectilePierces: number;
|
||||||
private projectileLifetime: number;
|
private projectileLifetime: number;
|
||||||
private speed: number;
|
private projectileSpeed: number;
|
||||||
private wavesToShoot: number;
|
|
||||||
private wavesDelayMs: number;
|
private projectilePool: ObjectPool<Projectile>;
|
||||||
private cooldown: number;
|
|
||||||
|
|
||||||
private projectiles: Projectile[] = [];
|
private projectiles: Projectile[] = [];
|
||||||
private directions: Vec2[] = [];
|
private directions: Vec2[] = [];
|
||||||
private expireTimes: number[] = [];
|
private expireTimes: number[] = [];
|
||||||
private currentTime = 0;
|
private currentTime = 0;
|
||||||
|
|
||||||
public get WavesToShoot(): number {
|
|
||||||
return this.wavesToShoot;
|
|
||||||
}
|
|
||||||
|
|
||||||
public set WavesToShoot(value: number) {
|
|
||||||
this.wavesToShoot = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get Cooldown(): number {
|
|
||||||
return this.cooldown;
|
|
||||||
}
|
|
||||||
|
|
||||||
public set Cooldown(value: number) {
|
|
||||||
this.cooldown = value;
|
|
||||||
this.fireTimer = new GameTimer(this.cooldown);
|
|
||||||
}
|
|
||||||
|
|
||||||
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
|
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
|
||||||
return this.projectileCollisionEvent;
|
return this.projectileCollisionEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(settings: ProjectileLauncherSettings, projectileData: ProjectileData): void {
|
public init(projectileLifetime: number, projectileSpeed: number, projectileDamage: number, projectilePierces: number): void {
|
||||||
this.projectileData = projectileData;
|
this.projectileLifetime = projectileLifetime;
|
||||||
this.projectileLifetime = settings.projectileLifetime;
|
this.projectileSpeed = projectileSpeed;
|
||||||
this.speed = settings.projectileSpeed;
|
this.projectileDamage = projectileDamage;
|
||||||
this.wavesToShoot = settings.wavesToShoot;
|
this.projectilePierces = projectilePierces;
|
||||||
this.wavesDelayMs = settings.wavesDelayMs;
|
|
||||||
this.cooldown = settings.cooldown;
|
|
||||||
|
|
||||||
this.projectilePool = new ObjectPool<Projectile>(this.projectilePrefab, this.node, 6, "Projectile");
|
this.projectilePool = new ObjectPool<Projectile>(this.projectilePrefab, this.node, 6, "Projectile");
|
||||||
this.fireTimer = new GameTimer(this.cooldown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public gameTick(deltaTime: number, startPosition: Vec3, fireDirections: Vec2[]): void {
|
public gameTick(deltaTime: number): void {
|
||||||
this.currentTime += deltaTime;
|
this.currentTime += deltaTime;
|
||||||
this.fireTimer.gameTick(deltaTime);
|
|
||||||
if (this.fireTimer.tryFinishPeriod()) {
|
|
||||||
this.fireProjectiles(startPosition, fireDirections);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.tryRemoveExpiredProjectiles();
|
this.tryRemoveExpiredProjectiles();
|
||||||
this.moveAllProjectiles(deltaTime);
|
this.moveAllProjectiles(deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async fireProjectiles(startPosition: Vec3, fireDirections: Vec2[]): Promise<void> {
|
public fireProjectiles(startPosition: Vec3, fireDirections: Vec2[]): void {
|
||||||
for (let i = 0; i < this.wavesToShoot; i++) {
|
for (const direction of fireDirections) {
|
||||||
for (const direction of fireDirections) {
|
this.fireProjectile(startPosition, direction);
|
||||||
this.fireProjectile(startPosition, direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay(this.wavesDelayMs);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fireProjectile(startPosition: Vec3, direction: Vec2): void {
|
private fireProjectile(startPosition: Vec3, direction: Vec2): void {
|
||||||
const projectile: Projectile = this.projectilePool.borrow();
|
const projectile: Projectile = this.projectilePool.borrow();
|
||||||
projectile.init(this.projectileData.damage, this.projectileData.pierces, getDegreeAngleFromDirection(direction.x, direction.y));
|
projectile.init(this.projectileDamage, this.projectilePierces, getDegreeAngleFromDirection(direction.x, direction.y));
|
||||||
projectile.node.setWorldPosition(startPosition);
|
projectile.node.setWorldPosition(startPosition);
|
||||||
projectile.node.active = true;
|
projectile.node.active = true;
|
||||||
projectile.ContactBeginEvent.on(this.onProjectileCollision, this);
|
projectile.ContactBeginEvent.on(this.onProjectileCollision, this);
|
||||||
@ -129,8 +96,8 @@ export class ProjectileLauncher extends Component implements IProjectileCollisio
|
|||||||
private moveAllProjectiles(deltaTime: number): void {
|
private moveAllProjectiles(deltaTime: number): void {
|
||||||
for (let i = 0; i < this.projectiles.length; i++) {
|
for (let i = 0; i < this.projectiles.length; i++) {
|
||||||
const newPosition: Vec3 = this.projectiles[i].node.worldPosition;
|
const newPosition: Vec3 = this.projectiles[i].node.worldPosition;
|
||||||
newPosition.x += this.directions[i].x * deltaTime * this.speed;
|
newPosition.x += this.directions[i].x * deltaTime * this.projectileSpeed;
|
||||||
newPosition.y += this.directions[i].y * deltaTime * this.speed;
|
newPosition.y += this.directions[i].y * deltaTime * this.projectileSpeed;
|
||||||
|
|
||||||
this.projectiles[i].node.setWorldPosition(newPosition);
|
this.projectiles[i].node.setWorldPosition(newPosition);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { Node, Vec2 } from "cc";
|
import { Node, Vec2 } from "cc";
|
||||||
import { ISignal } from "../../../../Services/EventSystem/ISignal";
|
import { ISignal } from "../../../../Services/EventSystem/ISignal";
|
||||||
|
import { GameTimer } from "../../../../Services/GameTimer";
|
||||||
|
import { delay } from "../../../../Services/Utils/AsyncUtils";
|
||||||
import { WaveLauncherSettings } from "../../../Data/GameSettings";
|
import { WaveLauncherSettings } from "../../../Data/GameSettings";
|
||||||
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
|
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
|
||||||
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
|
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
|
||||||
@ -8,6 +10,9 @@ import { ProjectileData, ProjectileLauncher } from "./ProjectileLauncher";
|
|||||||
export class WaveProjectileLauncher implements IProjectileCollisionSignaler {
|
export class WaveProjectileLauncher implements IProjectileCollisionSignaler {
|
||||||
private currentUpgrade = 0;
|
private currentUpgrade = 0;
|
||||||
private wavesToShootPerUpgrade = 0;
|
private wavesToShootPerUpgrade = 0;
|
||||||
|
private fireTimer: GameTimer;
|
||||||
|
private wavesToShoot: number;
|
||||||
|
private wavesDelayMs: number;
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private launcher: ProjectileLauncher,
|
private launcher: ProjectileLauncher,
|
||||||
@ -17,7 +22,12 @@ export class WaveProjectileLauncher implements IProjectileCollisionSignaler {
|
|||||||
projectileData: ProjectileData
|
projectileData: ProjectileData
|
||||||
) {
|
) {
|
||||||
this.wavesToShootPerUpgrade = settings.wavesToShootPerUpgrade;
|
this.wavesToShootPerUpgrade = settings.wavesToShootPerUpgrade;
|
||||||
launcher.init(settings.launcher, projectileData);
|
|
||||||
|
this.fireTimer = new GameTimer(settings.launcher.cooldown);
|
||||||
|
this.wavesToShoot = settings.launcher.wavesToShoot;
|
||||||
|
this.wavesDelayMs = settings.launcher.wavesDelayMs;
|
||||||
|
|
||||||
|
launcher.init(settings.launcher.projectileLifetime, settings.launcher.projectileSpeed, projectileData.damage, projectileData.pierces);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
|
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
|
||||||
@ -27,11 +37,24 @@ export class WaveProjectileLauncher implements IProjectileCollisionSignaler {
|
|||||||
public gameTick(deltaTime: number): void {
|
public gameTick(deltaTime: number): void {
|
||||||
if (this.currentUpgrade == 0) return;
|
if (this.currentUpgrade == 0) return;
|
||||||
|
|
||||||
this.launcher.gameTick(deltaTime, this.playerNode.worldPosition, this.directions);
|
this.launcher.gameTick(deltaTime);
|
||||||
|
this.fireTimer.gameTick(deltaTime);
|
||||||
|
|
||||||
|
if (this.fireTimer.tryFinishPeriod()) {
|
||||||
|
this.fireProjectiles();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public upgrade(): void {
|
public upgrade(): void {
|
||||||
this.currentUpgrade++;
|
this.currentUpgrade++;
|
||||||
this.launcher.WavesToShoot += this.wavesToShootPerUpgrade;
|
this.wavesToShoot += this.wavesToShootPerUpgrade;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async fireProjectiles(): Promise<void> {
|
||||||
|
for (let i = 0; i < this.wavesToShoot; i++) {
|
||||||
|
this.launcher.fireProjectiles(this.playerNode.worldPosition, this.directions);
|
||||||
|
|
||||||
|
await delay(this.wavesDelayMs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user