BaseWeaponUpgradeSO.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// All weapon upgrades use this class for their type, status, multiplier, and activation.
/// Each new weapon upgrade is meant to override the ActivateUpgrade() method with the desired behavior.
/// 
/// Weapon upgrades are loaded in the battle scene by the PlayerUpgrades script/manager.
/// 
/// The PlayerUpgrades script reads the upgradeStatus which is set to true by the ShipUpgradeManager script
/// when an upgrade is purchased from the shop.
/// 
/// Dan Hassett, 10/8/22
/// </summary>

public enum UpgradeTypes {Damage, WeaponCharge, CriticalHitRate, ShieldCharge, AntiHazard}

public class BaseWeaponUpgradeSO : ScriptableObject
{
    [Tooltip("A multiplier of the weapon's type of upgrade. For example, if you want to cut " +
        "the the value in HALF, set it to 0.5. If you want to DOUBLE it, set it to 2.")]
    public float upgradeMultiplier = 1.0f;
    public bool upgradeStatus = false;

    public virtual void SetUpgradeStatus (bool status)
    {
        upgradeStatus = status;
    }

    public virtual void ActivateUpgrade() { }

}

PlayerUpgrades.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// This class exists to load the upgrades purchased from the shop into the battle scenes
/// Dan Hassett, 10/8/22
/// </summary>
public class PlayerUpgrades : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        LoadUpgrades();
    }

    // This method loads any upgrades which have been purchased from the shop by reading data from a static dictionary
    void LoadUpgrades()
    {
        foreach (KeyValuePair<BaseWeaponUpgradeSO, bool> entry in ShipUpgradeManager.weaponUpgradeDictionary)
        {
            if (entry.Value == true)
            {
                entry.Key.ActivateUpgrade();
                EventBus.Publish(new UpgradeAddedEvent(entry.Key.GetType(), entry.Key, false));
            }
        }
    }
}

ShipUpgradeManager.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// This class handles the various upgrade systems of the ship which are purchased in the shop.
/// The upgrades are loaded in the battle scene by the PlayerUpgrades script/manager
/// 
/// Dan Hassett, 10/1/22
/// </summary>

public class ShipUpgradeManager : MonoBehaviour
{
    [Header("Weapon Upgrade Scriptable Objects")]
    public BaseWeaponUpgradeSO[] upgrades;

    public WeaponRoomSO[] weaponRooms;
    public ShieldRoomSO[] shieldRooms;

    public static Dictionary<BaseWeaponUpgradeSO, bool> weaponUpgradeDictionary = new Dictionary<BaseWeaponUpgradeSO, bool>();
    public static Dictionary<string, WeaponRoomSO[]> weaponRoomSODictionary = new Dictionary<string, WeaponRoomSO[]>();
    public static ShieldRoomSO[] staticShieldRooms;

    // Awake() loads the necessary upgrade and room scriptable objects into the static Dictionaries.
    // The weaponRooms array must be filled with WeaponRoomSO scriptable objects in the inspector in Unity.
    private void Awake()
    {
        for(int a = 0; a < upgrades.Length; a++)
        {
            if (weaponUpgradeDictionary.ContainsKey(upgrades[a]) == false)
            {
                weaponUpgradeDictionary.Add(upgrades[a], false);
            }
        }

        if (weaponRoomSODictionary.ContainsKey("weaponRooms") == false)
        {
            weaponRoomSODictionary.Add("weaponRooms", weaponRooms);
        }     
    }

    // This method should be attached to the "Purchase" button in the Ship Upgrades section of the Shop Scene.
    // When an upgrade is purchased in the Shop scene, this method changes the value to "true"
    // so that the PlayerUpgrade script knows which upgrades to activate in the Battle scenes
    public void UpgradePurchased(UpgradeTypes upgrade)
    {
        switch (upgrade)
        {
            case UpgradeTypes.Damage:
                //Debug.Log("ShipUpgradeManager: DAMAGE UPGRADE UPGRADE purchased");
                upgrades[0].SetUpgradeStatus(true);
                break;

            case UpgradeTypes.WeaponCharge:
                //Debug.Log("ShipUpgradeManager: CHARGE SPEED UPGRADE purchased");
                upgrades[1].SetUpgradeStatus(true);
                break;

            case UpgradeTypes.CriticalHitRate:
                //Debug.Log("ShipUpgradeManager: CRITICAL HIT UPGRADE purchased");
                upgrades[2].SetUpgradeStatus(true);
                break;

            case UpgradeTypes.ShieldCharge:
                upgrades[3].SetUpgradeStatus(true);
                break;

            case UpgradeTypes.AntiHazard:
                upgrades[4].SetUpgradeStatus(true);
                break;

            default:
                Debug.LogError(upgrade + " is an invalid upgrade type");
                break;
        }
    }
}
Scroll to top