UnityCommon/Role/Role.cs
2024-12-04 10:08:59 +08:00

170 lines
4.7 KiB
C#
Raw 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;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using Debug = UnityEngine.Debug;
/// <summary>
/// 角色基类,玩家处理事件
/// </summary>
[ExecuteInEditMode]
public class Role : Fun
{
[Header("血量")] public float hp = 100f;//血量
[Header("掉落")] public float gold = 10f;
public float Hp
{
get => hp;
set
{
hp = value;
if (hp <= 0)
{
die();
}
}
}
[Header("物理护甲")] public int physicalArmor = 10;//物理护甲
[Header("魔法护甲")] public int magicArmor = 5;//魔法护甲
public List<BUff> buffList = new List<BUff>();
[Header("导航组件")] public SimplePathfindingDoTween Navigation;
[System.Serializable ]
public class AnimationList//动画list
{
//[Header("动画是否循环")] public bool isloop = false;
[Header("动画图片")] public List<Sprite> value; // 字典的值
[Header("角色动画帧数间隔(毫秒)")] public int CharacterAnimationFrameInterval = 50;
}
[Header("角色动画")] public List<AnimationList> AnimationTree = new List<AnimationList>();
[Header("角色动画播放")] public int animationHighlight = 0;
[Header("角色精灵位置")] public SpriteRenderer spriteRenderer;
[Header("角色Image位置")] public Image image;
public delegate void AnimationItem(int AnimationItem);
public event AnimationItem OnAnimationStart;
public event AnimationItem OnAnimationIng;
public event AnimationItem OnAnimationEnd;
[Header("动画是否正常播放")] public bool isAnimationPlay = false;//动画是否正常播放
async void Start()
{
if (Application.isPlaying)
{
UpdateBuff();
}
updateAnimation();
}
/// <summary>
/// 角色动画更新
/// </summary>
public async void updateAnimation()
{
while (true)
{
if (AnimationTree.Count == 0)
{
isAnimationPlay = false;
return;
}
isAnimationPlay = true;
if (animationHighlight >= AnimationTree.Count)
{
animationHighlight = AnimationTree.Count - 1;
}
else if (animationHighlight < 0) {
animationHighlight = 0;
}
EditorUtility.SetDirty(this);
List<Sprite> LightSprite = AnimationTree[animationHighlight].value;
if (LightSprite == null )
{
isAnimationPlay = false;
return;
};
OnAnimationStart?.Invoke(animationHighlight);
foreach (Sprite sprite in LightSprite)
{
if (image != null)
{
image.sprite = sprite;
}
else if (spriteRenderer != null)
{
spriteRenderer.sprite = sprite;
}
if (animationHighlight >= 0 && animationHighlight < AnimationTree.Count)
{
OnAnimationIng?.Invoke(animationHighlight);
await Task.Delay(AnimationTree[animationHighlight].CharacterAnimationFrameInterval);
}
}
OnAnimationEnd?.Invoke(animationHighlight);
}
}
/// <summary>
/// 角色死亡
/// </summary>
public virtual void die()
{
if (Application.isPlaying)
{
Destroy(gameObject);
}
}
/// <summary>
/// 单位更新buff
/// </summary>
async void UpdateBuff()//单位buff更新
{
while (true)
{
List<BUff> deleteArr = new List<BUff>();
foreach (BUff buffItem in buffList)
{
if (buffItem.executionInterval <= 0)
{
buffItem.executionInterval = buffItem.executionInterval_max;
buffItem.Funaction.Invoke(buffItem.value);
}
buffItem.executionInterval -= 0.1f;
buffItem.timeLeft -= 0.1f;
if (buffItem.timeLeft <= 0)
{
deleteArr.Add(buffItem);
}
}
foreach (BUff item in deleteArr)
{
buffList.Remove(item);
}
await Task.Delay(100);//buff检测最小时间0.1秒执行一次
}
}
// 如果启用 MonoBehaviour则每个固定帧速率的帧都将调用此函数
private void FixedUpdate()
{
if (!isAnimationPlay)
{
updateAnimation();
}
}
}