120 lines
3.0 KiB
C#
120 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class PlayerObject : MonoBehaviour
|
|
{
|
|
//飞剑生成点
|
|
public RectTransform feijianPos;
|
|
//血条长度
|
|
public RectTransform imgUserHp;
|
|
//血量文字
|
|
public Text txtUserHp;
|
|
|
|
private PlayerData player;
|
|
|
|
//当前血量
|
|
private int hp;
|
|
private float hpWide = 240f;
|
|
//玩家是否死亡
|
|
public bool isDead;
|
|
|
|
//记录上一次攻击的时间
|
|
private float frontTime;
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
//检测什么时候停下来攻击
|
|
if (isDead)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (GameMgr.Instance.isStartAtk)
|
|
{
|
|
//检测和目标点达到移动条件时 就攻击
|
|
if (Time.time - frontTime >= player.atkTime)
|
|
{
|
|
//记录这次攻击时的时间
|
|
frontTime = Time.time;
|
|
//创建飞剑攻击敌人
|
|
GameObject obj = PoolMgr.Instance.GetObj("Object/FeiJian");
|
|
obj.transform.SetParent(feijianPos, false);
|
|
obj.transform.localScale=Vector3.one;
|
|
}
|
|
}
|
|
}
|
|
|
|
//受伤
|
|
public void Wound(MonsterData monster)
|
|
{
|
|
if (isDead)
|
|
{
|
|
return;
|
|
}
|
|
//防御抵消伤害
|
|
//减少血量
|
|
int damageHP = monster.atk - player.def;
|
|
//防止没有破防产生负数的情况
|
|
if (damageHP < 0)
|
|
{
|
|
damageHP = 0;
|
|
}
|
|
hp -= damageHP;
|
|
//弹出减血量数字
|
|
EventCenter.Instance.EventTrigger<int>(E_EventType.E_Player_DamageNum, damageHP);
|
|
|
|
if (hp <= 0)
|
|
{
|
|
//死亡
|
|
isDead = true;
|
|
//飞剑回收仓库
|
|
GameMgr.Instance.isFeijianClose = true;
|
|
|
|
UpdateHp(0);
|
|
//弹出失败框
|
|
EventCenter.Instance.EventTrigger<string>(E_EventType.E_Pool_Register1, "战斗失败");
|
|
|
|
//开启生成新一轮游戏
|
|
GameMgr.Instance.StartInit();
|
|
|
|
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 = GameMgr.Instance.SetNumber(hp) + "/" + GameMgr.Instance.SetNumber(player.hp);
|
|
}
|
|
|
|
//重置面板上玩家的数据
|
|
public void UpdatePanel()
|
|
{
|
|
//是否死亡重置
|
|
isDead = false;
|
|
//数据重置
|
|
player = GameDataMgr.Instance.player;
|
|
hp = player.hp;
|
|
//重新注册玩家是否受伤
|
|
EventCenter.Instance.AddEventListener<MonsterData>(E_EventType.E_Player_Wound, Wound);
|
|
|
|
imgUserHp.sizeDelta = new Vector2(hpWide, imgUserHp.sizeDelta.y);
|
|
txtUserHp.text = GameMgr.Instance.SetNumber(player.hp) + "/" + GameMgr.Instance.SetNumber(player.hp);
|
|
hp=player.hp;
|
|
//重置玩家面板时 把攻击时间归0
|
|
frontTime = Time.time;
|
|
}
|
|
|
|
//失活也要注销事件
|
|
void OnDisable()
|
|
{
|
|
EventCenter.Instance.Claer(E_EventType.E_Player_Wound);
|
|
}
|
|
}
|