1+ //=======================================================================
2+ // Copyright (c) 2015 John Pan
3+ // Distributed under the MIT License.
4+ // (See accompanying file LICENSE or copy at
5+ // http://opensource.org/licenses/MIT)
6+ //=======================================================================
7+
8+ using UnityEngine ;
9+ using UnityEngine . Serialization ;
10+ using System . Collections . Generic ;
11+
12+ namespace Lockstep
13+ {
14+ [ System . Serializable ]
15+ public partial class LSBody : MonoBehaviour
16+ {
17+ #region Core deterministic variables
18+
19+ [ SerializeField ] //For inspector debugging
20+ internal Vector2d _position ;
21+ [ SerializeField ]
22+ internal Vector2d _rotation = Vector2d . up ;
23+ [ SerializeField , FixedNumber ]
24+ internal long _heightPos ;
25+ [ SerializeField ]
26+ public Vector2d _velocity ;
27+
28+ #endregion
29+
30+ #region Lockstep variables
31+
32+ private bool _forwardNeedsSet = false ;
33+
34+ private bool ForwardNeedsSet {
35+ get { return _forwardNeedsSet ; }
36+ set { _forwardNeedsSet = value ; }
37+ }
38+
39+
40+
41+
42+ private Vector2d _forward ;
43+
44+ public Vector2d Forward {
45+ get {
46+ return Rotation . ToDirection ( ) ;
47+ }
48+ set {
49+ Rotation = value . ToRotation ( ) ;
50+ }
51+ }
52+
53+ [ Lockstep ]
54+ public bool PositionChanged { get ; set ; }
55+
56+ [ Lockstep ]
57+ public Vector2d Position {
58+ get {
59+ return _position ;
60+ }
61+ set {
62+ _position = value ;
63+ this . PositionChanged = true ;
64+ }
65+ }
66+
67+ private bool _rotationChanged ;
68+
69+ [ Lockstep ]
70+ public bool RotationChanged {
71+ get {
72+ return _rotationChanged ;
73+ }
74+ set {
75+ if ( value )
76+ ForwardNeedsSet = true ;
77+ _rotationChanged = value ;
78+ }
79+ }
80+
81+
82+ [ Lockstep ]
83+ public Vector2d Rotation {
84+ get {
85+ return _rotation ;
86+ }
87+ set {
88+ _rotation = value ;
89+ this . RotationChanged = true ;
90+ }
91+ }
92+
93+ [ Lockstep ]
94+ public bool HeightPosChanged { get ; set ; }
95+
96+ [ Lockstep ]
97+ public long HeightPos {
98+ get { return _heightPos ; }
99+ set {
100+ _heightPos = value ;
101+ this . HeightPosChanged = true ;
102+ }
103+ }
104+
105+ [ Lockstep ]
106+ public bool VelocityChanged { get ; set ; }
107+
108+ [ Lockstep ]
109+ public Vector2d Velocity {
110+ get { return _velocity ; }
111+ set {
112+ _velocity = value ;
113+ VelocityChanged = true ;
114+ }
115+ }
116+
117+ public Vector2d LastPosition { get ; private set ; }
118+
119+ internal uint RaycastVersion { get ; set ; }
120+
121+ #endregion
122+
123+ internal Vector3 _visualPosition ;
124+
125+ public Vector3 VisualPosition { get { return _visualPosition ; } }
126+
127+ public LSAgent Agent { get ; private set ; }
128+
129+ public long FastRadius { get ; private set ; }
130+
131+ public bool PositionChangedBuffer { get ; private set ; }
132+ //D
133+ public bool SetPositionBuffer { get ; private set ; }
134+ //ND
135+
136+
137+ public bool RotationChangedBuffer { get ; private set ; }
138+ //D
139+ private bool SetRotationBuffer { get ; set ; }
140+ //ND
141+
142+ bool SetVisualPosition { get ; set ; }
143+
144+ bool SetVisualRotation { get ; set ; }
145+
146+ private bool Setted { get ; set ; }
147+
148+
149+ public long VelocityFastMagnitude { get ; private set ; }
150+
151+
152+ public bool Active { get ; private set ; }
153+
154+ private void AddChild ( LSBody_ child )
155+ {
156+ if ( Children == null )
157+ Children = new FastBucket < LSBody_ > ( ) ;
158+ Children . Add ( child ) ;
159+ }
160+
161+ private void RemoveChild ( LSBody_ child )
162+ {
163+ Children . Remove ( child ) ;
164+ }
165+
166+ private FastBucket < LSBody_ > Children ;
167+ public Vector2d [ ] RealPoints ;
168+ public Vector2d [ ] Edges ;
169+ public Vector2d [ ] EdgeNorms ;
170+
171+
172+ public long XMin { get ; private set ; }
173+
174+ public long XMax { get ; private set ; }
175+
176+ public long YMin { get ; private set ; }
177+
178+ public long YMax { get ; private set ; }
179+
180+ public int PastGridXMin ;
181+ public int PastGridXMax ;
182+ public int PastGridYMin ;
183+ public int PastGridYMax ;
184+
185+ public long HeightMin { get ; private set ; }
186+
187+ public long HeightMax { get ; private set ; }
188+
189+ public delegate void CollisionFunction ( LSBody_ other ) ;
190+
191+ private Dictionary < int , CollisionPair > _collisionPairs ;
192+ private HashSet < int > _collisionPairHolders ;
193+
194+ internal Dictionary < int , CollisionPair > CollisionPairs {
195+ get {
196+ return _collisionPairs . IsNotNull ( ) ? _collisionPairs : ( _collisionPairs = new Dictionary < int , CollisionPair > ( ) ) ;
197+ }
198+ }
199+
200+ internal HashSet < int > CollisionPairHolders {
201+ get {
202+ return _collisionPairHolders ?? ( _collisionPairHolders = new HashSet < int > ( ) ) ;
203+ }
204+ }
205+
206+ internal void NotifyContact ( LSBody_ other , bool isColliding , bool isChanged )
207+ {
208+ if ( isColliding ) {
209+ if ( isChanged ) {
210+ if ( onContactEnter . IsNotNull ( ) )
211+ onContactEnter ( other ) ;
212+ }
213+ if ( onContact != null )
214+ onContact ( other ) ;
215+
216+ } else {
217+ if ( isChanged ) {
218+ if ( onContactExit != null )
219+ onContactExit ( other ) ;
220+ }
221+ }
222+ }
223+
224+ public event CollisionFunction onContact ;
225+ public event CollisionFunction onContactEnter ;
226+ public event CollisionFunction onContactExit ;
227+
228+ public int ID { get ; private set ; }
229+
230+ private int _dynamicID = - 1 ;
231+
232+ internal int DynamicID { get { return _dynamicID ; } set { _dynamicID = value ; } }
233+
234+ public int Priority { get ; set ; }
235+
236+ #region Serialized
237+
238+ [ SerializeField ]
239+ protected ColliderType _shape = ColliderType . None ;
240+
241+ public ColliderType Shape { get { return _shape ; } }
242+
243+ [ SerializeField ]
244+ private bool _isTrigger ;
245+
246+ public bool IsTrigger { get { return _isTrigger ; } }
247+
248+ [ SerializeField ]
249+ private int _layer ;
250+
251+ public int Layer { get { return _layer ; } }
252+
253+ [ SerializeField , FixedNumber ]
254+ private long _halfWidth = FixedMath . Half ;
255+
256+ public long HalfWidth { get { return _halfWidth ; } }
257+
258+ [ SerializeField , FixedNumber ]
259+ public long _halfHeight = FixedMath . Half ;
260+
261+ public long HalfHeight { get { return _halfHeight ; } }
262+
263+ [ SerializeField , FixedNumber ]
264+ protected long _radius = FixedMath . Half ;
265+
266+ public long Radius { get { return _radius ; } }
267+
268+ [ SerializeField ]
269+ protected bool _immovable ;
270+
271+ public bool Immovable { get ; private set ; }
272+
273+ [ SerializeField ]
274+ private int _basePriority ;
275+
276+ public int BasePriority { get { return _basePriority ; } }
277+
278+ [ SerializeField ]
279+ private Vector2d [ ] _vertices ;
280+
281+ public Vector2d [ ] Vertices { get { return _vertices ; } }
282+
283+ [ SerializeField , FixedNumber ]
284+ private long _height = FixedMath . One ;
285+
286+ public long Height { get { return _height ; } }
287+
288+
289+ [ SerializeField ]
290+ private Transform _positionalTransform ;
291+
292+ public Transform PositionalTransform { get ; set ; }
293+
294+
295+ [ SerializeField ]
296+ private Transform _rotationalTransform ;
297+
298+ public Vector3 _rotationOffset ;
299+
300+ public Transform RotationalTransform { get ; set ; }
301+
302+
303+ #endregion
304+
305+ #region Runtime Values
306+
307+ private bool _canSetVisualPosition ;
308+
309+ public bool CanSetVisualPosition {
310+ get {
311+ return _canSetVisualPosition ;
312+ }
313+ set {
314+ _canSetVisualPosition = value && PositionalTransform != null ;
315+ }
316+ }
317+
318+ private bool _canSetVisualRotation ;
319+
320+ public bool CanSetVisualRotation {
321+ get {
322+ return _canSetVisualRotation && RotationalTransform != null ;
323+ }
324+ set {
325+ _canSetVisualRotation = value ;
326+ }
327+ }
328+
329+ public Vector3d Position3d {
330+ get {
331+ return this . Position . ToVector3d ( this . HeightPos ) ;
332+ }
333+ }
334+
335+ private Vector2d [ ] RotatedPoints ;
336+
337+
338+ #endregion
339+
340+ }
341+
342+ }
0 commit comments