Advertisement
Guest User

unity-stuff

a guest
Dec 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. //PLAYER SCRIPT
  2. public class Player : Character
  3. {
  4.     public HealthBar healthBarPrefab;
  5.     HealthBar healthBar;
  6.     public void Start()
  7.     {
  8.         hitPoints.value = startingHitPoints;
  9.         healthBar = Instantiate(healthBarPrefab);
  10.         healthBar.character = this;
  11.  
  12.     }
  13.     void OnTriggerEnter2D(Collider2D collision)
  14.     {
  15.         if (collision.gameObject.CompareTag("ItemPickups"))
  16.         {
  17.             Item hitObject = collision.gameObject.GetComponent<Consumable>().item;
  18.             if (hitObject != null)
  19.             {
  20.                 bool shouldDisappear = false;
  21.                 switch (hitObject.itemType)
  22.                 {
  23.                     case Item.ItemType.COIN:
  24.                         shouldDisappear = true;
  25.                         break;
  26.                     case Item.ItemType.HEALTH:
  27.                         shouldDisappear =
  28.                         AdjustHitPoints(hitObject.quantity);
  29.                         break;
  30.                     default:
  31.                         break;
  32.                 }
  33.                 if (shouldDisappear)
  34.                 {
  35.                     collision.gameObject.SetActive(false);
  36.                 }
  37.             }
  38.         }
  39.     }
  40.  
  41.  
  42. //CHARACTER SCRIPT
  43. public abstract class Character : MonoBehaviour
  44. {
  45.     public HitPoints hitPoints;
  46.     public float maxHitPoints;
  47.     public float startingHitPoints;
  48.  
  49.     // Start is called before the first frame update
  50.     void Start()
  51.     {
  52.        
  53.     }
  54.  
  55.     // Update is called once per frame
  56.     void Update()
  57.     {
  58.        
  59.     }
  60. }
  61.  
  62.  
  63. //HEALTHBAR SCRIPT
  64. public class HealthBar : MonoBehaviour
  65. {
  66.     public HitPoints hitPoints;
  67.     [HideInInspector]
  68.     public Character character;
  69.     public Image meterImage;
  70.     public Text hpText;
  71.     float maxHitPoints;
  72.  
  73.     // Start is called before the first frame update
  74.     void Start()
  75.     {
  76.         maxHitPoints = character.maxHitPoints;
  77.     }
  78.  
  79.     // Update is called once per frame
  80.     void Update()
  81.     {
  82.         if (character != null)
  83.         {
  84.             meterImage.fillAmount = hitPoints.value / maxHitPoints;
  85.             hpText.text = "HP:" + (meterImage.fillAmount * 100);
  86.         }
  87.        
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement