EgonMilanVotrubec

WeaponManager

Mar 27th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace JeffreyStephens
  6. {
  7.     public enum ElementType
  8.     {
  9.         None = 0, Fire, Earth, Air, Water
  10.     }
  11. }
  12.  
  13.  
  14. namespace JeffreyStephens.Weapons
  15. {
  16.     /// <summary>
  17.     /// IWeapon is an interface. It requires a class or struct have the following methods and properties.
  18.     /// </summary>
  19.     public interface IWeapon
  20.     {
  21.         string Name { get; set; }
  22.         int Accuracy { get; set; }
  23.         int Damage { get; set; }
  24.         JeffreyStephens.ElementType Element { get; set; }
  25.         GameObject WeaponModel { get; set; }
  26.     }
  27.  
  28.     /// <summary>
  29.     /// We define Weapon as a POCO ( a Plain Old Class Object ). This way, we're more concerned about the data than the object.
  30.     /// It needs to honour the IWeapon interface.
  31.     /// </summary>
  32.     public class Weapon : IWeapon
  33.     {
  34.         public string Name { get; set; }
  35.         public int Accuracy { get; set; }
  36.         public int Damage { get; set; }
  37.         public JeffreyStephens.ElementType Element { get; set; }
  38.         public GameObject WeaponModel { get; set; }
  39.     }
  40.  
  41.     public class WeaponManager : MonoBehaviour
  42.     {
  43.         // This dictionary is the local store for the WeaponManager isntance. Kind of like a local database.
  44.         private Dictionary<string, IWeapon> WeaponsList;
  45.  
  46.         public WeaponManager ( )
  47.         {
  48.             WeaponsList = new Dictionary<string, IWeapon> ( );
  49.         }
  50.  
  51.         // Add a weapon to our dictionary, by passing a weapon name, and a weapon.
  52.         public bool AddWeapon ( string weaponName, IWeapon weapon )
  53.         {
  54.             if ( WeaponsList.ContainsKey ( weaponName ) ) return false;
  55.             if ( string.IsNullOrEmpty ( weapon.Name ) ) weapon.Name = weaponName;
  56.             WeaponsList.Add ( weaponName, weapon );
  57.             return true;
  58.         }
  59.  
  60.         // Get a weapon from the dictionary.
  61.         public IWeapon GetWeapon ( string weaponName )
  62.         {
  63.             return WeaponsList [ weaponName ];
  64.         }
  65.  
  66.         // We may want to remove a weapon from the collection of weapons.
  67.         public bool RemoveWeapon ( string weaponName )
  68.         {
  69.             return WeaponsList.Remove ( weaponName );
  70.         }
  71.  
  72.         // We can use this later to move through the weapons list. Untested.
  73.         public IEnumerator GetEnumerator ( )
  74.         {
  75.             return WeaponsList.GetEnumerator ( );
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment