Advertisement
MysteryGM

UnityStatsSystem_Part2: UseSystem

Dec 28th, 2020 (edited)
1,564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using StatsSystem;
  5. using UnityEngine;
  6.  
  7. //I would normally name this script "StatsKeeper" or "ActorStats" so I can use it on players and enemies
  8. //Player stats is less abstract and easier to learn from
  9. public class PlayerStats : MonoBehaviour
  10. {
  11.     //We will only have two stats for this example
  12.     public StatisticPoint HealthPoints = new StatisticPoint(100);
  13.     public StatisticPoint Strength = new StatisticPoint(12);// 12 STR
  14.     ////Special note: if you want these stats private you will have to make functions to change them
  15.  
  16.     //We use an list of status effects to allow effect stacking as an mechanic
  17.     private List<StatusEffect> StatusEffectList = new List<StatusEffect>();
  18.  
  19.  
  20.     void Start()
  21.     {
  22.         //Now lets add some effects on start
  23.         StatusEffectList.Add(new Bleed());
  24.         StatusEffectList.Add(new Bleed());
  25.         //StatusEffectList.Add(new Weaken()); //Weaken will drastically reduce strength
  26.         //StatusEffectList.Add(new Poison());
  27.         StatusEffectList.Add(new Poison());
  28.  
  29.         //This will start the turn based simulation
  30.         StartCoroutine(EverySecondIsATurn());
  31.     }
  32.  
  33.     //Use a Coroutine to simulate a turn based game
  34.     IEnumerator EverySecondIsATurn()
  35.     {
  36.         ////This is where the magic happens! we run DoEffect here but in your game it would be at the start or end of turn
  37.         for (; ; )
  38.         {
  39.             //Run list backwards so we can remove effects without the need of a cleanup pass
  40.             for (int i = StatusEffectList.Count - 1; i >= 0; i--)
  41.             {
  42.                 //Do the thing
  43.                 StatusEffectList.ElementAt(i).DoEffect(this);
  44.                 //Remove condition, an item will just remove this without checking
  45.                 if (StatusEffectList.ElementAt(i).RemoveEffect() == true)
  46.                 {
  47.                     Debug.Log( string.Format("Effect expired:{0}", StatusEffectList.ElementAt(i)) );
  48.                     StatusEffectList.RemoveAt(i);
  49.                 }
  50.             }
  51.  
  52.             //Now of course you would update the HUD:
  53.             print(string.Format("Player Health is:{0}, and strength is:{1}", HealthPoints.GetValue(), Strength.GetValue()));
  54.             yield return new WaitForSeconds(1f);
  55.         }
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement