summaryrefslogtreecommitdiff
path: root/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Erika/Assets/Scripts/Unit/Collider/ColliderData.cs')
-rw-r--r--Erika/Assets/Scripts/Unit/Collider/ColliderData.cs136
1 files changed, 136 insertions, 0 deletions
diff --git a/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs b/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs
new file mode 100644
index 00000000..58743aff
--- /dev/null
+++ b/Erika/Assets/Scripts/Unit/Collider/ColliderData.cs
@@ -0,0 +1,136 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+// 用来返回某一时刻的碰撞盒几何数据
+public struct ColliderInfo
+{
+ public bool active;
+ public float frame;
+ public Vector3 position;
+ public Vector3 size;
+ public ColliderBox.EColliderType type { get { return collider.type; } }
+ public ColliderBox.Pivot pivot { get { return collider.pivot; } }
+ public ColliderBox collider;
+ public int colliderHash { get { return collider.GetHashCode(); } }
+
+ public bool isValid { get { return collider != null; } } // 没有对应的数据时为false
+}
+
+// 某个碰撞盒的属性和帧数据,从属于animation data
+[Serializable]
+public class ColliderData
+{
+ [Serializable]
+ public class ColliderFrame
+ {
+ public int frame;
+ public bool active;
+ public Vector3 position;
+ public Vector3 size;
+ }
+
+ public ColliderBox.EColliderType type { get { return collider.type; } }
+ public ColliderBox.Pivot pivot { get { return collider.pivot; } }
+
+ public ColliderBox collider;
+
+ public List<ColliderFrame> frames;
+
+ public ColliderData(ColliderBox.EColliderType type, ColliderBox.Pivot pivot)
+ {
+ this.frames = new List<ColliderFrame>();
+ if (collider == null)
+ collider = new ColliderBox();
+ collider.type = type;
+ collider.pivot = pivot;
+ }
+
+ public ColliderInfo GetColliderInfo(float frame)
+ {
+ ColliderInfo info = new ColliderInfo();
+ info.active = false; // default
+ info.collider = collider;
+ info.frame = frame;
+ int previous = 0;
+ int end = -1;
+ for (int i = 0; i < frames.Count; ++i)
+ {
+ if(frame >= frames[i].frame)
+ {
+ previous = frames[i].frame;
+ }
+ if(frames[i].frame > frame)
+ {
+ end = frames[i].frame;
+ break;
+ }
+ }
+ if(end == -1)
+ {
+ if(type == ColliderBox.EColliderType.HurtBox)
+ {
+ ColliderFrame pre = frames.Find(s => s.frame == previous);
+ if (pre == null)
+ return info;
+ info.active = pre.active;
+ info.position = pre.position;
+ info.size = pre.size;
+ }
+ }
+ else
+ {
+ ColliderFrame pre = frames.Find(s => s.frame == previous);
+ ColliderFrame next = frames.Find(s => s.frame == end);
+ if (pre == null || next == null)
+ return info;
+ info.active = pre.active;
+ float t = (frame - previous) / (end - previous);
+ info.position = Vector3.Lerp(pre.position, next.position, t);
+ info.size = Vector3.Lerp(pre.size, next.size, t);
+ }
+ return info;
+ }
+
+ public ColliderFrame AddFrame(int frameIndex)
+ {
+ if (frames == null)
+ frames = new List<ColliderFrame>();
+ ColliderFrame frame = new ColliderFrame();
+ frame.frame = frameIndex;
+ frame.active = true;
+ frame.position = Vector3.zero;
+ frame.size = new Vector3(0.5f,1,1);
+ frames.Add(frame);
+ frames.Sort((a, b) => {
+ if (a == null)
+ return 1;
+ if (b == null)
+ return -1;
+ if (a.frame < b.frame)
+ return -1;
+ if (a.frame > b.frame)
+ return 1;
+ return 0;
+ });
+ return frame;
+ }
+
+ public void DeleteFrame(int frameIndex)
+ {
+ if (frames == null)
+ return;
+ ColliderFrame frame = null;
+ foreach(var f in frames)
+ {
+ if (f.frame == frameIndex)
+ frame = f;
+ }
+ if(frame != null)
+ {
+ frames.Remove(frame);
+ }
+ }
+
+}