Files
2026-11-25 16:51:44 +08:00

66 lines
1.9 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
// 依照 StartUI 選的武器,只啟用對應的那一把槍
// 測試模式下可以用 1~4 鍵切換,方便比較手感
public class WeaponLoadout : MonoBehaviour
{
[Header("武器 (順序對應 1~4 鍵)")]
[Tooltip("每把槍模型上的 HitscanWeapon")]
public HitscanWeapon[] weapons;
public HitscanWeapon Current { get; private set; }
private bool allowSwitch;
void Start()
{
GameSettings settings = GameSettings.Load();
allowSwitch = settings.isTest;
int index = 0;
for (int i = 0; i < weapons.Length; i++)
{
if (weapons[i] != null && weapons[i].data != null && weapons[i].data.weaponName == settings.weapon)
{
index = i;
break;
}
}
Equip(index);
}
void Update()
{
if (!allowSwitch || Keyboard.current == null) return;
if (Keyboard.current.digit1Key.wasPressedThisFrame) Equip(0);
else if (Keyboard.current.digit2Key.wasPressedThisFrame) Equip(1);
else if (Keyboard.current.digit3Key.wasPressedThisFrame) Equip(2);
else if (Keyboard.current.digit4Key.wasPressedThisFrame) Equip(3);
}
// 每回合開始時由 BotSpawner 呼叫 : 所有槍都重設後座力種子 (不看 Current,因為第一回合時可能還沒 Equip)
public void SetRecoilSeed(int seed)
{
if (weapons == null) return;
foreach (HitscanWeapon w in weapons)
{
if (w != null) w.ResetRecoilSeed(seed);
}
}
void Equip(int index)
{
if (index < 0 || index >= weapons.Length || weapons[index] == null) return;
for (int i = 0; i < weapons.Length; i++)
{
if (weapons[i] != null) weapons[i].gameObject.SetActive(i == index);
}
Current = weapons[index];
}
}