diff --git a/src/ship.ts b/src/ship.ts index b6c1dd7..6696b10 100644 --- a/src/ship.ts +++ b/src/ship.ts @@ -108,6 +108,7 @@ export class Ship { // Initialize weapon system this._weapons = new WeaponSystem(DefaultScene.MainScene); this._weapons.initialize(); + this._weapons.setShipStatus(this._scoreboard.shipStatus); // Initialize input systems this._keyboardInput = new KeyboardInput(DefaultScene.MainScene); diff --git a/src/weaponSystem.ts b/src/weaponSystem.ts index f69ad00..5d44dcd 100644 --- a/src/weaponSystem.ts +++ b/src/weaponSystem.ts @@ -13,6 +13,7 @@ import { Vector3, } from "@babylonjs/core"; import { GameConfig } from "./gameConfig"; +import { ShipStatus } from "./shipStatus"; /** * Handles weapon firing and projectile lifecycle @@ -21,11 +22,19 @@ export class WeaponSystem { private _ammoBaseMesh: AbstractMesh; private _ammoMaterial: StandardMaterial; private _scene: Scene; + private _shipStatus: ShipStatus | null = null; constructor(scene: Scene) { this._scene = scene; } + /** + * Set the ship status instance for ammo tracking + */ + public setShipStatus(shipStatus: ShipStatus): void { + this._shipStatus = shipStatus; + } + /** * Initialize weapon system (create ammo template) */ @@ -57,6 +66,11 @@ export class WeaponSystem { return; } + // Check if we have ammo before firing + if (this._shipStatus && this._shipStatus.ammo <= 0) { + return; + } + // Create projectile instance const ammo = new InstancedMesh("ammo", this._ammoBaseMesh as Mesh); ammo.parent = shipTransform; @@ -82,6 +96,11 @@ export class WeaponSystem { // Set projectile velocity (already includes ship velocity) ammoAggregate.body.setLinearVelocity(velocityVector); + // Consume ammo + if (this._shipStatus) { + this._shipStatus.consumeAmmo(0.01); + } + // Auto-dispose after 2 seconds window.setTimeout(() => { ammoAggregate.dispose();