MysteryGM

UnityStatsSystem_Part1: MakeSystem

Dec 28th, 2020 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. //Stats allows us to keep track of player data or any data the game needs
  4. namespace StatsSystem //Name spaces are just a way of sorting all these classes you will need for a game
  5. {
  6.     //We want to know that our statistics data is limited to keep order
  7.     public class StatisticPoint
  8.     {
  9.         private int MaxValue;
  10.         private int CurrentValue;
  11.  
  12.         //Constructors for making the stat
  13.         public StatisticPoint(int SizeOfStat)
  14.         {
  15.             MaxValue = SizeOfStat;
  16.             CurrentValue = SizeOfStat;
  17.         }
  18.  
  19.         //Add value safely to the stat, we can use this to subtract also
  20.         public void AddToStat(int Amount)
  21.         {
  22.             CurrentValue += Amount;
  23.             CurrentValue = Mathf.Clamp(CurrentValue, 0, MaxValue);
  24.         }
  25.  
  26.         //Set value safely to the stat, for effects that don't stack, must be positive!
  27.         public void SetToStat(int Amount)
  28.         {
  29.             if (Amount < 0)
  30.             {
  31.                 Debug.LogError("Used a negative number to set a stat for the game");
  32.             }
  33.             else
  34.             {
  35.                 CurrentValue = Mathf.Clamp(Amount, 0, MaxValue);
  36.             }
  37.         }
  38.  
  39.         public int GetValue() { return CurrentValue; }
  40.         public int GetMaxValue() { return MaxValue; }
  41.     }
  42. }
  43.  
  44.  
  45. //For now lets just re-use the name space to split them for readability, again a name space is like a group
  46. //I would usually use two scripts to keep the status effects separate from the stats them self
  47. namespace StatsSystem
  48. {
  49.     //Think as an abstract class as a guide or contract for how classes derived from it should work
  50.     public abstract class StatusEffect //The master class that all status effect are derived from
  51.     {
  52.         //We use the stat class we made before to target the exact stats we need
  53.         public abstract void DoEffect(PlayerStats Target);
  54.         //Every effect must implement the idea of removal (even if a effect that can't be removed must think about it)
  55.         public abstract bool RemoveEffect();
  56.     }
  57.  
  58.  
  59.     //Health loss over time
  60.     public class Bleed : StatusEffect
  61.     {
  62.         int TurnCount = 3;
  63.  
  64.         public override void DoEffect(PlayerStats Target)
  65.         {
  66.             Target.HealthPoints.AddToStat(-1);
  67.             TurnCount -= 1;
  68.         }
  69.         //Will run out
  70.         public override bool RemoveEffect()
  71.         {
  72.             if (TurnCount < 1)
  73.             {
  74.                 return true;
  75.             }
  76.             return false;
  77.         }
  78.     }
  79.  
  80.     //Strength loss over time
  81.     public class Weaken : StatusEffect
  82.     {
  83.         int TurnCount = 1;
  84.  
  85.         public override void DoEffect(PlayerStats Target)
  86.         {
  87.             //subtracts 10% of the stat value
  88.             float Influence = (Target.Strength.GetMaxValue() / 100f) * 10f;
  89.             Target.Strength.AddToStat((int)-Influence); //Will -1 from 12 because 12/100 = 0,12 * 10 = 1,2 to int -> 1
  90.         }
  91.         //Will run out
  92.         public override bool RemoveEffect()
  93.         {
  94.             if (TurnCount < 1)
  95.             {
  96.                 return true;
  97.             }
  98.             return false;
  99.         }
  100.     }
  101.  
  102.     //Health loss over time and strength set to a low value
  103.     public class Poison : StatusEffect
  104.     {
  105.         public override void DoEffect(PlayerStats Target)
  106.         {
  107.             Target.HealthPoints.AddToStat(-1);
  108.             float TenPercent = (Target.Strength.GetMaxValue() / 100f) * 10f;
  109.  
  110.             //Only subtract strength when it is 10% or more of max, to allow other effects to stack and reduce it bellow 10%
  111.             if (Target.Strength.GetValue() > TenPercent)
  112.             {
  113.                 //sets stength to 10% of max, without subtracting more strength
  114.                 Target.Strength.SetToStat((int)TenPercent);
  115.             }
  116.         }
  117.  
  118.         //Needs to be removed by item or the player will loose health till dead
  119.         public override bool RemoveEffect()
  120.         {
  121.             return false;
  122.         }
  123.     }
  124. }
Add Comment
Please, Sign In to add comment