mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 00:26:04 +00:00
Projectile launcher
This commit is contained in:
@@ -33,11 +33,11 @@ export class HaloProjectileLauncher extends Component {
|
||||
public init(playerNode: Node, settings: HaloLauncherSettings): void {
|
||||
this.playerNode = playerNode;
|
||||
this.projectilesToSpawn = settings.projectilesToSpawn;
|
||||
this.projectilePool = new ObjectPool<Projectile>(this.projectilePrefab, this.node, this.projectilesToSpawn, "PlayerProjectile");
|
||||
this.projectilePool = new ObjectPool<Projectile>(this.projectilePrefab, this.node, this.projectilesToSpawn, "Projectile");
|
||||
|
||||
this.speed = settings.projectileSpeed;
|
||||
this.defaultCooldown = settings.cooldown;
|
||||
this.lifetimeTimer = new GameTimer(settings.projectileLifetime);
|
||||
this.speed = settings.launcher.projectileSpeed;
|
||||
this.defaultCooldown = settings.launcher.cooldown;
|
||||
this.lifetimeTimer = new GameTimer(settings.launcher.projectileLifetime);
|
||||
this.fireTimer = new GameTimer(this.defaultCooldown);
|
||||
|
||||
const angle: number = (2 * Math.PI) / this.projectilesToSpawn;
|
||||
|
@@ -0,0 +1,92 @@
|
||||
import { _decorator, Component, Node, Prefab, Vec2, Vec3 } from "cc";
|
||||
import { GameTimer } from "../../../../Services/GameTimer";
|
||||
import { ObjectPool } from "../../../../Services/ObjectPool";
|
||||
import { delay } from "../../../../Services/Utils/AsyncUtils";
|
||||
import { Projectile } from "../../../Projectile/Projectile";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("ProjectileLauncher")
|
||||
export class ProjectileLauncher extends Component {
|
||||
@property(Prefab) private projectilePrefab: Prefab;
|
||||
private projectilePool: ObjectPool<Projectile>;
|
||||
private fireTimer: GameTimer;
|
||||
private projectileLifetime = 5;
|
||||
private speed = 300;
|
||||
|
||||
private fireDirections: Vec2[];
|
||||
|
||||
private projectiles: Projectile[] = [];
|
||||
private directions: Vec2[] = [];
|
||||
private expireTimes: number[] = [];
|
||||
private currentTime = 0;
|
||||
|
||||
private currentUpgrade = 0;
|
||||
|
||||
private playerNode: Node;
|
||||
|
||||
public init(playerNode: Node, fireDirections: Vec2[]): void {
|
||||
this.playerNode = playerNode;
|
||||
this.fireDirections = fireDirections;
|
||||
this.projectilePool = new ObjectPool<Projectile>(this.projectilePrefab, this.node, 6, "Projectile");
|
||||
this.fireTimer = new GameTimer(2);
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
if (this.currentUpgrade == 0) return;
|
||||
|
||||
this.currentTime += deltaTime;
|
||||
this.fireTimer.gameTick(deltaTime);
|
||||
if (this.fireTimer.tryFinishPeriod()) {
|
||||
this.fireProjectiles();
|
||||
}
|
||||
|
||||
this.tryRemoveExpiredProjectiles();
|
||||
this.moveAllProjectiles(deltaTime);
|
||||
}
|
||||
|
||||
public upgrade(): void {
|
||||
this.currentUpgrade++;
|
||||
}
|
||||
|
||||
private async fireProjectiles(): Promise<void> {
|
||||
for (let i = 0; i < this.currentUpgrade; i++) {
|
||||
await delay(100);
|
||||
for (const direction of this.fireDirections) {
|
||||
this.fireProjectile(direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fireProjectile(direction: Vec2): void {
|
||||
const projectile: Projectile = this.projectilePool.borrow();
|
||||
projectile.tryInit();
|
||||
projectile.node.setWorldPosition(this.playerNode.worldPosition);
|
||||
projectile.node.active = true;
|
||||
|
||||
this.projectiles.push(projectile);
|
||||
this.directions.push(direction);
|
||||
this.expireTimes.push(this.currentTime + this.projectileLifetime);
|
||||
}
|
||||
|
||||
private tryRemoveExpiredProjectiles(): void {
|
||||
for (let i = 0; i < this.projectiles.length; i++) {
|
||||
if (this.currentTime < this.expireTimes[i]) break; // the oldest particles are at the start of the array
|
||||
|
||||
this.projectilePool.return(this.projectiles[i]);
|
||||
this.projectiles.splice(i, 1);
|
||||
this.directions.splice(i, 1);
|
||||
this.expireTimes.splice(i, 1);
|
||||
i--; // Check the same index
|
||||
}
|
||||
}
|
||||
|
||||
private moveAllProjectiles(deltaTime: number): void {
|
||||
for (let i = 0; i < this.projectiles.length; i++) {
|
||||
const newPosition: Vec3 = this.projectiles[i].node.worldPosition;
|
||||
newPosition.x += this.directions[i].x * deltaTime * this.speed;
|
||||
newPosition.y += this.directions[i].y * deltaTime * this.speed;
|
||||
|
||||
this.projectiles[i].node.setWorldPosition(newPosition);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "e3d06e80-f1ec-4505-a596-9f6831e1ce23",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user