Advertisement
MysteryGM

Unity_ItemExample

Oct 9th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. ////////Item script//////////////////////////////////////
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Item
  7. {
  8.     public string Name = "None";
  9.     public string Description = "Please add description";
  10.  
  11.     //Using an array allows the item to be used on any amount of targers, including 0, 1 and 10000
  12.     GameObject[] Targets;
  13.     //The effect is a abstract class
  14.     public ItemEffect Effect = null;
  15.  
  16.     public Item(string NewName, string NewDescription , ItemEffect NewEffect)
  17.     {
  18.         Name = NewName;
  19.         Description = NewDescription;
  20.         Effect = NewEffect;
  21.     }
  22.  
  23.     public void OnUse()
  24.     {
  25.         Effect.Use(Targets);
  26.     }
  27.  
  28. }
  29.  
  30. ////////Item Effect//////////////////////////////////////
  31.  
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using UnityEngine;
  35.  
  36. public abstract class ItemEffect
  37. {
  38.     public abstract void Use(GameObject[] Targets);
  39. };
  40.  
  41. //The All items class is just to keep things organized
  42. public class AllItemEffects
  43. {
  44.     public class HealingTargets : ItemEffect
  45.     {
  46.         int Healing = 20;
  47.         public HealingTargets(int Amount)
  48.         {
  49.             Healing = Amount;
  50.         }
  51.  
  52.         public override void Use(GameObject[] Targets)
  53.         {
  54.             foreach (GameObject Target in Targets)
  55.             {
  56.                 Target.GetComponent<DudHealthSystem>().ChangeHealth(Healing);
  57.             }
  58.         }
  59.     }
  60.  
  61.     public class DamageTargets : ItemEffect
  62.     {
  63.         int Damage = 20;
  64.         public DamageTargets(int Amount)
  65.         {
  66.             Damage = Amount;
  67.         }
  68.  
  69.         public override void Use(GameObject[] Targets)
  70.         {
  71.             foreach (GameObject Target in Targets)
  72.             {
  73.                 Target.GetComponent<DudHealthSystem>().ChangeHealth(-Damage);
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. ////////Dud Health System//////////////////////////////////////
  80. using System.Collections;
  81. using System.Collections.Generic;
  82. using UnityEngine;
  83.  
  84. public class DudHealthSystem : MonoBehaviour
  85. {
  86.     //Just a placeholder health system
  87.     int MaxHP = 100;
  88.     int HP = 100;
  89.  
  90.     public void ChangeHealth(int Amount)
  91.     {
  92.         HP = Mathf.Clamp(HP + Amount, 0, MaxHP);
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement