When I saw the Ammo chapter, I thought to try all of it without looking at the videos.
Here’s my Ammo.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AmmoPickup : MonoBehaviour
{
[SerializeField] private AmmoType ammoType;
[SerializeField] private int ammoAmount;
private Ammo ammo;
private void OnTriggerEnter(Collider other) {
if (other.gameObject.GetComponent<Ammo>() != null) {
ammo = other.gameObject.GetComponent<Ammo>();
switch (ammoType) {
case AmmoType.Bullets:
ammo.IncreaseCurrentAmmo(ammoType, ammoAmount);
Destroy(gameObject);
return;
case AmmoType.Rockets:
ammo.IncreaseCurrentAmmo(ammoType, ammoAmount);
Destroy(gameObject);
return;
case AmmoType.Shells:
ammo.IncreaseCurrentAmmo(ammoType, ammoAmount);
Destroy(gameObject);
return;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ammo : MonoBehaviour
{
[SerializeField] private AmmoSlot[] ammoSlots;
[System.Serializable]
private class AmmoSlot {
public AmmoType ammoType;
public int ammoAmount;
}
public int GetCurrentAmmo(AmmoType ammoType) {
return GetAmmoSlot(ammoType).ammoAmount;
}
public void ReduceCurrentAmmo(AmmoType ammoType) {
GetAmmoSlot(ammoType).ammoAmount--;
}
public void IncreaseCurrentAmmo(AmmoType ammoType, int amount) {
GetAmmoSlot(ammoType).ammoAmount += amount;
}
private AmmoSlot GetAmmoSlot(AmmoType ammoType) {
foreach (AmmoSlot slot in ammoSlots) {
if (slot.ammoType == ammoType) {
return slot;
}
}
return null;
}
}