blob: fde705fda4004e661651e01effe942030fadb070 (
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PrimitiveType
{
None,
Ball,
Box
}
/// <summary>
/// 物理碰撞体基类
/// 对于格斗游戏来说只需要Box和Ball两种
/// </summary>
public abstract class PhysicsPrimitive : MonoBehaviour
{
public virtual PrimitiveType Type
{
get
{
return PrimitiveType.None;
}
}
/// <summary>
/// 世界坐标,物理模拟是在世界空间,本地坐标没有意义
/// </summary>
public Vector3 Position
{
get
{
Vector3 euler = Quaternion.ToEulerAngles(transform.rotation);
euler.y = Mathf.Rad2Deg * euler.y;
Vector3 res = m_Center;
res.x = (euler.y > 0 && euler.y <= 180) ? -res.x : res.x;
res = transform.position + res;
return res;
}
}
[SerializeField]
/// <summary>
/// 这个primitive是否参与物理计算,用来快速给物体取消和恢复重力影响
/// </summary>
protected bool m_IsActive;
public bool IsActive
{
get
{
return m_IsActive;
}
}
/// <summary>
/// 唯一ID
/// </summary>
protected int m_ID;
public int ID
{
get
{
return m_ID;
}
}
[Tooltip("给primitive分组,决定大组和大组之间是否有碰撞检测")]
[SerializeField]
protected PhysicsGroup m_Group;
public PhysicsGroup Group
{
get
{
return m_Group;
}
}
[Tooltip("给primitive一个标记,表明它的从属关系。帮助做hitbox和hurtbox")]
[SerializeField]
protected PhysicsTag m_Tag;
public PhysicsTag Tag
{
get
{
return m_Tag;
}
}
[Tooltip("同一个标签的没有碰撞检测")]
[SerializeField]
protected string m_Label;
public string Label
{
get
{
return m_Label;
}
}
[Tooltip("单个碰撞体的描述(名称),作为标识用来识别")]
[SerializeField]
protected string m_Title;
public string Title
{
get
{
return m_Title;
}
}
[SerializeField]
protected string m_Desc;
public string Desc
{
get
{
return m_Desc;
}
}
/// <summary>
/// 初始化,设置一些公共数据
/// </summary>
protected void OnInit()
{
m_ID = UIDManager.Acquire();
PhysicsWorld.Instance.AddPrimitive(this);
}
protected Color Color_Green = new Color(0, 1, 0, 0.5f);
[SerializeField]
protected Color m_HintColor = new Color(0, 0, 0, 0.5f);
[Tooltip("Physics body, leave blank and primitive will be static.")]
[SerializeField]
protected PhysicsBody m_Body;
public PhysicsBody Body
{
get
{
return m_Body;
}
}
/// <summary>
/// 中心点在本地空间的位置
/// </summary>
public Vector3 m_Center;
/// <summary>
/// 边界:左、右、上、下
/// </summary>
public Vector4 Bound
{
get
{
Vector4 bound = new Vector4();
if(this is PhysicsBox)
{
PhysicsBox box = this as PhysicsBox;
bound.x = box.Left;
bound.y = box.Right;
bound.z = box.Top;
bound.w = box.Bottom;
}
else if(this is PhysicsBall)
{
PhysicsBall ball = this as PhysicsBall;
bound.x = ball.Position.x - ball.Radius;
bound.y = ball.Position.x + ball.Radius;
bound.z = ball.Position.y + ball.Radius;
bound.w = ball.Position.y - ball.Radius;
}
return bound;
}
}
public bool IsOnGround
{
get
{
if (Mathf.Approximately(Bound.w, PhysicsWorld.Ground))
return true;
return Bound.w <= PhysicsWorld.Ground;
}
}
public bool IsInAir
{
get
{
//return Bound.w > PhysicsWorld.Ground;
return !IsOnGround;
}
}
}
|