Projectile launcher

This commit is contained in:
Martin 2022-11-30 09:37:09 +01:00
parent 7e20e41482
commit ac9b67503d
7 changed files with 465 additions and 284 deletions

View File

@ -20,11 +20,15 @@
"damage": 1
},
"haloLauncher": {
"projectileLifetime": 2,
"projectileSpeed": 150,
"projectilesToSpawn": 24,
"launcher": {
"projectileLifetime": 5,
"projectileSpeed": 150,
"wavesToShoot": 1,
"wavesDelayMs": 0,
"cooldown": 10
}
}
},
"upgrades": {
"maxWeaponLengthUpgrades": 5,

File diff suppressed because it is too large Load Diff

View File

@ -18,9 +18,15 @@ export class WeaponSettings {
}
export class HaloLauncherSettings {
public projectilesToSpawn = 0;
public launcher = new ProjectileLauncherSettings();
}
export class ProjectileLauncherSettings {
public projectileLifetime = 0;
public projectileSpeed = 0;
public projectilesToSpawn = 0;
public wavesToShoot = 0;
public wavesDelayMs = 0;
public cooldown = 0;
}

View File

@ -1,4 +1,4 @@
import { Camera, Component, JsonAsset, KeyCode, _decorator } from "cc";
import { Camera, Component, JsonAsset, KeyCode, Vec2, _decorator } from "cc";
import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem";
@ -13,6 +13,7 @@ import { GameUI } from "./UI/GameUI";
import { EnemyManager } from "./Unit/Enemy/EnemyManager";
import { Player } from "./Unit/Player/Player";
import { HaloProjectileLauncher } from "./Unit/Player/ProjectileLauncher/Halo/HaloProjectileLauncher";
import { ProjectileLauncher } from "./Unit/Player/ProjectileLauncher/ProjectileLauncher";
import { Upgrader } from "./Upgrades/Upgrader";
const { ccclass, property } = _decorator;
@ -22,6 +23,7 @@ export class GameBootstrapper extends Component {
@property(VirtualJoystic) private virtualJoystic: VirtualJoystic;
@property(Player) private player: Player;
@property(HaloProjectileLauncher) private haloProjectiles: HaloProjectileLauncher;
@property(ProjectileLauncher) private verticalProjectileLauncher: ProjectileLauncher;
@property(EnemyManager) private enemyManager: EnemyManager;
@property(Camera) private camera: Camera;
@property(GameUI) private gameUI: GameUI;
@ -53,6 +55,9 @@ export class GameBootstrapper extends Component {
this.haloProjectiles.init(this.player.node, settings.player.haloLauncher);
this.haloProjectiles.upgrade();
this.verticalProjectileLauncher.init(this.player.node, [new Vec2(-1, 0)]);
this.verticalProjectileLauncher.upgrade();
new PlayerProjectileCollisionSystem(this.haloProjectiles);
this.gameUI.init(this.player);
@ -65,6 +70,7 @@ export class GameBootstrapper extends Component {
this.playerCollisionSystem.gameTick(deltaTime);
this.enemyManager.gameTick(deltaTime);
this.haloProjectiles.gameTick(deltaTime);
this.verticalProjectileLauncher.gameTick(deltaTime);
this.camera.node.worldPosition = this.player.node.worldPosition;
}

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "e3d06e80-f1ec-4505-a596-9f6831e1ce23",
"files": [],
"subMetas": {},
"userData": {}
}