initial weapons
This commit is contained in:
1 parent
b87010d2e5
commit
afcd8761e6
66 files changed
+6388
-49
No files matched your search
@@ -0,0 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Code.Gameplay.Weapons;
|
||||
using System;
|
||||
|
||||
namespace Code.Gameplay.Character.Inventory
|
||||
{
|
||||
public class WeaponInventory : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private List<WeaponBase> weapons = new List<WeaponBase>();
|
||||
[SerializeField] private Transform weaponHolder;
|
||||
|
||||
[SerializeField] private int currentWeaponIndex = 0;
|
||||
|
||||
public event Action<WeaponBase> OnWeaponSwitched;
|
||||
public event Action OnWeaponsInitialized;
|
||||
|
||||
public WeaponBase CurrentWeapon => weapons.Count > 0 ? weapons[currentWeaponIndex] : null;
|
||||
public int WeaponCount => weapons.Count;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InitializeWeapons();
|
||||
}
|
||||
|
||||
private void InitializeWeapons()
|
||||
{
|
||||
for (var i = 0; i < weapons.Count; i++)
|
||||
{
|
||||
weapons[i] = Instantiate(weapons[i].gameObject, weaponHolder).GetComponent<WeaponBase>();
|
||||
weapons[i].gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (weapons.Count > 0 && currentWeaponIndex < weapons.Count)
|
||||
{
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(true);
|
||||
OnWeaponSwitched?.Invoke(weapons[currentWeaponIndex]);
|
||||
}
|
||||
|
||||
OnWeaponsInitialized?.Invoke();
|
||||
}
|
||||
|
||||
public void SwitchWeapon(int index)
|
||||
{
|
||||
if (index < 0 || index >= weapons.Count) return;
|
||||
if (index == currentWeaponIndex) return;
|
||||
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(false);
|
||||
currentWeaponIndex = index;
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(true);
|
||||
|
||||
OnWeaponSwitched?.Invoke(weapons[currentWeaponIndex]);
|
||||
}
|
||||
|
||||
public void NextWeapon()
|
||||
{
|
||||
int nextIndex = (currentWeaponIndex + 1) % weapons.Count;
|
||||
SwitchWeapon(nextIndex);
|
||||
}
|
||||
|
||||
public void PreviousWeapon()
|
||||
{
|
||||
int prevIndex = (currentWeaponIndex - 1 + weapons.Count) % weapons.Count;
|
||||
SwitchWeapon(prevIndex);
|
||||
}
|
||||
|
||||
public void RemoveWeapon(WeaponBase weapon)
|
||||
{
|
||||
if (weapons.Contains(weapon))
|
||||
{
|
||||
int index = weapons.IndexOf(weapon);
|
||||
weapons.Remove(weapon);
|
||||
Destroy(weapon.gameObject);
|
||||
|
||||
if (index <= currentWeaponIndex)
|
||||
{
|
||||
currentWeaponIndex = Mathf.Max(0, currentWeaponIndex - 1);
|
||||
}
|
||||
|
||||
if (weapons.Count > 0)
|
||||
{
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(true);
|
||||
OnWeaponSwitched?.Invoke(weapons[currentWeaponIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReloadCurrentWeapon()
|
||||
{
|
||||
CurrentWeapon?.Reload();
|
||||
}
|
||||
|
||||
public void ShootCurrentWeapon()
|
||||
{
|
||||
CurrentWeapon?.StartShooting();
|
||||
}
|
||||
|
||||
public void StopShootingCurrentWeapon()
|
||||
{
|
||||
CurrentWeapon?.StopShooting();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user