using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponManager : MonoBehaviour
{
public static WeaponManager Instance { get; set; }
public List<GameObject> weaponSlots;
public GameObject activeWeaponSlot;
[Header("Ammo")]
public int totalARAmmo = 0;
public int totalPistolAmmo = 0;
[Header("Throwables General")]
public float throwForce = 10f;
public GameObject throwableSpawn;
public float forceMultiplier = 0;
public float forceMultiplierLimit = 2;
[Header("Lethals")]
public int lethalsCount = 0;
public Throwable.ThrowableType equippedLethalType;
public GameObject fragPrefab;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}
private void Start()
{
activeWeaponSlot = weaponSlots[0];
}
private void Update()
{
foreach (GameObject weaponSlot in weaponSlots)
{
if (weaponSlot == activeWeaponSlot)
{
weaponSlot.SetActive(true);
}
else
{
weaponSlot.SetActive(false);
}
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
SwitchActiveSlot(0);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
SwitchActiveSlot(1);
}
if (Input.GetKey(KeyCode.G))
{
forceMultiplier += Time.deltaTime;
if (forceMultiplier > forceMultiplierLimit)
{
forceMultiplier = forceMultiplierLimit;
}
}
if (Input.GetKeyUp(KeyCode.G))
{
if (lethalsCount > 0)
{
ThrowLethal();
}
forceMultiplier = 0;
}
}
public void PickupWeapon(GameObject pickedupWeapon)
{
AddWeaponIntoActiveSlot(pickedupWeapon);
}
private void AddWeaponIntoActiveSlot(GameObject pickedupWeapon)
{
DropCurrentWeapon(pickedupWeapon);
pickedupWeapon.transform.SetParent(activeWeaponSlot.transform, false);
Weapon weapon = pickedupWeapon.GetComponent<Weapon>();
pickedupWeapon.transform.localPosition = new Vector3(weapon.spawnPosition.x, weapon.spawnPosition.y, weapon.spawnPosition.z);
pickedupWeapon.transform.localRotation = Quaternion.Euler(weapon.spawnRotation.x, weapon.spawnRotation.y, weapon.spawnRotation.z);
weapon.isActiveWeapon = true;
weapon.animator.enabled = true;
}
internal void PickupAmmo(AmmoBox ammo)
{
switch (ammo.ammoType)
{
case AmmoBox.AmmoType.Pistol_Ammo:
totalPistolAmmo += ammo.ammoAmount;
break;
case AmmoBox.AmmoType.AssaultRifle_Ammo:
totalARAmmo += ammo.ammoAmount;
break;
}
}
private void DropCurrentWeapon(GameObject pickedupWeapon)
{
if (activeWeaponSlot.transform.childCount > 0)
{
var weaponToDrop = activeWeaponSlot.transform.GetChild(0).gameObject;
weaponToDrop.GetComponent<Weapon>().isActiveWeapon = false;
weaponToDrop.GetComponent<Weapon>().animator.enabled = false;
weaponToDrop.transform.SetParent(pickedupWeapon.transform.parent);
weaponToDrop.transform.localPosition = pickedupWeapon.transform.localPosition;
weaponToDrop.transform.localRotation = pickedupWeapon.transform.localRotation;
}
}
public void SwitchActiveSlot(int slotNumber)
{
if (activeWeaponSlot.transform.childCount > 0)
{
Weapon currentWeapon = activeWeaponSlot.transform.GetChild(0).GetComponent<Weapon>();
currentWeapon.isActiveWeapon = false;
}
activeWeaponSlot = weaponSlots[slotNumber];
if (activeWeaponSlot.transform.childCount > 0)
{
Weapon newWeapon = activeWeaponSlot.transform.GetChild(0).GetComponent<Weapon>();
newWeapon.isActiveWeapon = true;
}
}
//Weapon sprite
internal void DecreaseTotalAmmo(int bulletsToDecrease, Weapon.WeaponModel thisWeaponModel)
{
switch (thisWeaponModel)
{
case Weapon.WeaponModel.SVA_545:
totalARAmmo -= bulletsToDecrease;
break;
case Weapon.WeaponModel.FF_Magnum:
totalPistolAmmo -= bulletsToDecrease;
break;
case Weapon.WeaponModel.Glock:
totalPistolAmmo -= bulletsToDecrease;
break;
}
}
//Ammo sprite
public int CheckAmmoLeftFor(Weapon.WeaponModel thisWeaponModel)
{
switch (thisWeaponModel)
{
case Weapon.WeaponModel.SVA_545:
return totalARAmmo;
case Weapon.WeaponModel.FF_Magnum:
return totalPistolAmmo;
case Weapon.WeaponModel.Glock:
return totalPistolAmmo;
default:
return 0;
}
}
#region || ---- Throwables ---- ||
public void PickupThrowable(Throwable throwable)
{
switch (throwable.throwableType)
{
case Throwable.ThrowableType.Frag:
PickupThrowableAsLethal(Throwable.ThrowableType.Frag);
break;
}
}
private void PickupThrowableAsLethal(Throwable.ThrowableType lethal)
{
if (equippedLethalType == lethal || equippedLethalType == Throwable.ThrowableType.None)
{
equippedLethalType = lethal;
if (lethalsCount < 3)
{
lethalsCount <= 1;
Destroy(InteractionManager.Instance.hoveredThrowable.gameObject);
HUDmanager.Instance.UpdateThrowables();
}
else{
print("Lethals limit reached.");
}
}
else
{
//Cannot pick up differnt lethal
//Option to swap lethal
}
}
}
private void ThrowLethal()
{
GameObject lethalPrefab = GetThrowablePrefab();
GameObject throwable = Instantiate(lethalPrefab, throwableSpawn.transform.position, Camera.main.transform.rotation);
Rigidbody rb = throwable.GetComponent<Rigidbody>();
rb.AddForce(Camera.main.transform.forward * (throwForce * forceMultiplier), ForceMode.Impulse);
throwable.GetComponent<Throwable>().hasBeenThrown = true;
lethalsCount -= 1;
if (lethalsCount <= 0)
{
equippedLethalType = Throwable.ThrowableType.None;
}
HUDmanager.Instance.UpdateThrowables();
}
private GameObject GetThrowablePrefab()
{
switch (equippedLethalType)
{
case Throwable.ThrowableType.Frag:
return fragPrefab;
}
return new();
}
#endregion
}
Not sure what is happening.
I have 3 errors.
Assets\Scripts\WeaponManager.cs(233,5): error CS8803: Top-level statements must precede namespace and type declarations.
Assets\Scripts\WeaponManager.cs(233,5): error CS0106: The modifier ‘private’ is not valid for this item
Assets\Scripts\WeaponManager.cs(253,5): error CS0106: The modifier ‘private’ is not valid for this item
Assets\Scripts\WeaponManager.cs(264,1): error CS1022: Type or namespace definition, or end-of-file expected