Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace JeffreyStephens
- {
- public enum ElementType
- {
- None = 0, Fire, Earth, Air, Water
- }
- }
- namespace JeffreyStephens.Weapons
- {
- /// <summary>
- /// IWeapon is an interface. It requires a class or struct have the following methods and properties.
- /// </summary>
- public interface IWeapon
- {
- string Name { get; set; }
- int Accuracy { get; set; }
- int Damage { get; set; }
- JeffreyStephens.ElementType Element { get; set; }
- GameObject WeaponModel { get; set; }
- }
- /// <summary>
- /// We define Weapon as a POCO ( a Plain Old Class Object ). This way, we're more concerned about the data than the object.
- /// It needs to honour the IWeapon interface.
- /// </summary>
- public class Weapon : IWeapon
- {
- public string Name { get; set; }
- public int Accuracy { get; set; }
- public int Damage { get; set; }
- public JeffreyStephens.ElementType Element { get; set; }
- public GameObject WeaponModel { get; set; }
- }
- public class WeaponManager : MonoBehaviour
- {
- // This dictionary is the local store for the WeaponManager isntance. Kind of like a local database.
- private Dictionary<string, IWeapon> WeaponsList;
- public WeaponManager ( )
- {
- WeaponsList = new Dictionary<string, IWeapon> ( );
- }
- // Add a weapon to our dictionary, by passing a weapon name, and a weapon.
- public bool AddWeapon ( string weaponName, IWeapon weapon )
- {
- if ( WeaponsList.ContainsKey ( weaponName ) ) return false;
- if ( string.IsNullOrEmpty ( weapon.Name ) ) weapon.Name = weaponName;
- WeaponsList.Add ( weaponName, weapon );
- return true;
- }
- // Get a weapon from the dictionary.
- public IWeapon GetWeapon ( string weaponName )
- {
- return WeaponsList [ weaponName ];
- }
- // We may want to remove a weapon from the collection of weapons.
- public bool RemoveWeapon ( string weaponName )
- {
- return WeaponsList.Remove ( weaponName );
- }
- // We can use this later to move through the weapons list. Untested.
- public IEnumerator GetEnumerator ( )
- {
- return WeaponsList.GetEnumerator ( );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment