Projectile Launcher refactor

This commit is contained in:
Martin 2022-12-20 14:37:26 +01:00
parent a327fc4a8d
commit 370e960f95
7 changed files with 49 additions and 24 deletions

View File

@ -196,7 +196,7 @@
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
"startTime": 0, "startTime": 0,
"startXP": 0, "startXP": 200,
"maxHpLevel": 0, "maxHpLevel": 0,
"bonusDamageLevel": 0, "bonusDamageLevel": 0,
"projectilePiercingLevel": 0, "projectilePiercingLevel": 0,

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "d360ea8e-7bcb-4c85-bb9e-4a4bcfb800ae",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,5 @@
import { ProjectileLauncher } from "../../Player/ProjectileLauncher/ProjectileLauncher";
export class EnemyProjectileLauncher {
public constructor(private projectileLauncher: ProjectileLauncher) {}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "057f6d42-fe19-45af-befe-8d665f8a201f",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -10,21 +10,26 @@ 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[] = [];
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.defaultCooldown = settings.launcher.cooldown;
this.cooldownDivisorPerUpgrade = settings.cooldownDivisorPerUpgrade; this.cooldownDivisorPerUpgrade = settings.cooldownDivisorPerUpgrade;
const directions: Vec2[] = [];
const angle: number = (2 * Math.PI) / settings.projectilesToSpawn; const angle: number = (2 * Math.PI) / settings.projectilesToSpawn;
for (let i = 0; i < settings.projectilesToSpawn; i++) { for (let i = 0; i < settings.projectilesToSpawn; i++) {
const x: number = roundToOneDecimal(Math.sin(angle * i)); const x: number = roundToOneDecimal(Math.sin(angle * i));
const y: number = roundToOneDecimal(Math.cos(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<ProjectileCollision> { public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
@ -34,7 +39,7 @@ 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.launcher.gameTick(deltaTime, this.playerNode.worldPosition, this.directions);
} }
public upgrade(): void { public upgrade(): void {

View File

@ -24,15 +24,11 @@ export class ProjectileLauncher extends Component implements IProjectileCollisio
private wavesDelayMs: number; private wavesDelayMs: number;
private cooldown: number; private cooldown: number;
private fireDirections: Vec2[];
private projectiles: Projectile[] = []; private projectiles: Projectile[] = [];
private directions: Vec2[] = []; private directions: Vec2[] = [];
private expireTimes: number[] = []; private expireTimes: number[] = [];
private currentTime = 0; private currentTime = 0;
private playerNode: Node;
public get WavesToShoot(): number { public get WavesToShoot(): number {
return this.wavesToShoot; return this.wavesToShoot;
} }
@ -54,7 +50,7 @@ export class ProjectileLauncher extends Component implements IProjectileCollisio
return this.projectileCollisionEvent; 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.projectileData = projectileData;
this.projectileLifetime = settings.projectileLifetime; this.projectileLifetime = settings.projectileLifetime;
this.speed = settings.projectileSpeed; this.speed = settings.projectileSpeed;
@ -62,37 +58,35 @@ export class ProjectileLauncher extends Component implements IProjectileCollisio
this.wavesDelayMs = settings.wavesDelayMs; this.wavesDelayMs = settings.wavesDelayMs;
this.cooldown = settings.cooldown; this.cooldown = settings.cooldown;
this.playerNode = playerNode;
this.fireDirections = fireDirections;
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); this.fireTimer = new GameTimer(this.cooldown);
} }
public gameTick(deltaTime: number): void { public gameTick(deltaTime: number, startPosition: Vec3, fireDirections: Vec2[]): void {
this.currentTime += deltaTime; this.currentTime += deltaTime;
this.fireTimer.gameTick(deltaTime); this.fireTimer.gameTick(deltaTime);
if (this.fireTimer.tryFinishPeriod()) { if (this.fireTimer.tryFinishPeriod()) {
this.fireProjectiles(); this.fireProjectiles(startPosition, fireDirections);
} }
this.tryRemoveExpiredProjectiles(); this.tryRemoveExpiredProjectiles();
this.moveAllProjectiles(deltaTime); this.moveAllProjectiles(deltaTime);
} }
private async fireProjectiles(): Promise<void> { private async fireProjectiles(startPosition: Vec3, fireDirections: Vec2[]): Promise<void> {
for (let i = 0; i < this.wavesToShoot; i++) { for (let i = 0; i < this.wavesToShoot; i++) {
for (const direction of this.fireDirections) { for (const direction of fireDirections) {
this.fireProjectile(direction); this.fireProjectile(startPosition, direction);
} }
await delay(this.wavesDelayMs); await delay(this.wavesDelayMs);
} }
} }
private fireProjectile(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.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.node.active = true;
projectile.ContactBeginEvent.on(this.onProjectileCollision, this); projectile.ContactBeginEvent.on(this.onProjectileCollision, this);
projectile.PiercesDepletedEvent.on(this.onPiercesDepleted, this); projectile.PiercesDepletedEvent.on(this.onPiercesDepleted, this);

View File

@ -11,13 +11,13 @@ export class WaveProjectileLauncher implements IProjectileCollisionSignaler {
public constructor( public constructor(
private launcher: ProjectileLauncher, private launcher: ProjectileLauncher,
playerNode: Node, private playerNode: Node,
directions: Vec2[], private directions: Vec2[],
settings: WaveLauncherSettings, settings: WaveLauncherSettings,
projectileData: ProjectileData projectileData: ProjectileData
) { ) {
this.wavesToShootPerUpgrade = settings.wavesToShootPerUpgrade; this.wavesToShootPerUpgrade = settings.wavesToShootPerUpgrade;
launcher.init(playerNode, directions, settings.launcher, projectileData); launcher.init(settings.launcher, projectileData);
} }
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> { public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
@ -27,7 +27,7 @@ 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.launcher.gameTick(deltaTime, this.playerNode.worldPosition, this.directions);
} }
public upgrade(): void { public upgrade(): void {