using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MonsterObject : MonoBehaviour { //血量字体生成点 public RectTransform damagePos; //血条长度 public RectTransform imgDragHp; //血量文字 public Text txtDragHp; //当前血量 private int hp; private float hpWide = 240f; //怪物是否死亡 public bool isDead = false; //记录上一次攻击的时间 private float frontTime; //怪物数据 private MonsterData monster; void Awake() { Time.timeScale = 1f; monster=GameDataMgr.Instance.monster; hp = monster.hp; } // Start is called before the first frame update void OnEnable() { EventCenter.Instance.AddEventListener(E_EventType.E_Monster_Wound,Wound); } // Update is called once per frame void Update() { //检测什么时候停下来攻击 if (isDead) { return; } //检测和目标点达到移动条件时 就攻击 if (Time.time - frontTime >= monster.atkTime) { //记录这次攻击时的时间 frontTime = Time.time; //玩家受到伤害 EventCenter.Instance.EventTrigger(E_EventType.E_Player_Wound, monster); } } //受伤 public void Wound(PlayerData player) { if (isDead) { return; } //防御抵消伤害 //减少血量 hp = hp + monster.def - player.atk; //弹出减血量数字 GameObject go = PoolMgr.Instance.GetObj("Object/DamageNum"); go.transform.SetParent(damagePos, false); go.GetComponent().UpdateTxtInfo((player.atk-monster.def).ToString()); if (hp <= 0) { Dead(); //对象失活 this.gameObject.SetActive(false); return; } UpdateHp(hp); } //死亡 public void Dead() { isDead = true; GameMgr.Instance.isStopAtk = false; //血条长度变为0 UpdateHp(0); //弹出战斗胜利 EventCenter.Instance.EventTrigger(E_EventType.E_Pool_Register1,"战斗胜利"); //玩家获得奖励 GameDataMgr.Instance.player.stone += 9999; } private void UpdateHp(int hp) { imgDragHp.sizeDelta = new Vector2(hpWide * hp / monster.hp, imgDragHp.sizeDelta.y); txtDragHp.text = hp + "/" + monster.hp; } //重置面板上玩家的数据 public void UpdatePanel() { imgDragHp.sizeDelta = new Vector2(hpWide, imgDragHp.sizeDelta.y); txtDragHp.text = monster.hp + "/" + monster.hp; hp = monster.hp; } void OnDestroy() { EventCenter.Instance.Claer(E_EventType.E_Monster_Wound); } }