UnityCommon/Role/Role.cs

763 lines
24 KiB
C#
Raw Normal View History

2024-12-29 17:29:36 +08:00
using System;
2024-12-04 05:50:39 +08:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
2024-12-16 17:48:50 +08:00
using DG.Tweening;
2024-12-29 17:29:36 +08:00
using TMPro;
2025-01-09 13:07:52 +08:00
using Debug = UnityEngine.Debug;
2025-01-09 13:30:13 +08:00
using UnityEngine.EventSystems;
2024-12-28 10:27:09 +08:00
2024-12-05 16:25:46 +08:00
public enum Camp
{/// <summary>
2024-12-29 17:29:36 +08:00
/// 玩家
2024-12-05 16:25:46 +08:00
/// </summary>
Player,
/// <summary>
2024-12-29 17:29:36 +08:00
/// 怪物
2024-12-05 16:25:46 +08:00
/// </summary>
Enemy,
/// <summary>
2024-12-29 17:29:36 +08:00
/// 中立
2024-12-05 16:25:46 +08:00
/// </summary>
Neutral
}
2024-12-04 05:50:39 +08:00
2024-12-25 16:33:55 +08:00
public enum CharacterFlags
{
2024-12-25 21:41:45 +08:00
no=0<<0,
2024-12-25 16:33:55 +08:00
fly = 1 << 0,
land = 1 << 1,
big = 1 << 2,
min = 1 << 3,
2024-12-29 17:29:36 +08:00
2025-01-03 18:02:54 +08:00
FlyBig =fly|big,
2024-12-25 16:47:54 +08:00
FlyMin=fly|min,
LandBig = land | big,
LandMin = land | min,
2024-12-25 16:33:55 +08:00
}
2024-12-04 05:50:39 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 角色基类,玩家处理事件
2024-12-04 05:50:39 +08:00
/// </summary>
[ExecuteInEditMode]
public class Role : Fun
{
2024-12-23 16:57:40 +08:00
[Header("Id")] public string id;
2024-12-29 17:29:36 +08:00
[Header("名称")] public string Name;
[Header("阵营")] public Camp camp ;
[Header("血量")] public float hp = 100f;//血量
2024-12-30 12:57:12 +08:00
public float maxHp;
2024-12-29 17:29:36 +08:00
[Header(("死亡动画编号"))] public int dieIndex=-1;
[Header("hp填充直接扣")] public Image Hpfiil;
[Header("hp填充慢慢扣")] public Image HpfiilYello;
[Header("扣血填充")] public GameObject HpTextPrefab;
[Header("自己的画布")]public Canvas _Canvas;
[Header("自己的图片")] public SpriteRenderer spriteRenderers; // 存储所有的SpriteRenderer组件
[Header("被打的方向")] public int HurtDirectin;
public string Quality;//品质
public string Elements;//属性
public string Info;//详情
public string SkillId;//技能
public string AttackType;//攻击类型
public string AttackActionType;//攻击作用类型
public float AttackRange;//攻击范围
2024-12-26 18:11:09 +08:00
2024-12-29 17:29:36 +08:00
public float AttackCD;//攻击CD
public float CritRate;//暴击率
public float CriticalHitRateBonus;//暴击加成
[Header("击杀数量")] public int killNum;
2025-01-04 20:08:40 +08:00
[Header("是不是魔法伤害")] public bool isMoFa=false;
2024-12-04 05:50:39 +08:00
public float Hp
{
get => hp;
set
{
2024-12-27 22:58:44 +08:00
2024-12-14 21:15:33 +08:00
float temp = hp;
2024-12-27 22:58:44 +08:00
2024-12-04 05:50:39 +08:00
hp = value;
2024-12-14 21:15:33 +08:00
2024-12-27 22:58:44 +08:00
2024-12-29 17:29:36 +08:00
// 更新血条显示
2024-12-18 16:55:00 +08:00
if (Hpfiil != null)
2024-12-14 21:15:33 +08:00
{
Hpfiil.fillAmount = hp / maxHp;
2025-01-02 16:35:49 +08:00
2024-12-18 16:55:00 +08:00
2024-12-29 17:29:36 +08:00
// 使用 DOTween 动画平滑过渡血条填充
DOTween.To(() => HpfiilYello.fillAmount, x => HpfiilYello.fillAmount = x, hp / maxHp, 0.8f) // 0.5f 为过渡时间
.SetEase(Ease.InOutQuad); // 使用缓动效果,使血条变化更加平滑
2025-01-02 18:04:35 +08:00
if (UIContorl.instance.NowShowInfo != null && UIContorl.instance.NowShowInfo.Id == 1 && this == UIContorl.instance.NowShowInfo.role)
{
UIContorl.instance.NowShowInfo.UpDateShow("1", "Enemy_001", "0", "3", "1", "测试文档一", hp, maxHp);
}
2024-12-14 21:15:33 +08:00
}
2024-12-29 17:29:36 +08:00
// 更新血量文本效果
2024-12-18 16:55:00 +08:00
if (HpTextPrefab != null && hp < maxHp)
2024-12-14 21:15:33 +08:00
{
2024-12-19 19:01:39 +08:00
hit();
2024-12-29 17:29:36 +08:00
Navigation.StopPathDoTween(0.2f); // 停止任何进行中的 DoTween 动画0.2s为示例值
2024-12-14 21:15:33 +08:00
GameObject go = GameObject.Instantiate(HpTextPrefab, _Canvas.transform);
2024-12-18 20:43:54 +08:00
go.GetComponent<SnowHpControl>().direction = HurtDirectin;
2024-12-29 17:29:36 +08:00
if (isGoodDamege)
{
go.GetComponent<SnowHpControl>().CreateText(true);
isGoodDamege = false;
}
else
{
go.GetComponent<SnowHpControl>().CreateText();
}
2025-01-04 20:08:40 +08:00
2025-01-03 20:50:25 +08:00
if (temp - hp>0)
{
2025-01-04 20:08:40 +08:00
if (isMoFa)
{
go.GetComponent<Text>().text = "<color=blue>" + (temp - hp).ToString("F0") + "</color>"; // 显示伤害值
isMoFa = false;
}
else
{
go.GetComponent<Text>().text = (temp - hp).ToString("F0"); // 显示伤害值
}
2025-01-03 20:50:25 +08:00
}
2024-12-29 17:29:36 +08:00
2024-12-14 21:15:33 +08:00
}
2024-12-29 17:29:36 +08:00
// 判断角色是否死亡
2024-12-04 05:50:39 +08:00
if (hp <= 0)
{
2024-12-29 17:29:36 +08:00
die(); // 执行死亡处理
2024-12-04 05:50:39 +08:00
}
2024-12-18 16:55:00 +08:00
2024-12-04 05:50:39 +08:00
}
}
2024-12-29 17:29:36 +08:00
[Header("掉落")] public int gold = 6;
2024-12-28 10:27:09 +08:00
public int Gold
2024-12-05 16:25:46 +08:00
{
[DebuggerStepThrough]
get => gold;
[DebuggerStepThrough]
set => gold = value;
}
2024-12-29 17:29:36 +08:00
[Header("攻击力")] private float attack = 10f;
[Header("攻击力上限")] public float MaxAttack = 10f;
[Header("攻击力下限")] public float MinAttack = 10f;
public bool isGoodDamege=false;//是否暴击
2024-12-05 16:25:46 +08:00
public float Attack
{
get => attack;
set => attack = value;
}
2024-12-29 17:29:36 +08:00
[Header("等级")] private int level = 1;
2024-12-05 16:25:46 +08:00
public int Level
{
[DebuggerStepThrough] get => level;
[DebuggerStepThrough] set => level = value;
}
2024-12-29 17:29:36 +08:00
[Header("物理护甲")] public int physicalArmor = 10;//物理护甲
[Header("魔法护甲")] public int magicArmor = 5;//魔法护甲
2024-12-23 22:31:00 +08:00
2024-12-29 17:29:36 +08:00
[Header("导航组件")] public SimplePathfindingDoTween Navigation;
2024-12-04 05:50:39 +08:00
[System.Serializable ]
2024-12-29 17:29:36 +08:00
public class AnimationList//动画list
2024-12-04 05:50:39 +08:00
{
2024-12-29 17:29:36 +08:00
//[Header("动画是否循环")] public bool isloop = false;
[Header("动画图片")] public List<Sprite> value; // 字典的值
[Header("角色动画帧数间隔(毫秒)")] public int CharacterAnimationFrameInterval = 50;
2024-12-04 05:50:39 +08:00
}
2024-12-29 17:29:36 +08:00
[Header("角色动画")] public List<AnimationList> AnimationTree = new List<AnimationList>();
[Header("角色动画播放")] public int animationHighlight = 0;
[Header("角色精灵位置")] public SpriteRenderer spriteRenderer;
[Header("角色Image位置")] public Image image;
2024-12-04 05:50:39 +08:00
public delegate void AnimationItem(int AnimationItem);
public event AnimationItem OnAnimationStart;
public event AnimationItem OnAnimationIng;
public event AnimationItem OnAnimationEnd;
2024-12-29 17:29:36 +08:00
[Header("动画是否正常播放")] public bool isAnimationPlay = false;//动画是否正常播放
[Header("碰撞体")]public Collider2D mycollider;
[Header("攻击脚本")] public Attack attackClass;
[Header("当前播放图片序列")] public int CurrentIndex;
2024-12-14 21:15:33 +08:00
2024-12-29 17:29:36 +08:00
public int hitIndex = 2;//受击动画索引
public int normalIndex = 0;//移动动画索引
2024-12-19 19:01:39 +08:00
public bool isHit=false;//
2024-12-14 21:15:33 +08:00
2024-12-23 22:31:00 +08:00
public List<BUff> buffList = new List<BUff>();
2024-12-25 21:41:45 +08:00
public CharacterFlags myTags = 0;
2024-12-25 16:33:55 +08:00
public bool hasfalg(CharacterFlags flag)
{
2024-12-25 21:41:45 +08:00
return (myTags & flag) == flag;
2024-12-25 16:33:55 +08:00
}
2024-12-25 21:41:45 +08:00
public SkillUp mySkillUp;
2024-12-23 22:31:00 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 储存buff
2024-12-23 22:31:00 +08:00
/// </summary>
public List<Action<Role>> storageBuff = new List<Action<Role>>();
/// <summary>
2024-12-29 17:29:36 +08:00
/// 使用buff
2024-12-23 22:31:00 +08:00
/// </summary>
public List<Action<Role>> PlayerBuff=new List<Action<Role>>();
2024-12-27 22:58:44 +08:00
public bool IsDead = false;
public int HaveDieTime = 0;
2024-12-14 21:15:33 +08:00
public virtual async void Start()
2024-12-04 05:50:39 +08:00
{
2025-01-08 21:03:19 +08:00
if (mySkillUp == null)
{
mySkillUp = this.transform.GetComponent<SkillUp>();
}
if (mySkillUp == null)
{
UnityEngine.Debug.LogError(this.name+"mySkillUp is no");
}
2024-12-14 21:15:33 +08:00
maxHp = hp;
2024-12-23 16:57:40 +08:00
SetSelfInfo();
2024-12-29 17:29:36 +08:00
//SetAttackRange();//设置攻击范围
2024-12-04 05:50:39 +08:00
if (Application.isPlaying)
{
2024-12-05 16:25:46 +08:00
if (attackClass)
{
attackClass.role = this;
}
2024-12-04 05:50:39 +08:00
UpdateBuff();
}
2024-12-05 16:25:46 +08:00
tag = Enum.GetName(typeof(Camp), camp);
2024-12-04 05:50:39 +08:00
updateAnimation();
2024-12-26 18:11:09 +08:00
2024-12-23 22:31:00 +08:00
}
/// <summary>
2024-12-29 17:29:36 +08:00
/// 添加Buff
2024-12-23 22:31:00 +08:00
/// </summary>
/// <param name="buffAction"></param>
public void AddBuff(List<Action<Role>> buffs, Action<Role> buffAction)
{
buffs.Add(buffAction);
2024-12-29 17:29:36 +08:00
UnityEngine.Debug.LogError(this.name + "添加buff到"+ buffs);
2024-12-23 22:31:00 +08:00
}
2025-01-07 16:50:42 +08:00
public void RemoveBuff(List<Action<Role>> buffs, Action<Role> buffAction)
{
buffs.Remove(buffAction);
UnityEngine.Debug.LogError(this.name + "移除" + buffs);
}
2024-12-23 22:31:00 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 执行所有当前 Buff
2024-12-23 22:31:00 +08:00
/// </summary>
public void ApplyBuffs()
{
foreach (var buff in PlayerBuff)
{
2024-12-29 17:29:36 +08:00
UnityEngine.Debug.LogError(this.name + "进入执行buff判断");
buff.Invoke(this); // 将自己作为目标传递
2024-12-23 22:31:00 +08:00
}
2024-12-29 17:29:36 +08:00
// 清空 Buff 列表,假设 Buff 是一次性的
2024-12-25 11:22:39 +08:00
PlayerBuff.Clear();
2024-12-04 05:50:39 +08:00
}
2024-12-23 22:31:00 +08:00
2024-12-04 05:50:39 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 角色动画更新
2024-12-04 05:50:39 +08:00
/// </summary>
public async void updateAnimation()
{
while (true)
{
2025-01-09 13:07:52 +08:00
if (hp < 0)
{
Debug.LogError("!!!!!!!!!!!跳过while");
return;
}
2024-12-04 06:08:11 +08:00
if (AnimationTree.Count == 0)
{
isAnimationPlay = false;
return;
}
isAnimationPlay = true;
if (animationHighlight >= AnimationTree.Count)
2024-12-04 05:50:39 +08:00
{
animationHighlight = AnimationTree.Count - 1;
}
2024-12-19 19:01:39 +08:00
else if (animationHighlight < 0)
{
2024-12-04 05:50:39 +08:00
animationHighlight = 0;
}
List<Sprite> LightSprite = AnimationTree[animationHighlight].value;
2024-12-19 19:01:39 +08:00
if (LightSprite == null)
2024-12-04 05:50:39 +08:00
{
2024-12-04 06:08:11 +08:00
isAnimationPlay = false;
2024-12-04 05:50:39 +08:00
return;
};
OnAnimationStart?.Invoke(animationHighlight);
2024-12-14 21:15:33 +08:00
var lsanimationHighlight = animationHighlight;
2024-12-04 05:50:39 +08:00
foreach (Sprite sprite in LightSprite)
{
2024-12-14 21:15:33 +08:00
if (lsanimationHighlight == animationHighlight)
2024-12-04 05:50:39 +08:00
{
2024-12-14 21:15:33 +08:00
if (image != null)
{
image.sprite = sprite;
}
else if (spriteRenderer != null)
{
spriteRenderer.sprite = sprite;
}
if (animationHighlight >= 0 && animationHighlight < AnimationTree.Count)
{
2025-01-09 13:07:52 +08:00
if (hp < 0)
{
Debug.LogError("@@@@@@@@@@@@跳过while");
return;
}
2024-12-14 21:15:33 +08:00
OnAnimationIng?.Invoke(animationHighlight);
await Task.Delay(AnimationTree[animationHighlight].CharacterAnimationFrameInterval);
}
2024-12-04 05:50:39 +08:00
}
2024-12-14 21:15:33 +08:00
else
2024-12-04 05:50:39 +08:00
{
2024-12-14 21:15:33 +08:00
break;
2024-12-04 05:50:39 +08:00
}
}
2024-12-14 21:15:33 +08:00
OnAnimationEnd?.Invoke(lsanimationHighlight);
if (lsanimationHighlight == dieIndex)
{
2025-01-06 16:52:14 +08:00
//SpawnPool.intance.DeadNumber += 1;
2025-01-09 10:35:40 +08:00
2025-01-06 16:52:14 +08:00
2025-01-09 10:35:40 +08:00
2025-01-06 14:51:48 +08:00
SpawnMonster.intance.enemysList.Remove(this.gameObject);
2025-01-09 10:35:40 +08:00
2025-01-06 14:51:48 +08:00
if (SpawnMonster.intance.enemysList.Count<=0)
{
SpawnMonster.intance.Index += 1;
}
2025-01-06 16:52:14 +08:00
2025-01-09 13:07:52 +08:00
Destroy(this.gameObject);
//Destroy(this);
2024-12-19 19:01:39 +08:00
}
if (lsanimationHighlight == hitIndex)
{
animationHighlight = normalIndex;
2024-12-14 21:15:33 +08:00
}
2024-12-04 05:50:39 +08:00
}
}
2024-12-05 16:25:46 +08:00
2024-12-19 19:01:39 +08:00
2024-12-04 05:50:39 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 角色死亡
2024-12-04 05:50:39 +08:00
/// </summary>
2024-12-19 19:01:39 +08:00
public virtual void die()
2024-12-04 05:50:39 +08:00
{
2024-12-27 22:58:44 +08:00
2024-12-04 05:50:39 +08:00
if (Application.isPlaying)
{
2025-01-02 18:04:35 +08:00
GetComponent<CharacterClick>().HideInfoPanel();
2024-12-14 21:15:33 +08:00
animationHighlight = dieIndex;
Navigation.PauseAnimation();
2024-12-27 22:58:44 +08:00
if (!IsDead)
{
UIContorl.instance.Killnumber++;
2024-12-28 10:27:09 +08:00
SkillBox.instance.AddExperience(gold, this.gameObject.transform);
2024-12-27 22:58:44 +08:00
IsDead = true;
}
HaveDieTime++;
2024-12-04 05:50:39 +08:00
}
}
2024-12-19 19:01:39 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 角色受到攻击
2024-12-19 19:01:39 +08:00
/// </summary>
public virtual void hit()
{
if (Application.isPlaying)
{
animationHighlight = hitIndex;
}
}
2024-12-20 21:41:40 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 角色减速
2024-12-20 21:41:40 +08:00
/// </summary>
public void SlowDown(float num)
{
Navigation.speedFactor *= num;
}
2024-12-04 05:50:39 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 单位更新buff
2024-12-04 05:50:39 +08:00
/// </summary>
2024-12-29 17:29:36 +08:00
async void UpdateBuff()//单位buff更新
2024-12-04 05:50:39 +08:00
{
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);
}
2024-12-29 17:29:36 +08:00
await Task.Delay(100);//buff检测最小时间0.1秒执行一次
2024-12-04 05:50:39 +08:00
}
}
2024-12-14 21:15:33 +08:00
/// <summary>
2024-12-29 17:29:36 +08:00
/// 重置角色所有状态
2024-12-14 21:15:33 +08:00
/// </summary>
public void ResetAllStatus()
{
2024-12-27 22:58:44 +08:00
IsDead = false;
2024-12-29 17:29:36 +08:00
// 重置血量
2024-12-14 21:15:33 +08:00
Hp = 100f;
2024-12-29 17:29:36 +08:00
// 重置攻击力
2024-12-14 21:15:33 +08:00
Attack = 10f;
2024-12-29 17:29:36 +08:00
// 重置护甲
2024-12-14 21:15:33 +08:00
physicalArmor = 10;
magicArmor = 5;
2024-12-29 17:29:36 +08:00
// 重置等级
2024-12-14 21:15:33 +08:00
Level = 1;
2024-12-29 17:29:36 +08:00
// 重置金币
2024-12-28 10:27:09 +08:00
Gold = 10;
2024-12-14 21:15:33 +08:00
2024-12-29 17:29:36 +08:00
// 清空 Buff 列表
2024-12-14 21:15:33 +08:00
buffList.Clear();
2024-12-29 17:29:36 +08:00
// 如果有导航组件,停止当前的导航行为
2024-12-14 21:15:33 +08:00
if (Navigation != null)
{
2024-12-29 17:29:36 +08:00
//Navigation.Stop(); // 假设你有 Stop 方法来停止导航
2024-12-14 21:15:33 +08:00
}
2024-12-29 17:29:36 +08:00
// 重置角色动画
2024-12-14 21:15:33 +08:00
animationHighlight = 0;
isAnimationPlay = false;
2024-12-29 17:29:36 +08:00
// 重置碰撞体(根据需求是否需要调整)
2024-12-14 21:15:33 +08:00
if (mycollider != null)
{
2024-12-29 17:29:36 +08:00
mycollider.enabled = true; // 启用碰撞体
2024-12-14 21:15:33 +08:00
}
2024-12-29 17:29:36 +08:00
// 如果有其他需要重置的状态,可以在这里添加
2024-12-14 21:15:33 +08:00
}
2024-12-16 17:48:50 +08:00
2025-01-07 16:50:42 +08:00
public void FlashRedEffect(int color = 0, float time = 0.2f)//0、1、2 红、蓝、紫
2024-12-16 17:48:50 +08:00
{
2024-12-29 17:29:36 +08:00
// 如果spriteRenderer存在
2024-12-24 14:56:21 +08:00
if (spriteRenderer != null)
{
2024-12-29 17:29:36 +08:00
// 创建一个Sequence来确保动画按顺序执行
2024-12-24 14:56:21 +08:00
Sequence sequence = DOTween.Sequence();
2024-12-29 17:29:36 +08:00
// 颜色变化
2024-12-24 14:56:21 +08:00
if (color == 0)
2024-12-16 17:48:50 +08:00
{
2024-12-29 17:29:36 +08:00
// 变为红色并恢复白色
2024-12-24 14:56:21 +08:00
sequence.Append(spriteRenderer.DOColor(UnityEngine.Color.red, 0.2f))
.Append(spriteRenderer.DOColor(UnityEngine.Color.white, time));
}
else if (color == 1)
{
2024-12-29 17:29:36 +08:00
// 更冷的深蓝色
UnityEngine.Color frostBlueWithAlpha = new UnityEngine.Color(0.2f, 0.5f, 1f); ; // 冰冻蓝色
2024-12-24 14:56:21 +08:00
2024-12-29 17:29:36 +08:00
// 变为蓝色并恢复白色
2024-12-24 14:56:21 +08:00
sequence.Append(spriteRenderer.DOColor(frostBlueWithAlpha, 0.2f))
.Append(spriteRenderer.DOColor(UnityEngine.Color.white, time));
2024-12-16 17:48:50 +08:00
}
2025-01-07 16:50:42 +08:00
else if (color == 2)
{
// 中毒的紫色
UnityEngine.Color poisonPurpleWithAlpha = new UnityEngine.Color(0.6f, 0.2f, 0.8f); // 紫色调,带有绿色的感觉
2024-12-24 14:56:21 +08:00
2025-01-07 16:50:42 +08:00
// 变为紫色并恢复白色
sequence.Append(spriteRenderer.DOColor(poisonPurpleWithAlpha, 0.2f))
.Append(spriteRenderer.DOColor(UnityEngine.Color.white, time));
}
2024-12-29 17:29:36 +08:00
// 开始执行Sequence
2024-12-24 14:56:21 +08:00
sequence.Play();
}
2024-12-16 17:48:50 +08:00
}
2024-12-24 14:56:21 +08:00
2024-12-16 17:48:50 +08:00
public void StopDoTween(float stopTime)
{
2024-12-29 17:29:36 +08:00
// 暂停当前的动画
2024-12-16 17:48:50 +08:00
transform.DOPause();
2024-12-29 17:29:36 +08:00
// 插入一个自定义的延迟(僵直时间)
2024-12-16 17:48:50 +08:00
DOTween.To(() => 0f, x => { }, 0f, stopTime).OnKill(() =>
{
2024-12-29 17:29:36 +08:00
// 在延迟结束后恢复动画
2024-12-16 17:48:50 +08:00
transform.DOPlay();
});
}
2024-12-23 16:57:40 +08:00
public void SetSelfInfo()
{
if (camp==Camp.Player)
{
2024-12-29 17:29:36 +08:00
// Debug.Log("攻击力赋值==============================" + MengyaoInfo.Instance.m_Mengyao.Count);
2024-12-23 16:57:40 +08:00
foreach (Character character in MengyaoInfo.Instance.m_Mengyao)
{
if (id ==character.Id)
{
2024-12-24 09:36:18 +08:00
Name = character.Name;
2024-12-23 16:57:40 +08:00
MinAttack = int.Parse(character.MinAttack);
MaxAttack = int.Parse(character.MaxAttack);
AttackCD = float.Parse(character.AttackCD);
CritRate = float.Parse(character.CritRate);
2024-12-29 17:29:36 +08:00
Quality=character.Quality;//品质
Elements=character.Elements;//属性
Info=character.Info;//详情
SkillId=character.SkillId;//技能
AttackType=character.AttackType;//攻击类型
AttackActionType=character.AttackActionType;//攻击作用类型
2024-12-23 16:57:40 +08:00
CriticalHitRateBonus = float.Parse(character.CriticalHitRateBonus);
2024-12-25 20:31:40 +08:00
AttackRange = float.Parse(character.AttackRange);
2024-12-29 17:29:36 +08:00
//Debug.Log("攻击力赋值++++++++++++++++++++++++++++++");
2024-12-23 16:57:40 +08:00
}
}
}
}
2024-12-29 17:29:36 +08:00
/// <summary>
/// 计算伤害
/// </summary>
/// <param name="self">造成者</param>
/// <param name="Target">目标</param>
/// <returns></returns>
/// 萌妖每次攻击对敌人造成伤害=a*(1+g+d+e)*(1-b/100)*c*(1+h)
//暴击情况下萌妖每次攻击对敌人造成伤害=a* (1+g+d+e)*(1-b/100)*c* (1+h)*(2+f)
public float DamageCreate(Role Target)//伤害生成
2024-12-23 16:57:40 +08:00
{
2024-12-29 17:29:36 +08:00
// 生成普通伤害
2024-12-25 21:41:45 +08:00
float hurt = UnityEngine.Random.Range(MinAttack, MaxAttack);
2024-12-29 17:29:36 +08:00
// 判断是否暴击
float critChance = UnityEngine.Random.Range(0f, 1f); // 生成一个0到1之间的随机数
//敌人防御力
float TargetDefense = Target.gameObject.GetComponent<enemy>().Defense;
2024-12-25 21:41:45 +08:00
2024-12-29 17:29:36 +08:00
if (critChance < CritRate*(1+mySkillUp.CriticalRate)) // 如果暴击发生
2024-12-23 16:57:40 +08:00
{
2024-12-29 17:29:36 +08:00
// 计算暴击伤害(普通伤害乘以暴击加成)
hurt *= (2 + CriticalHitRateBonus*(1+ mySkillUp.CriticalDamage));
isGoodDamege = true;
UnityEngine.Debug.Log("暴击发生!伤害增加");
2024-12-23 16:57:40 +08:00
}
2024-12-25 23:33:51 +08:00
hurt *= (1 + mySkillUp.DamageUp);
2024-12-29 17:29:36 +08:00
// Debug.LogError("伤害加成:"+ mySkillUp.DamageUp);
if (Target.hasfalg(CharacterFlags.big)) // 如果是大型敌人
2024-12-25 21:41:45 +08:00
{
hurt*=(1+mySkillUp.DamageOfBig);
2024-12-29 17:29:36 +08:00
//Debug.LogError("大型敌人加成");
2024-12-25 21:41:45 +08:00
}
2024-12-29 17:29:36 +08:00
if (Target.hasfalg(CharacterFlags.min)) // 如果是小型敌人
2024-12-25 21:41:45 +08:00
{
hurt *= (1 + mySkillUp.DamageOfMin);
2024-12-29 17:29:36 +08:00
//Debug.LogError("小型敌人加成");
2024-12-25 21:41:45 +08:00
}
2024-12-29 17:29:36 +08:00
if (Target.hasfalg(CharacterFlags.fly)) // 如果是飞行敌人
2024-12-25 21:41:45 +08:00
{
hurt *= (1 + mySkillUp.DamageOfSky);
2024-12-29 17:29:36 +08:00
//Debug.LogError("飞行敌人加成");
2024-12-25 21:41:45 +08:00
}
2024-12-29 17:29:36 +08:00
if (Target.hasfalg(CharacterFlags.land)) // 如果是地面敌人
2024-12-25 21:41:45 +08:00
{
hurt *= (1 + mySkillUp.DamageOfland);
2024-12-29 17:29:36 +08:00
//Debug.LogError("地面敌人加成");
2024-12-25 21:41:45 +08:00
}
2025-01-03 18:02:54 +08:00
if (Target.GetComponent<enemy>().isSlowed) // 如果敌人减速
{
hurt *= (1 + mySkillUp.DamageOfSlow);
UnityEngine.Debug.Log("减速敌人加成");
}
hurt *= (1 + WuxingDamage(Target.gameObject.GetComponent<enemy>()));//计算五行属性伤害
hurt *= HujiaDamage(Target.gameObject.GetComponent<enemy>());//计算破甲伤害
2024-12-25 21:41:45 +08:00
2024-12-29 17:29:36 +08:00
if (hurt * (1 - TargetDefense / 100) < 1)
{
hurt = 1;
}
else
{
2025-01-03 18:02:54 +08:00
hurt *= (1 - TargetDefense / 100);
2024-12-29 17:29:36 +08:00
}
2024-12-27 16:49:55 +08:00
if (GetComponent<enemy>())
{
2024-12-27 18:13:16 +08:00
GetComponent<enemy>().HarmNumber += Mathf.Round(hurt);
2024-12-27 16:49:55 +08:00
}
2024-12-27 11:38:49 +08:00
return Mathf.Round(hurt);
2024-12-23 16:57:40 +08:00
}
2024-12-24 12:04:18 +08:00
2024-12-25 11:22:39 +08:00
public virtual void SlowDown(float slowFactor, float duration)
2024-12-24 12:04:18 +08:00
{
2024-12-25 11:22:39 +08:00
}
2024-12-24 12:04:18 +08:00
2025-01-07 16:50:42 +08:00
2025-01-07 17:58:18 +08:00
public void ApplyPoisonDamage(float poisonDuration, float poisonInterval, float poisonDamage, Role AttackRole)
{
// 使用异步方法时要确保调用时使用 await或者启动异步任务
ApplyPoisonCoroutine(poisonDuration, poisonInterval, poisonDamage, AttackRole);
}
private async Task ApplyPoisonCoroutine(float poisonDuration, float poisonInterval, float poisonDamage, Role AttackRole)
2025-01-07 16:50:42 +08:00
{
float elapsedTime = 0f;
FlashRedEffect(2, poisonDuration);
2025-01-07 17:58:18 +08:00
// 通过一个时间间隔来每隔一段时间触发伤害
while (elapsedTime < poisonDuration)
2025-01-07 17:26:01 +08:00
{
2025-01-07 17:58:18 +08:00
// 等待下一个伤害时间点
await Task.Delay((int)(poisonInterval * 1000)); // 乘以1000转换为毫秒
// 每隔 poisonInterval 执行一次伤害
this.bloodLoss(new object[] { this, poisonDamage, DamageType.magicDamage, AttackRole });
//Hp -= poisonDamage;
UnityEngine.Debug.Log($"中毒伤害: {poisonDamage}, 当前血量: {hp}/{maxHp}");
elapsedTime += poisonInterval;
}
UnityEngine.Debug.Log("中毒效果结束");
2025-01-07 16:50:42 +08:00
}
2025-01-07 17:58:18 +08:00
2025-01-07 16:50:42 +08:00
public float WuxingDamage(enemy Target)//五行属性伤害计算 返回倍率减伤或增加伤害
2024-12-29 17:29:36 +08:00
{
// 克制关系表(使用二维数组或字典表示)
// 行:敌人属性,列:萌妖属性
// 正数表示加成负数表示减成0表示无影响
float[,] attributeRelations = new float[5, 5]
{
// 金 木 水 火 土
{ 0f, 0.5f, 0f, -0.25f, 0f }, // 金
{ -0.25f, 0f, 0f, 0f, 0.5f }, // 木
{ 0f, 0f, 0f, 0.5f, -0.25f }, // 水
{ 0.5f, 0f, -0.25f, 0f, 0f }, // 火
{ 0f, -0.25f, 0.5f, 0f, 0f } // 土
};
// 获取克制关系
float relation = attributeRelations[ (int)GetComponent<enemy>().elementType,(int)Target.elementType];
// 如果是正数,表示加成,增加伤害
// 如果是负数,表示减成,减少伤害
2024-12-29 17:44:10 +08:00
// UnityEngine.Debug.Log("属性关系:"+"萌妖"+ GetComponent<enemy>().elementType+"::"+"敌人"+ Target.elementType+"伤害加成"+relation);
2024-12-29 17:29:36 +08:00
return relation;
}
2024-12-24 12:04:18 +08:00
2024-12-29 17:44:10 +08:00
public float HujiaDamage(enemy Target)//护甲伤害计算 返回倍率减伤或增加伤害
{
// 克制关系表(使用二维数组表示,行表示攻击类型,列表示防御类型)
float[,] attackDefenseRelations = new float[5, 4]
{
// 轻甲 重甲 魔盾 灵体
{ 1f, 0.5f, 1f, 0.35f }, // 普通攻击
{ 0.6f, 1.5f, 1f, 0.35f }, // 破甲攻击
{ 1.5f, 0.75f, 1f, 0.35f }, // 刺穿攻击
{ 1.25f, 1.5f, 0.5f, 2f }, // 魔法攻击
{ 1f, 1f, 1f, 1f } // 真实攻击
};
// 获取克制关系
float relation = attackDefenseRelations[int.Parse(AttackType), (int)Target.defenseType];
// 如果是正数,表示加成,增加伤害
// 如果是负数,表示减成,减少伤害
//UnityEngine.Debug.Log("破甲关系:" + "萌妖" + AttackType + "::" + "敌人" + Target.defenseType + "伤害加成" + relation);
return relation;
}
2025-01-09 13:07:52 +08:00
2025-01-09 13:30:13 +08:00
2025-01-09 13:07:52 +08:00
void OnDestroy()
{
this.AnimationTree = null;
2025-01-09 13:30:13 +08:00
// 清理动态加载资源
if (transform.GetComponent<Renderer>() != null)
{
var material = transform.GetComponent<Renderer>().material;
if (material != null) Destroy(material);
var texture = material.mainTexture;
if (texture != null) Destroy(texture);
}
// 停止协程
StopAllCoroutines();
// 清理未使用的资源
Resources.UnloadUnusedAssets();
2025-01-09 13:07:52 +08:00
}
2024-12-04 05:50:39 +08:00
}