From 0fdb81ffb2af8c39cfd611f485d46f3341206832 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 9 Nov 2020 16:03:45 +0800 Subject: * HitManager --- Assets/Scripts/Managers/HitManager.cs | 121 ++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 Assets/Scripts/Managers/HitManager.cs (limited to 'Assets/Scripts/Managers/HitManager.cs') diff --git a/Assets/Scripts/Managers/HitManager.cs b/Assets/Scripts/Managers/HitManager.cs new file mode 100644 index 00000000..dfa6530f --- /dev/null +++ b/Assets/Scripts/Managers/HitManager.cs @@ -0,0 +1,121 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public struct HitInfo +{ + public HitDefination hitDef; + public Hitbox hitbox; + public Hurtbox hurtbox; + public Vector3 contact; + public Vector3 size; +} + +public struct HurtInfo +{ + public HitDefination hitDef; + public Hitbox hitbox; + public Hurtbox hurtbox; + public Vector3 contact; + public Vector3 size; +} + +public class HitManager : Singleton +{ + private List m_HitBoxes = new List(); + private List m_HurtBoxes = new List(); + + public void AddHitBox(Hitbox hitbox) + { + m_HitBoxes.Add(hitbox); + } + + public void AddHurtBox(Hurtbox hurtbox) + { + m_HurtBoxes.Add(hurtbox); + } + + /// + /// 根据碰撞结果产生击中信息 + /// + public void OnPhysicsUpdate() + { + for(int i = 0;i < m_HitBoxes.Count; ++i) + { + Hitbox hitbox = m_HitBoxes[i]; + if (hitbox == null) + return; + for (int j = 0; j < PhysicsWorld.Instance.Collisions.Count; ++j) + { + PhysicsCollisionInfo info = PhysicsWorld.Instance.Collisions[j]; + PhysicsPrimitive collider; + if(info.prim1 == hitbox.Collider) + { + collider = info.prim2; + } + else if(info.prim2 == hitbox.Collider) + { + collider = info.prim1; + } + else + { + continue; + } + + Hurtbox hurtbox = GetHurtboxByCollider(collider); + if(hurtbox == null) + { + Debug.LogError("没有找到hurtbox"); + continue; + } + + HitDefination hitDef = null; + + if (hitbox.Host is Avatar) + { + Avatar attacker = hitbox.Host as Avatar; + Hit hit = attacker.GetHit(); + if(hit == null) + continue; + if (hit.HasRecord(hurtbox.Host)) + continue; + hit.AddRecord(hurtbox.Host); + hitDef = hit.defination; + } + + // 发送击中消息 + HitInfo hitInfo = new HitInfo (); + hitInfo.hitbox = hitbox; + hitInfo.hurtbox = hurtbox; + hitInfo.contact = info.contact; + hitInfo.size = info.size; + hitInfo.hitDef = hitDef; + + hitbox.Host.OnHit(hitInfo); + + // 发送受击消息 + HurtInfo hurtInfo = new HurtInfo(); + hurtInfo.hitbox = hitbox; + hurtInfo.hurtbox = hurtbox; + hurtInfo.contact = info.contact; + hurtInfo.size = info.size; + hurtInfo.hitDef = hitDef; + + hurtbox.Host.OnHurt(hurtInfo); + } + } + } + + Hurtbox GetHurtboxByCollider(PhysicsPrimitive collider) + { + foreach(var hurtbox in m_HurtBoxes) + { + if(hurtbox.Collider == collider) + { + return hurtbox; + } + } + return null; + } + +} -- cgit v1.1-26-g67d0