Pro_Unit

Weapon_Monkey_Code

Sep 17th, 2021
687
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2.  
  3. using UnityEngine;
  4.  
  5. public enum AmmoType
  6. {
  7.     Pistol,
  8.     AK47
  9. }
  10.  
  11. public class Weapon : MonoBehaviour
  12. {
  13.     private int currentClip;
  14.     private int clipSize;
  15.     private AmmoSlot ammoSlot;
  16.     private AmmoType ammoType;
  17.  
  18.     private void Reload()
  19.     {
  20.         if (Input.GetKeyDown(KeyCode.R) && currentClip < clipSize && ammoSlot.GetCurrentAmmo(ammoType) > 0)
  21.         {
  22.             int missingClip = clipSize - currentClip;
  23.             if (ammoSlot.GetCurrentAmmo(ammoType) >= missingClip)
  24.             {
  25.                 currentClip += missingClip;
  26.                 ammoSlot.ReduceCurrentAmmo(ammoType, missingClip);
  27.             }
  28.             else
  29.             {
  30.                 currentClip += ammoSlot.GetCurrentAmmo(ammoType);
  31.                 ammoSlot.ReduceCurrentAmmo(ammoType, ammoSlot.GetCurrentAmmo(ammoType));
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37. public class AmmoSlot : MonoBehaviour
  38. {
  39.     private Dictionary<AmmoType, int> _ammos =  new Dictionary<AmmoType, int>();
  40.  
  41.     public int GetCurrentAmmo(AmmoType ammoType)
  42.     {
  43.         return _ammos[ammoType];
  44.     }
  45.  
  46.     public void ReduceCurrentAmmo(AmmoType ammoType, int missingClip)
  47.     {
  48.         _ammos[ammoType] -= missingClip;
  49.     }
  50. }
RAW Paste Data