summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/Collider/ColliderData.cs
blob: 0b79d04ae0c39f64d90a385a67bfbe19fd6ec484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct ColliderInfo
{
    public bool active;
    public Vector3 position;
    public Vector3 size;
}

[Serializable]
public class ColliderData
{
    [Serializable]
    public class ColliderFrame
    {
        public int frame;
        public bool active;
        public Vector3 position;
        public Vector3 size;
    }

    public ColliderBox.EColliderType type;

    public ColliderBox.Pivot pivot;

    public List<ColliderFrame> frames;

    public ColliderData(ColliderBox.EColliderType type, ColliderBox.Pivot pivot)
    {
        this.type = type;
        this.pivot = pivot;
        this.frames = new List<ColliderFrame>();
    }

    public ColliderInfo GetColliderInfo(float frame)
    {
        ColliderInfo info = new ColliderInfo();
        info.active = false; // default
        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 void 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 = Vector3.one;
        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;
        });
    }

    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);
        }
    }

}