Vertical launcher

This commit is contained in:
Martin
2022-11-30 11:44:20 +01:00
parent ac9b67503d
commit ab39355a5c
16 changed files with 221 additions and 163 deletions

View File

@@ -1,12 +0,0 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "b89dc5a7-3450-47e4-bc36-ae5d08f4bc89",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -1,114 +0,0 @@
import { Component, Node, Prefab, Vec2, Vec3, _decorator } from "cc";
import { ISignal } from "../../../../../Services/EventSystem/ISignal";
import { Signal } from "../../../../../Services/EventSystem/Signal";
import { GameTimer } from "../../../../../Services/GameTimer";
import { ObjectPool } from "../../../../../Services/ObjectPool";
import { roundToOneDecimal } from "../../../../../Services/Utils/MathUtils";
import { HaloLauncherSettings } from "../../../../Data/GameSettings";
import { Projectile } from "../../../../Projectile/Projectile";
import { ProjectileCollision } from "../../../../Projectile/ProjectileCollision";
const { ccclass, property } = _decorator;
@ccclass("HaloProjectileLauncher")
export class HaloProjectileLauncher extends Component {
@property(Prefab) private projectilePrefab: Prefab;
private fireTimer: GameTimer;
private lifetimeTimer: GameTimer;
private projectilesToSpawn: number;
private defaultCooldown: number;
private speed: number;
private currentLevel = 0;
private isFiring = false;
private projectilePool: ObjectPool<Projectile>;
private projectiles: Projectile[] = [];
private directions: Vec2[] = [];
private playerNode: Node;
private projectileCollisionEvent: Signal<ProjectileCollision> = new Signal<ProjectileCollision>();
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, "Projectile");
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;
for (let i = 0; i < this.projectilesToSpawn; i++) {
const x: number = roundToOneDecimal(Math.sin(angle * i));
const y: number = roundToOneDecimal(Math.cos(angle * i));
this.directions.push(new Vec2(x, y).normalize());
}
}
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
return this.projectileCollisionEvent;
}
public upgrade(): void {
this.currentLevel++;
this.fireTimer = new GameTimer(this.defaultCooldown - this.currentLevel);
}
public gameTick(deltaTime: number): void {
if (this.currentLevel == 0) return;
this.fireTimer.gameTick(deltaTime);
if (this.isFiring) {
this.moveAllProjectiles(deltaTime);
this.tryRemoveAllProjectiles(deltaTime);
} else {
if (this.fireTimer.tryFinishPeriod()) {
this.fireProjectiles();
}
}
}
private fireProjectiles(): void {
for (let index = 0; index < this.projectilesToSpawn; index++) {
const projectile: Projectile = this.projectilePool.borrow();
projectile.tryInit();
projectile.node.setWorldPosition(this.playerNode.worldPosition);
projectile.node.active = true;
projectile.ContactBeginEvent.on(this.onProjectileCollision, this);
this.projectiles.push(projectile);
}
this.isFiring = true;
}
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);
}
}
private tryRemoveAllProjectiles(deltaTime: number): void {
this.lifetimeTimer.gameTick(deltaTime);
if (this.lifetimeTimer.tryFinishPeriod()) {
for (const projectile of this.projectiles) {
projectile.ContactBeginEvent.off(this.onProjectileCollision);
this.projectilePool.return(projectile);
}
this.projectiles = [];
this.isFiring = false;
}
}
private onProjectileCollision(projectileCollision: ProjectileCollision): void {
this.projectileCollisionEvent.trigger(projectileCollision);
}
}

View File

@@ -0,0 +1,44 @@
import { Vec2, Node } from "cc";
import { ISignal } from "../../../../Services/EventSystem/ISignal";
import { roundToOneDecimal } from "../../../../Services/Utils/MathUtils";
import { HaloLauncherSettings } from "../../../Data/GameSettings";
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
import { ProjectileLauncher } from "./ProjectileLauncher";
export class HaloProjectileLauncher implements IProjectileCollisionSignaler {
private currentUpgrade = 0;
private defaultCooldown = 0;
private cooldownDivisorPerUpgrade = 0;
public constructor(private launcher: ProjectileLauncher, playerNode: Node, settings: HaloLauncherSettings) {
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());
}
launcher.init(playerNode, directions, settings.launcher);
}
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
return this.launcher.ProjectileCollisionEvent;
}
public gameTick(deltaTime: number): void {
if (this.currentUpgrade == 0) return;
this.launcher.gameTick(deltaTime);
}
public upgrade(): void {
this.currentUpgrade++;
this.launcher.Cooldown = (this.defaultCooldown / this.cooldownDivisorPerUpgrade) * this.currentUpgrade;
}
}

View File

@@ -1,17 +1,27 @@
import { _decorator, Component, Node, Prefab, Vec2, Vec3 } from "cc";
import { ISignal } from "../../../../Services/EventSystem/ISignal";
import { Signal } from "../../../../Services/EventSystem/Signal";
import { GameTimer } from "../../../../Services/GameTimer";
import { ObjectPool } from "../../../../Services/ObjectPool";
import { delay } from "../../../../Services/Utils/AsyncUtils";
import { ProjectileLauncherSettings } from "../../../Data/GameSettings";
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
import { Projectile } from "../../../Projectile/Projectile";
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
const { ccclass, property } = _decorator;
@ccclass("ProjectileLauncher")
export class ProjectileLauncher extends Component {
export class ProjectileLauncher extends Component implements IProjectileCollisionSignaler {
@property(Prefab) private projectilePrefab: Prefab;
private projectileCollisionEvent: Signal<ProjectileCollision> = new Signal<ProjectileCollision>();
private projectilePool: ObjectPool<Projectile>;
private fireTimer: GameTimer;
private projectileLifetime = 5;
private speed = 300;
private projectileLifetime: number;
private speed: number;
private wavesToShoot: number;
private wavesDelayMs: number;
private cooldown: number;
private fireDirections: Vec2[];
@@ -20,20 +30,43 @@ export class ProjectileLauncher extends Component {
private expireTimes: number[] = [];
private currentTime = 0;
private currentUpgrade = 0;
private playerNode: Node;
public init(playerNode: Node, fireDirections: Vec2[]): void {
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> {
return this.projectileCollisionEvent;
}
public init(playerNode: Node, fireDirections: Vec2[], settings: ProjectileLauncherSettings): void {
this.projectileLifetime = settings.projectileLifetime;
this.speed = settings.projectileSpeed;
this.wavesToShoot = settings.wavesToShoot;
this.wavesDelayMs = settings.wavesDelayMs;
this.cooldown = settings.cooldown;
this.playerNode = playerNode;
this.fireDirections = fireDirections;
this.projectilePool = new ObjectPool<Projectile>(this.projectilePrefab, this.node, 6, "Projectile");
this.fireTimer = new GameTimer(2);
this.fireTimer = new GameTimer(this.cooldown);
}
public gameTick(deltaTime: number): void {
if (this.currentUpgrade == 0) return;
this.currentTime += deltaTime;
this.fireTimer.gameTick(deltaTime);
if (this.fireTimer.tryFinishPeriod()) {
@@ -44,16 +77,13 @@ export class ProjectileLauncher extends Component {
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 (let i = 0; i < this.wavesToShoot; i++) {
for (const direction of this.fireDirections) {
this.fireProjectile(direction);
}
await delay(this.wavesDelayMs);
}
}
@@ -62,6 +92,7 @@ export class ProjectileLauncher extends Component {
projectile.tryInit();
projectile.node.setWorldPosition(this.playerNode.worldPosition);
projectile.node.active = true;
projectile.ContactBeginEvent.on(this.onProjectileCollision, this);
this.projectiles.push(projectile);
this.directions.push(direction);
@@ -72,7 +103,10 @@ export class ProjectileLauncher extends Component {
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]);
const projectile: Projectile = this.projectiles[i];
projectile.ContactBeginEvent.off(this.onProjectileCollision);
this.projectilePool.return(projectile);
this.projectiles.splice(i, 1);
this.directions.splice(i, 1);
this.expireTimes.splice(i, 1);
@@ -89,4 +123,8 @@ export class ProjectileLauncher extends Component {
this.projectiles[i].node.setWorldPosition(newPosition);
}
}
private onProjectileCollision(projectlieCollision: ProjectileCollision): void {
this.projectileCollisionEvent.trigger(projectlieCollision);
}
}

View File

@@ -0,0 +1,31 @@
import { Node, Vec2 } from "cc";
import { ISignal } from "../../../../Services/EventSystem/ISignal";
import { WaveLauncherSettings } from "../../../Data/GameSettings";
import { IProjectileCollisionSignaler } from "../../../Projectile/IProjectileCollisionSignaler";
import { ProjectileCollision } from "../../../Projectile/ProjectileCollision";
import { ProjectileLauncher } from "./ProjectileLauncher";
export class VerticalProjectileLauncher implements IProjectileCollisionSignaler {
private currentUpgrade = 0;
private wavesToShootPerUpgrade = 0;
public constructor(private launcher: ProjectileLauncher, playerNode: Node, settings: WaveLauncherSettings) {
this.wavesToShootPerUpgrade = settings.wavesToShootPerUpgrade;
launcher.init(playerNode, [new Vec2(-1, 0), new Vec2(1, 0)], settings.launcher);
}
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
return this.launcher.ProjectileCollisionEvent;
}
public gameTick(deltaTime: number): void {
if (this.currentUpgrade == 0) return;
this.launcher.gameTick(deltaTime);
}
public upgrade(): void {
this.currentUpgrade++;
this.launcher.WavesToShoot += this.wavesToShootPerUpgrade;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "1e160c96-efda-4b1a-8e21-8c1b525bc7ef",
"files": [],
"subMetas": {},
"userData": {}
}