diff options
author | chai <215380520@qq.com> | 2022-11-13 11:45:02 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2022-11-13 11:45:02 +0800 |
commit | 0dc203e686f40e33ce384f47edcdaecf390e7aaf (patch) | |
tree | cbd3eabf5d1564564cc02259798b4408773ed95d /Erika/Assets/Scripts/Unit | |
parent | 67519f62c40fda3eb6d022171595a5ab10955f35 (diff) |
* 屏蔽hurtbox
Diffstat (limited to 'Erika/Assets/Scripts/Unit')
-rw-r--r-- | Erika/Assets/Scripts/Unit/AnimationData.cs | 12 | ||||
-rw-r--r-- | Erika/Assets/Scripts/Unit/Collider/ColliderBox.cs | 20 | ||||
-rw-r--r-- | Erika/Assets/Scripts/Unit/Collider/ColliderData.cs | 7 | ||||
-rw-r--r-- | Erika/Assets/Scripts/Unit/Collider/CollisionHandler.cs | 60 | ||||
-rw-r--r-- | Erika/Assets/Scripts/Unit/Components/UnitCollider.cs | 50 | ||||
-rw-r--r-- | Erika/Assets/Scripts/Unit/Controller/UnitController.cs | 11 |
6 files changed, 132 insertions, 28 deletions
diff --git a/Erika/Assets/Scripts/Unit/AnimationData.cs b/Erika/Assets/Scripts/Unit/AnimationData.cs index 3f516d4b..e66e70b2 100644 --- a/Erika/Assets/Scripts/Unit/AnimationData.cs +++ b/Erika/Assets/Scripts/Unit/AnimationData.cs @@ -403,12 +403,12 @@ public class AnimationData : ScriptableObject public int GetBoxesCount()
{
- int hurt = hurtBoxes != null ? hurtBoxes.Count : 0;
+ //int hurt = hurtBoxes != null ? hurtBoxes.Count : 0;
int hit = hitBoxes != null ? hitBoxes.Count : 0;
int thro = throwBoxes != null ? throwBoxes.Count : 0;
int block = blockBoxes != null ? blockBoxes.Count : 0;
int defend = defendBoxes != null ? defendBoxes.Count : 0;
- return hurt + hit + thro + block + defend;
+ return /*hurt + */hit + thro + block + defend;
}
public void AddBox(ref List<ColliderData> boxList, ColliderData box)
@@ -432,10 +432,10 @@ public class AnimationData : ScriptableObject public ColliderData GetColliderByIndex(int index)
{
- if (hurtBoxes != null && hurtBoxes.Count > index)
- return hurtBoxes[index];
- else
- index -= hurtBoxes.Count;
+ //if (hurtBoxes != null && hurtBoxes.Count > index)
+ // return hurtBoxes[index];
+ //else
+ // index -= hurtBoxes.Count;
if (hitBoxes != null && hitBoxes.Count > index)
return hitBoxes[index];
else
diff --git a/Erika/Assets/Scripts/Unit/Collider/ColliderBox.cs b/Erika/Assets/Scripts/Unit/Collider/ColliderBox.cs index b44f2bf3..1a5d3916 100644 --- a/Erika/Assets/Scripts/Unit/Collider/ColliderBox.cs +++ b/Erika/Assets/Scripts/Unit/Collider/ColliderBox.cs @@ -16,19 +16,27 @@ public partial class ColliderBox // 分化为不同的collider类型
public enum EColliderType
{
- HitBox, // Triggers //Physics.OverlapXXX()
- HurtBox, // Triggers
- ThrowBox, // Triggers
- BlockBox, // Triggers
- DefendBox, // Triggers
+ Begin = 0,
+
+ HitBox = 0, // Triggers //Physics.OverlapXXX()
+ HurtBox, // Triggers
+ ThrowBox, // Triggers
+ BlockBox, // Triggers
+ DefendBox, // Triggers
+
+ End
}
[DisallowModifiyInGUI]
+ [Tooltip("唯一编号")]
+ public int uid;
+
+ [DisallowModifiyInGUI]
[Tooltip("Collider类型")]
public EColliderType type;
[DisallowModifiyInGUI]
- [Tooltip("Collider原点")]
+ [Tooltip("Collider原点,不同类型有不同的pivot设置")]
public Pivot pivot;
}
\ No newline at end of file diff --git a/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs b/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs index 58743aff..719efa76 100644 --- a/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs +++ b/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs @@ -6,6 +6,7 @@ using UnityEngine; // 用来返回某一时刻的碰撞盒几何数据
public struct ColliderInfo
{
+ public int uid; // 唯一编号(非索引号),保证唯一性
public bool active;
public float frame;
public Vector3 position;
@@ -22,6 +23,9 @@ public struct ColliderInfo [Serializable]
public class ColliderData
{
+ /// <summary>
+ /// 帧数据
+ /// </summary>
[Serializable]
public class ColliderFrame
{
@@ -36,6 +40,7 @@ public class ColliderData public ColliderBox collider;
+ // 关键帧
public List<ColliderFrame> frames;
public ColliderData(ColliderBox.EColliderType type, ColliderBox.Pivot pivot)
@@ -43,6 +48,7 @@ public class ColliderData this.frames = new List<ColliderFrame>();
if (collider == null)
collider = new ColliderBox();
+ collider.uid = CommonFunction.GetUUID();
collider.type = type;
collider.pivot = pivot;
}
@@ -50,6 +56,7 @@ public class ColliderData public ColliderInfo GetColliderInfo(float frame)
{
ColliderInfo info = new ColliderInfo();
+ info.uid = collider.uid;
info.active = false; // default
info.collider = collider;
info.frame = frame;
diff --git a/Erika/Assets/Scripts/Unit/Collider/CollisionHandler.cs b/Erika/Assets/Scripts/Unit/Collider/CollisionHandler.cs index 0dd68d40..296d62b0 100644 --- a/Erika/Assets/Scripts/Unit/Collider/CollisionHandler.cs +++ b/Erika/Assets/Scripts/Unit/Collider/CollisionHandler.cs @@ -5,17 +5,53 @@ using UnityEngine; public class CollisionHandler : MonoBehaviour { + public ColliderBox.EColliderType colliderType { get { return m_ColliderBox.type; } } + public ColliderBox colliderBox { get { return m_ColliderBox; } } - public ColliderBox.EColliderType colliderType; +#if UNITY_EDITOR + public ColliderBox m_ColliderBox; + public UnitController m_Owner; +#else + ColliderBox m_ColliderBox; + UnitController m_Owner; +#endif - public ColliderData colliderData; + public new Collider collider { get { return m_Collider; } } + private Collider m_Collider; - public UnitController owner; + public void Initialize(UnitController owner, ColliderBox box) + { + this.m_ColliderBox = box; + this.m_Owner = owner; + + this.transform.SetParent(owner.transform); + this.transform.localPosition = Vector3.zero; + + m_Collider = GetComponent<Collider>(); + } + + private void SetActive(bool isActive) + { + m_Collider.enabled = isActive; + } - public void Initialize(ColliderBox.EColliderType type, UnitController owner) + public void OnUpdate(ColliderInfo info) { - this.colliderType = type; - this.owner = owner; + SetActive(info.active); + + if(collider is BoxCollider) + { + BoxCollider bc = collider as BoxCollider; + Vector3 center = info.position; + switch(info.collider.pivot) + { + case ColliderBox.Pivot.MiddleBottom: + center.y += info.size.y / 2.0f; + break; + } + bc.center = center; + bc.size = info.size; + } } public void OnTriggerEnter(Collider other) @@ -24,15 +60,15 @@ public class CollisionHandler : MonoBehaviour if (otherHandler == null) return; - if(owner != null) + if(m_Owner != null) { - owner.OnCollision(otherHandler.owner); + m_Owner.OnCollision(otherHandler.m_Owner, this, otherHandler); } - if(otherHandler.owner != null) - { - otherHandler.owner.OnCollision(owner); - } + //if(otherHandler.m_Owner != null) + //{ + // otherHandler.m_Owner.OnCollision(m_Owner, otherHandler, this); + //} } diff --git a/Erika/Assets/Scripts/Unit/Components/UnitCollider.cs b/Erika/Assets/Scripts/Unit/Components/UnitCollider.cs index 6858fe74..52790488 100644 --- a/Erika/Assets/Scripts/Unit/Components/UnitCollider.cs +++ b/Erika/Assets/Scripts/Unit/Components/UnitCollider.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using UnityEngine; -// 角色当前的碰撞盒 +/// <summary> +/// 角色当前所有碰撞盒的生命周期(创建、维护、销毁) +/// </summary> [DisallowMultipleComponent] public class UnitCollider : UnitComponent { @@ -11,6 +13,9 @@ public class UnitCollider : UnitComponent [System.Obsolete("废弃")] private Dictionary<int/*hitbox hash*/, List<int/*unitController hash*/>> m_HitMask = new Dictionary<int, List<int>>(); + private List<CollisionHandler> m_AllColliders = new List<CollisionHandler>(); + private HashSet<int> m_ActiveColliders = new HashSet<int>(); // 当前激活的collider + public override void Awake() { base.Awake(); @@ -29,15 +34,16 @@ public class UnitCollider : UnitComponent { base.Initialize(); showGizmos = true; + + m_AllColliders.Clear(); } /// <summary> /// 返回当前激活的对应类型的碰撞盒数据 /// </summary> /// <param name="type"></param> - /// <param name="layer"></param> /// <returns></returns> - public ColliderInfo[] GetCurrentBoxesInfoByType(ColliderBox.EColliderType type, int layer = 0) + public ColliderInfo[] GetCurrentBoxesInfoByType(ColliderBox.EColliderType type) { AnimationData animData = m_Owner.unitAnimation.animationData; float playbackTime = m_Owner.unitAnimation.playbackNormalizedTime * m_Owner.unitAnimation.animationClip.length; @@ -78,6 +84,44 @@ public class UnitCollider : UnitComponent return !record.Contains(targetHash); } + public override void OnUpdate() + { + m_ActiveColliders.Clear(); + + for (int i = (int)ColliderBox.EColliderType.Begin; i < (int)ColliderBox.EColliderType.End; ++i) + { + ColliderBox.EColliderType colliderType = (ColliderBox.EColliderType)i; + ColliderInfo[] activeColliders = GetCurrentBoxesInfoByType(colliderType); + if(activeColliders == null || activeColliders.Length == 0) + continue; + for(int j = 0; j < activeColliders.Length; ++j) + { + ColliderInfo info = activeColliders[j]; + m_ActiveColliders.Add(info.uid); + CollisionHandler handler = m_AllColliders.Find(s => s.colliderBox.uid == info.uid); + if(handler == null) + { + handler = ResourceManager.Instance.CreateObj<CollisionHandler>(StaticDefine.collider); + handler.Initialize(m_Owner, info.collider); + m_AllColliders.Add(handler); + } + handler.OnUpdate(info); + } + } + + for(int i = m_AllColliders.Count - 1; i >= 0; --i) + { + CollisionHandler handler = m_AllColliders[i]; + if(!m_ActiveColliders.Contains(handler.colliderBox.uid)) + { + m_AllColliders.RemoveAt(i); + + handler.gameObject.SetActive(false); + Destroy(handler.gameObject); + } + } + } + #if UNITY_EDITOR // 绘制collider调试 diff --git a/Erika/Assets/Scripts/Unit/Controller/UnitController.cs b/Erika/Assets/Scripts/Unit/Controller/UnitController.cs index f5577077..ef15ebae 100644 --- a/Erika/Assets/Scripts/Unit/Controller/UnitController.cs +++ b/Erika/Assets/Scripts/Unit/Controller/UnitController.cs @@ -67,6 +67,14 @@ public class UnitController : MonoBehaviour/*, Interactable*/ }
}
+ public int towardFactor
+ {
+ get
+ {
+ return isTowardRight ? 1 : -1;
+ }
+ }
+
public virtual bool isOnGround
{
get
@@ -231,6 +239,7 @@ public class UnitController : MonoBehaviour/*, Interactable*/ unitRootMotion.OnUpdate();
unitLensEffect.OnUpdate();
unitPhysicsBoxes.OnUpdate();
+ unitCollider.OnUpdate();
}
public virtual void OnDestroy()
@@ -270,7 +279,7 @@ public class UnitController : MonoBehaviour/*, Interactable*/ #region 物理碰撞
- public void OnCollision(UnitController other)
+ public void OnCollision(UnitController other, CollisionHandler selfHandler, CollisionHandler otherHandler)
{
}
|