gandalfbialy

Untitled

Jul 19th, 2025
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using UnityEngine;
  2. public abstract class Powerup : ScriptableObject
  3. {
  4.     public bool isActive;
  5.     [SerializeField]
  6.     protected int[] UpgradeCosts;
  7.     [SerializeField]
  8.     protected int currentLevel = 1;
  9.     [SerializeField]
  10.     protected int maxLevel = 3;
  11.     [SerializeField]
  12.     protected PowerupStats duration;
  13.     public float GetDuration() { return duration.GetValue(currentLevel); }
  14.     private void Awake()
  15.     {
  16.         LoadPowerupLevel();
  17.     }
  18.     private void OnValidate()
  19.     {
  20.         currentLevel = Mathf.Min(currentLevel, maxLevel);
  21.         currentLevel = Mathf.Max(currentLevel, 1);
  22.     }
  23.     public bool IsMaxedOut()
  24.     {
  25.         return currentLevel == maxLevel;
  26.     }
  27.     public int GetNextUpgradeCost()
  28.     {
  29.         if (!IsMaxedOut())
  30.         {
  31.             return UpgradeCosts[currentLevel - 1];
  32.         }
  33.         else
  34.         {
  35.             return -1;
  36.         }
  37.     }
  38.     public void Upgrade()
  39.     {
  40.         if (IsMaxedOut()) return;
  41.         currentLevel++;
  42.         SavePowerupLevel();
  43.     }
  44.     // potrzeba zapisać oraz wczytać do/z PlayerPrefs poziom powerupa, aby po zbudowaniu był zapamiętywany obecny poziom powerupa
  45. private void SavePowerupLevel()
  46.     {
  47.         string key = name + "Level";
  48.         PlayerPrefs.SetInt(key, currentLevel);
  49.     }
  50.     private void LoadPowerupLevel()
  51.     {
  52.         string key = name + "Level";
  53.         if (PlayerPrefs.HasKey(key))
  54.         {
  55.             currentLevel = PlayerPrefs.GetInt(key);
  56.         }
  57.     }
  58.     public override string ToString()
  59.     {
  60.         string text = $"{name}\nLVL. {currentLevel}";
  61.         if (IsMaxedOut())
  62.             text += " (MAX)";
  63.         return text;
  64.     }
  65.     public string UpgradeCostString()
  66.     {
  67.         if (!IsMaxedOut())
  68.             return $"UPGRADE\nCOST: {GetNextUpgradeCost()}";
  69.         else
  70.             return "MAXED OUT";
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment