using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BNG {
///
/// This is an example of how to spawn ammo depending on the weapon that is equipped in the opposite hand
///
public class AmmoDispenser : MonoBehaviour {
///
/// Used to determine if holding a weapon
///
public Grabber LeftGrabber;
///
/// Used to determine if holding a weapon
///
public Grabber RightGrabber;
///
/// Disable this if weapon not equipped
///
public GameObject AmmoDispenserObject;
///
/// Instantiate this if pistol equipped
///
public GameObject PistolClip;
///
/// Instantiate this if shotgun equipped
///
public GameObject ShotgunShell;
///
/// Instantiate this if shotgun equipped
///
public GameObject RifleClip;
///
/// Amount of Pistol Clips currently available
///
public int CurrentPistolClips = 5;
public int CurrentRifleClips = 5;
public int CurrentShotgunShells = 30;
// Update is called once per frame
void Update() {
bool weaponEquipped = false;
if (grabberHasWeapon(LeftGrabber) || grabberHasWeapon(RightGrabber)) {
weaponEquipped = true;
}
// Only show if we have something equipped
if(AmmoDispenserObject.activeSelf != weaponEquipped) {
AmmoDispenserObject.SetActive(weaponEquipped);
}
}
bool grabberHasWeapon(Grabber g) {
if(g == null || g.HeldGrabbable == null) {
return false;
}
// Holding shotgun, pistol, or rifle
string grabName = g.HeldGrabbable.transform.name;
if (grabName.Contains("Shotgun") || grabName.Contains("Pistol") || grabName.Contains("Rifle")) {
return true;
}
return false;
}
public GameObject GetAmmo() {
bool leftGrabberValid = LeftGrabber != null && LeftGrabber.HeldGrabbable != null;
bool rightGrabberValid = RightGrabber != null && RightGrabber.HeldGrabbable != null;
// Shotgun
if (leftGrabberValid && LeftGrabber.HeldGrabbable.transform.name.Contains("Shotgun") && CurrentShotgunShells > 0) {
CurrentShotgunShells--;
return ShotgunShell;
}
else if (rightGrabberValid && RightGrabber.HeldGrabbable.transform.name.Contains("Shotgun") && CurrentShotgunShells > 0) {
CurrentShotgunShells--;
return ShotgunShell;
}
// Rifle
if (leftGrabberValid && LeftGrabber.HeldGrabbable.transform.name.Contains("Rifle") && CurrentRifleClips > 0) {
CurrentRifleClips--;
return RifleClip;
}
else if (rightGrabberValid && RightGrabber.HeldGrabbable.transform.name.Contains("Rifle") && CurrentRifleClips > 0) {
CurrentRifleClips--;
return RifleClip;
}
// Pistol
if (leftGrabberValid && LeftGrabber.HeldGrabbable.transform.name.Contains("Pistol") && CurrentPistolClips > 0) {
CurrentPistolClips--;
return PistolClip;
}
else if (rightGrabberValid && RightGrabber.HeldGrabbable.transform.name.Contains("Pistol") && CurrentPistolClips > 0) {
CurrentPistolClips--;
return PistolClip;
}
// Default to nothing
return null;
}
public void GrabAmmo(Grabber grabber) {
GameObject ammoClip = GetAmmo();
if(ammoClip != null) {
GameObject ammo = Instantiate(ammoClip, grabber.transform.position, grabber.transform.rotation) as GameObject;
Grabbable g = ammo.GetComponent();
// Disable rings for performance
GrabbableRingHelper grh = ammo.GetComponentInChildren();
if (grh) {
Destroy(grh);
RingHelper r = ammo.GetComponentInChildren();
Destroy(r.gameObject);
}
// Offset to hand
ammo.transform.parent = grabber.transform;
ammo.transform.localPosition = -g.GrabPositionOffset;
ammo.transform.parent = null;
grabber.GrabGrabbable(g);
}
}
public virtual void AddAmmo(string AmmoName) {
if(AmmoName.Contains("Shotgun")) {
CurrentShotgunShells++;
}
else if (AmmoName.Contains("Rifle")) {
CurrentRifleClips--;
}
else if (AmmoName.Contains("Pistol")) {
CurrentPistolClips++;
}
}
}
}