Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PLAYER SCRIPT
- public class Player : Character
- {
- public HealthBar healthBarPrefab;
- HealthBar healthBar;
- public void Start()
- {
- hitPoints.value = startingHitPoints;
- healthBar = Instantiate(healthBarPrefab);
- healthBar.character = this;
- }
- void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.gameObject.CompareTag("ItemPickups"))
- {
- Item hitObject = collision.gameObject.GetComponent<Consumable>().item;
- if (hitObject != null)
- {
- bool shouldDisappear = false;
- switch (hitObject.itemType)
- {
- case Item.ItemType.COIN:
- shouldDisappear = true;
- break;
- case Item.ItemType.HEALTH:
- shouldDisappear =
- AdjustHitPoints(hitObject.quantity);
- break;
- default:
- break;
- }
- if (shouldDisappear)
- {
- collision.gameObject.SetActive(false);
- }
- }
- }
- }
- //CHARACTER SCRIPT
- public abstract class Character : MonoBehaviour
- {
- public HitPoints hitPoints;
- public float maxHitPoints;
- public float startingHitPoints;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- }
- }
- //HEALTHBAR SCRIPT
- public class HealthBar : MonoBehaviour
- {
- public HitPoints hitPoints;
- [HideInInspector]
- public Character character;
- public Image meterImage;
- public Text hpText;
- float maxHitPoints;
- // Start is called before the first frame update
- void Start()
- {
- maxHitPoints = character.maxHitPoints;
- }
- // Update is called once per frame
- void Update()
- {
- if (character != null)
- {
- meterImage.fillAmount = hitPoints.value / maxHitPoints;
- hpText.text = "HP:" + (meterImage.fillAmount * 100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement