From 5e92a0630dc1112bc03996a6b2e56a0cd461ca9d Mon Sep 17 00:00:00 2001 From: wulongxiao <2545507770@qq.com> Date: Sat, 14 Dec 2024 21:15:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=98=E8=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Role/Role.cs | 133 +++++++++++++++++++---- Role/move/2D/SimplePathfindingDoTween.cs | 12 +- 2 files changed, 122 insertions(+), 23 deletions(-) diff --git a/Role/Role.cs b/Role/Role.cs index c63b9b2..b9a777f 100644 --- a/Role/Role.cs +++ b/Role/Role.cs @@ -37,12 +37,34 @@ public class Role : Fun [Header("Id")] public int id; [Header("阵营")] public Camp camp ; [Header("血量")] public float hp = 100f;//血量 + private float maxHp; + [Header(("死亡动画编号"))] public int dieIndex=-1; + + [Header("hp填充")] public Image Hpfiil; + [Header("扣血填充")] public GameObject HpTextPrefab; + [Header("自己的画布")]public Canvas _Canvas; public float Hp { get => hp; set { + float temp = hp; hp = value; + + if (Hpfiil!=null) + { + Hpfiil.fillAmount = hp / maxHp; + + } + + if (HpTextPrefab != null&& hp().text= (temp - hp).ToString(); + + } + if (hp <= 0) { die(); @@ -90,14 +112,20 @@ public class Role : Fun public event AnimationItem OnAnimationEnd; [Header("动画是否正常播放")] public bool isAnimationPlay = false;//动画是否正常播放 [Header("碰撞体")]public CircleCollider2D mycollider; - [Header("攻击脚本")] public Attack attackClass; - - - - public virtual async void Start() + [Header("攻击脚本")] public Attack attackClass; + [Header("当前播放图片序列")] public int CurrentIndex; + + + public virtual async void Start() { + maxHp = hp; + + if (Application.isPlaying) { + + + if (attackClass) { attackClass.role = this; @@ -139,35 +167,55 @@ public class Role : Fun return; }; OnAnimationStart?.Invoke(animationHighlight); + CurrentIndex = 0; + var lsanimationHighlight = animationHighlight; foreach (Sprite sprite in LightSprite) { - if (image != null) + if (lsanimationHighlight == animationHighlight) { - image.sprite = sprite; + 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); + } + + CurrentIndex++; } - else if (spriteRenderer != null) + else { - spriteRenderer.sprite = sprite; + break; } - if (animationHighlight >= 0 && animationHighlight < AnimationTree.Count) - { - OnAnimationIng?.Invoke(animationHighlight); - await Task.Delay(AnimationTree[animationHighlight].CharacterAnimationFrameInterval); - } - } - OnAnimationEnd?.Invoke(animationHighlight); + + OnAnimationEnd?.Invoke(lsanimationHighlight); + + if (lsanimationHighlight == dieIndex) + { + SpawnPool.intance.ReturnEnemyToPool(this.gameObject); // 回收当前敌人到池中 + ResetAllStatus(); + } } } /// /// 角色死亡 /// - public virtual void die() + public virtual void die() { if (Application.isPlaying) { - Destroy(gameObject); + animationHighlight = dieIndex; + Navigation.PauseAnimation(); + Debug.Log("die"); } } @@ -205,9 +253,54 @@ public class Role : Fun } - + + /// + /// 重置角色所有状态 + /// + public void ResetAllStatus() + { + // 重置血量 + Hp = 100f; + + // 重置攻击力 + Attack = 10f; + + // 重置护甲 + physicalArmor = 10; + magicArmor = 5; + + // 重置等级 + Level = 1; + + // 重置金币 + Gold = 10f; + + // 清空 Buff 列表 + buffList.Clear(); + + // 如果有导航组件,停止当前的导航行为 + if (Navigation != null) + { + //Navigation.Stop(); // 假设你有 Stop 方法来停止导航 + } + + // 重置角色动画 + animationHighlight = 0; + isAnimationPlay = false; + + // 重置碰撞体(根据需求是否需要调整) + if (mycollider != null) + { + mycollider.enabled = true; // 启用碰撞体 + } + + + + // 如果有其他需要重置的状态,可以在这里添加 + } + + - } diff --git a/Role/move/2D/SimplePathfindingDoTween.cs b/Role/move/2D/SimplePathfindingDoTween.cs index 5d55955..8c8e48d 100644 --- a/Role/move/2D/SimplePathfindingDoTween.cs +++ b/Role/move/2D/SimplePathfindingDoTween.cs @@ -17,6 +17,8 @@ public class SimplePathfindingDoTween : Fun private int currentWaypointIndex = 0; // 当前路径点的索引 Vector3 Vector3 = new Vector3(); + + [Header("旋转的动画")]public GameObject obj; public void MoveToNextWaypoint(GameObject gameObject) { if (currentWaypointIndex < waypoints._waypoints.Count) @@ -47,7 +49,11 @@ public class SimplePathfindingDoTween : Fun } } - + public void PauseAnimation() + { + // 暂停动画 + transform.DOPause(); + } void RotateTowardsTarget() { Vector3 _vector3 = Vector3 - gameObject.transform.position; @@ -55,12 +61,12 @@ public class SimplePathfindingDoTween : Fun if (_vector3.x>0) { // 物体在目标的右边或平行,设置物体的y旋转为180°,面向左侧 - gameObject.transform.rotation = Quaternion.Euler(0, 0, 0); + obj.transform.rotation = Quaternion.Euler(0, 0, 0); } else { // 物体在目标的左边,设置物体的y旋转为0°,面向右侧 - gameObject.transform.rotation = Quaternion.Euler(0, 180, 0); + obj.transform.rotation = Quaternion.Euler(0, 180, 0); } } } \ No newline at end of file