Halo projectile launcher

This commit is contained in:
Martin
2022-11-29 15:55:47 +01:00
parent a0bd11b61d
commit da70723f2d
14 changed files with 1625 additions and 1182 deletions

View File

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

View File

@@ -0,0 +1,97 @@
import { Component, Prefab, Vec2, Vec3, _decorator, Node } from "cc";
import { GameTimer } from "../../../../Services/GameTimer";
import { ObjectPool } from "../../../../Services/ObjectPool";
import { roundToOneDecimal } from "../../../../Services/Utils/MathUtils";
import { HaloLauncherSettings } from "../../../Data/GameSettings";
import { PlayerProjectile } from "./PlayerProjectile";
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<PlayerProjectile>;
private projectiles: PlayerProjectile[] = [];
private directions: Vec2[] = [];
private playerNode: Node;
public init(playerNode: Node, settings: HaloLauncherSettings): void {
this.playerNode = playerNode;
this.projectilesToSpawn = settings.projectilesToSpawn;
this.projectilePool = new ObjectPool<PlayerProjectile>(this.projectilePrefab, this.node, this.projectilesToSpawn, "PlayerProjectile");
this.speed = settings.projectileSpeed;
this.defaultCooldown = settings.cooldown;
this.lifetimeTimer = new GameTimer(settings.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 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: PlayerProjectile = this.projectilePool.borrow();
projectile.node.setWorldPosition(this.playerNode.worldPosition);
projectile.node.active = true;
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) {
this.projectilePool.return(projectile);
}
this.projectiles = [];
this.isFiring = false;
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "422b46ec-3e15-437d-97fc-7f44a277c3be",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,14 @@
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('PlayerProjectile')
export class PlayerProjectile extends Component {
start() {
}
update(deltaTime: number) {
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "6a24e600-866b-4c0d-9a39-59bf222c2e50",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -14,22 +14,20 @@ export class Player extends Component {
@property private speed = 0;
@property(BoxCollider2D) private collider: BoxCollider2D;
@property(PlayerUI) private playerUI: PlayerUI;
@property(Weapon) private weapon: Weapon;
private input: IInput;
private weapon: Weapon;
private health: UnitHealth;
private level: UnitLevel;
private regeneration: PlayerRegeneration;
public init(input: IInput, weapon: Weapon, settings: PlayerSettings): void {
public init(input: IInput, settings: PlayerSettings): void {
this.input = input;
this.weapon = weapon;
this.health = new UnitHealth(settings.defaultHP);
this.level = new UnitLevel(settings.requiredXP);
this.regeneration = new PlayerRegeneration(this.health, settings.regenerationDelay);
this.weapon.node.parent = this.node;
this.weapon.node.setPosition(new Vec3());
this.weapon.init(settings.weapon);
this.playerUI.init(this.health);
}