using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerObject : MonoBehaviour { //飞剑生成点 public RectTransform feijianPos; //血量字体生成点 public RectTransform damagePos; //血条长度 public RectTransform imgUserHp; //血量文字 public Text txtUserHp; private PlayerData player; //当前血量 private int hp; private float hpWide = 240f; //玩家是否死亡 public bool isDead = false; //记录上一次攻击的时间 private float frontTime; void OnEnable() { player=GameDataMgr.Instance.player; hp = player.hp; } void Start() { EventCenter.Instance.AddEventListener(E_EventType.E_Player_Wound,Wound); } // Update is called once per frame void Update() { //检测什么时候停下来攻击 if (isDead) { return; } if (GameMgr.Instance.isStopAtk) { //检测和目标点达到移动条件时 就攻击 if (Time.time - frontTime >= player.atkTime) { //记录这次攻击时的时间 frontTime = Time.time; //创建飞剑攻击敌人 GameObject obj = PoolMgr.Instance.GetObj("Object/FeiJian"); obj.transform.SetParent(feijianPos, false); } } } //受伤 public void Wound(MonsterData monster) { if (isDead) { return; } //防御抵消伤害 //减少血量 hp = hp+player.def-monster.atk; //弹出减血量数字 GameObject go = PoolMgr.Instance.GetObj("Object/DamageNum"); go.transform.SetParent(damagePos, false); go.GetComponent().UpdateTxtInfo((monster.atk - player.def).ToString()); if (hp <= 0) { //死亡 isDead = true; UpdateHp(0); //弹出失败框 //组件失活 this.gameObject.SetActive(false); return; } UpdateHp(hp); } private void UpdateHp(int hp) { imgUserHp.sizeDelta = new Vector2(hpWide*hp / player.hp , imgUserHp.sizeDelta.y); txtUserHp.text = hp + "/" + player.hp; } //重置面板上玩家的数据 public void UpdatePanel() { imgUserHp.sizeDelta = new Vector2(hpWide, imgUserHp.sizeDelta.y); txtUserHp.text = player.hp + "/" + player.hp; hp=player.hp; } //对象删除时,清空事件中心的事件 void OnDestroy() { EventCenter.Instance.Claer(E_EventType.E_Player_Wound); } }