mycj_demo/mycj/Assets/Script/PlayerInfo.cs
2024-12-02 09:37:47 +08:00

64 lines
1.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class PlayerInfo : MonoBehaviour
{
public static PlayerInfo instance;
[Header("显示gold的文本")]
public Text goldText;
[Header("显示hp的文本")]
public Text hpText;
[Header("最大PlayerHp默认为5")]
public float maxPlayerHp=5;
private float playerHp=0;
[Header("PlayerGold默认为0")]
public float playerGold;
public event Action OnGoldReachedThreshold;
// Start is called before the first frame
private void Awake()
{
instance = this;
}
void Start()
{
ChangeHp(maxPlayerHp);
ChangeGold(0);
}
// Update is called once per frame
void Update()
{
}
public void ChangeHp(float number)
{
if ((this.playerHp +number)<=0)
{
Penal.instance.GameOver();
}
this.playerHp += number;
UpdateShow();
}
public void ChangeGold(float number)
{
this.playerGold += number;
if (this.playerGold>=10)
{
OnGoldReachedThreshold?.Invoke();
}
UpdateShow();
}
void UpdateShow()
{
goldText.text = "银币:" + playerGold.ToString();
hpText.text = "玩家Hp" + playerHp.ToString() + "/" + maxPlayerHp.ToString();
}
}