File size: 1,863 Bytes
565f6e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Pilot, WeaponType } from "../../types";
import { WEAPON_SPECS_MAP } from "../aircraftData";
import { Vector3 } from "three";
import { spawnOrdnance, spawnGunProjectile } from "./ProjectileSpawner";

export class WeaponSystem {
  public handleWeaponFiring(
    pilot: Pilot,
    triggerPrimary: boolean,
    triggerSecondary: boolean,
    dt: number,
    clientSeq: number | undefined,
    inputQueueLength: number,
    playerHistory: any[],
    triggerSplashDamage: (pos: Vector3, ownerId: string, team: number, type: WeaponType) => void,
    projectiles: any[],
    mapId: string,
    broadcast: (type: string, payload: any) => void
  ) {
    if (!pilot.entity.components.has("weaponized")) return;
    const wep = pilot.entity.components.get("weaponized") as any;
    if (!wep.cooldowns) wep.cooldowns = {};

    pilot.specs.weapons.forEach(wType => {
      const spec = WEAPON_SPECS_MAP[wType];
      const ammo = wep.ammo[wType] ?? 0;
      if (ammo <= 0) return;

      const cooldown = wep.cooldowns[wType] ?? 0;
      if (cooldown > 0) return;

      const isSecondary = wType === WeaponType.ROCKET || wType === WeaponType.BOMB;

      if (isSecondary) {
        if (!triggerSecondary) return;
        broadcast("player_fired", { id: pilot.id, weaponType: wType });
        spawnOrdnance(pilot, wType, projectiles);
        wep.ammo[wType]--;
        wep.cooldowns[wType] = 1 / Math.max(0.01, spec.fireRate);
        return;
      }

      if (!triggerPrimary) return;

      const shotChance = spec.fireRate * dt * 0.9;
      if (Math.random() < shotChance) {
        broadcast("player_fired", { id: pilot.id, weaponType: wType });
        spawnGunProjectile(pilot, wType, clientSeq, inputQueueLength, playerHistory, triggerSplashDamage, projectiles, mapId);
        wep.ammo[wType]--;
        wep.cooldowns[wType] = 0.015;
      }
    });
  }
}