CultivateImmortal/Assets/Scripts/GameObject/MonsterObject.cs

164 lines
4.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class MonsterObject : MonoBehaviour
{
//血条长度
public RectTransform imgDragHp;
//血量文字
public Text txtDragHp;
//怪物名字
public Text txtDragName;
//怪物的图片
public Image imgDrog;
//当前血量
private int hp;
private float hpWide = 240f;
//怪物是否死亡
public bool isDead;
//记录上一次攻击的时间
private float frontTime;
//怪物数据
private MonsterData monster;
// Update is called once per frame
void Update()
{
//检测什么时候停下来攻击
if (isDead)
{
return;
}
//检测是否能开始攻击 就攻击
if (GameMgr.Instance.isStartAtk)
{
if (Time.time - frontTime >= monster.atkTime)
{
//记录这次攻击时的时间
frontTime = Time.time;
//玩家受到伤害
EventCenter.Instance.EventTrigger<MonsterData>(E_EventType.E_Player_Wound, monster);
}
}
}
//受伤
public void Wound(PlayerData player)
{
if (isDead)
{
return;
}
//防御抵消伤害
//减少血量
int damageHP;
//是否触发暴击
if (GameMgr.Instance.ProbaReturn(player.crit))
{
//触发暴击
damageHP = (int)((player.atk - monster.def) * (1 + player.critDamage / 100));
//防止没有破防产生负数的情况
if (damageHP < 0)
{
damageHP = 0;
}
hp -= damageHP;
//弹出减血量数字
EventCenter.Instance.EventTrigger<int>(E_EventType.E_Monster_CritDamageNum, damageHP);
}
else
{
damageHP = player.atk - monster.def;
//防止没有破防产生负数的情况
if (damageHP < 0)
{
damageHP = 0;
}
hp -= damageHP;
//弹出减血量数字
EventCenter.Instance.EventTrigger<int>(E_EventType.E_Monster_DamageNum, damageHP);
}
if (hp <= 0)
{
Dead();
//开启生成新一轮游戏
GameMgr.Instance.StartInit();
this.gameObject.SetActive(false);
return;
}
UpdateHp(hp);
}
//死亡
public void Dead()
{
isDead = true;
//玩家和怪物停止攻击
GameMgr.Instance.isStartAtk = false;
//飞剑回收仓库
GameMgr.Instance.isFeijianClose = true;
//血条长度变为0
UpdateHp(0);
//弹出战斗胜利
EventCenter.Instance.EventTrigger<string>(E_EventType.E_Pool_Register1,"战斗胜利");
//玩家获得奖励
//玩家的总灵石 +=(怪物掉落+怪物掉落*玩家灵石加成)
int getStone = monster.stone + (int)(monster.stone * (GameDataMgr.Instance.player.stoneAdd/100));
GameDataMgr.Instance.player.stone += getStone;
//玩家背包增加源石和其他物品
//弹出奖励框 更新TopPanel的面板数据
GameMgr.Instance.StartTip(getStone,monster.yuan);
//保存玩家数据
GameDataMgr.Instance.PlayerDataSave();
}
private void UpdateHp(int hp)
{
imgDragHp.sizeDelta = new Vector2(hpWide * hp / monster.hp, imgDragHp.sizeDelta.y);
txtDragHp.text = GameMgr.Instance.SetNumber(hp) + "/" + GameMgr.Instance.SetNumber(monster.hp);
}
//重置面板上怪物的数据
public void UpdatePanel()
{
//更新怪物数据
isDead = false;
monster = GameDataMgr.Instance.nowMonster;
hp = monster.hp;
//注册受伤事件
EventCenter.Instance.AddEventListener<PlayerData>(E_EventType.E_Monster_Wound, Wound);
//怪物名字更新
txtDragName.text = monster.name;
//怪物名字颜色更新
txtDragName.color = GameMgr.Instance.HexToColor(monster.color);
//重置血条
imgDragHp.sizeDelta = new Vector2(hpWide, imgDragHp.sizeDelta.y);
txtDragHp.text = GameMgr.Instance.SetNumber(monster.hp) + "/" + GameMgr.Instance.SetNumber(monster.hp);
hp = monster.hp;
imgDrog.color = GameMgr.Instance.HexToColor(monster.color);
//重置怪物面板时 把攻击时间归0
frontTime = Time.time;
}
//失活也要注销事件
void OnDisable()
{
EventCenter.Instance.Claer(E_EventType.E_Monster_Wound);
}
}