TrueSync
TSTransform2D.cs
1 using UnityEngine;
2 
3 namespace TrueSync {
4 
8  [ExecuteInEditMode]
9  public class TSTransform2D : MonoBehaviour {
10 
11  private const float DELTA_TIME_FACTOR = 10f;
12 
13  [SerializeField]
14  [HideInInspector]
15  [AddTracking]
16  private TSVector2 _position;
17 
23  public TSVector2 position {
24  get {
25  if (tsCollider != null && tsCollider.Body != null) {
26  return tsCollider.Body.TSPosition - scaledCenter;
27  }
28 
29  return _position;
30  }
31  set {
32  _position = value;
33 
34  if (tsCollider != null && tsCollider.Body != null) {
35  tsCollider.Body.TSPosition = _position + scaledCenter;
36  }
37  }
38  }
39 
40  [SerializeField]
41  [HideInInspector]
42  [AddTracking]
43  private FP _rotation;
44 
50  public FP rotation {
51  get {
52  if (tsCollider != null && tsCollider.Body != null) {
53  return tsCollider.Body.TSOrientation * FP.Rad2Deg;
54  }
55 
56  return _rotation;
57  }
58  set {
59  _rotation = value;
60 
61  if (tsCollider != null && tsCollider.Body != null) {
62  tsCollider.Body.TSOrientation = _rotation * FP.Deg2Rad;
63  }
64  }
65  }
66 
67  [SerializeField]
68  [HideInInspector]
69  [AddTracking]
70  private TSVector _scale;
71 
75  public TSVector scale {
76  get {
77  return _scale;
78  }
79  set {
80  _scale = value;
81  }
82  }
83 
84  [SerializeField]
85  [HideInInspector]
86  private bool _serialized;
87 
88  private TSVector2 scaledCenter {
89  get {
90  if (tsCollider != null) {
91  return tsCollider.ScaledCenter;
92  }
93 
94  return TSVector2.zero;
95  }
96  }
97 
98  [HideInInspector]
99  public TSCollider2D tsCollider;
100 
101  [HideInInspector]
102  public TSTransform2D tsParent;
103 
104  private bool initialized = false;
105 
106  private TSRigidBody2D rb;
107 
108  public void Start() {
109  if (!Application.isPlaying) {
110  return;
111  }
112 
113  Initialize();
114  rb = GetComponent<TSRigidBody2D> ();
115  }
116 
120  public void Initialize() {
121  if (initialized) {
122  return;
123  }
124 
125  tsCollider = GetComponent<TSCollider2D>();
126  if (transform.parent != null) {
127  tsParent = transform.parent.GetComponent<TSTransform2D>();
128  }
129 
130  if (!_serialized) {
131  UpdateEditMode();
132  }
133 
134  if (tsCollider != null) {
135  if (tsCollider.IsBodyInitialized) {
136  tsCollider.Body.TSPosition = _position + scaledCenter;
137  tsCollider.Body.TSOrientation = _rotation * FP.Deg2Rad;
138  }
139  } else {
140  StateTracker.AddTracking(this);
141  }
142 
143  initialized = true;
144  }
145 
146  public void Update() {
147  if (Application.isPlaying) {
148  if (initialized) {
149  UpdatePlayMode();
150  }
151  } else {
152  UpdateEditMode();
153  }
154  }
155 
156  private void UpdateEditMode() {
157  if (transform.hasChanged) {
158  _position = transform.position.ToTSVector2();
159  _rotation = transform.rotation.eulerAngles.z;
160  _scale = transform.localScale.ToTSVector();
161 
162  _serialized = true;
163  }
164  }
165 
166  private void UpdatePlayMode() {
167  if (rb != null) {
168  if (rb.interpolation == TSRigidBody2D.InterpolateMode.Interpolate) {
169  transform.position = Vector3.Lerp(transform.position, position.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR);
170  transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, rotation.AsFloat()), Time.deltaTime * DELTA_TIME_FACTOR);
171  transform.localScale = Vector3.Lerp(transform.localScale, scale.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR);
172  return;
173  } else if (rb.interpolation == TSRigidBody2D.InterpolateMode.Extrapolate) {
174  transform.position = (position + tsCollider.Body.TSLinearVelocity * Time.deltaTime * DELTA_TIME_FACTOR).ToVector();
175  transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, rotation.AsFloat()), Time.deltaTime * DELTA_TIME_FACTOR);
176  transform.localScale = Vector3.Lerp(transform.localScale, scale.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR);
177  return;
178  }
179  }
180 
181  transform.position = position.ToVector();
182 
183  Quaternion rot = transform.rotation;
184  rot.eulerAngles = new Vector3(0, 0, rotation.AsFloat());
185  transform.rotation = rot;
186 
187  transform.localScale = scale.ToVector();
188  }
189 
190  }
191 
192 }
InterpolateMode interpolation
Interpolation mode that should be used.
A deterministic version of Unity&#39;s Transform component for 2D physics.
Definition: TSTransform2D.cs:9
bool IsBodyInitialized
Returns true if the body was already initialized.
TSVector2 position
Property access to position.
A vector structure.
Definition: TSVector.cs:29
FP rotation
Property access to rotation.
void Initialize()
Initializes internal properties based on whether there is a TSCollider2D attached.
TSVector2 TSPosition
Set/get body&#39;s position.
Definition: IBody2D.cs:11
Represents a physical 2D rigid body.
TSVector2 ScaledCenter
Returns a version of collider&#39;s center scaled by parent&#39;s transform.
Definition: TSCollider2D.cs:72
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:11
IBody2D Body
Returns RigidBody associated to this TSRigidBody.
Definition: TSCollider2D.cs:47
TSVector2 TSLinearVelocity
Set/get body&#39;s linear velocity.
Definition: IBody2D.cs:39
FP TSOrientation
Set/get body&#39;s orientation.
Definition: IBody2D.cs:18
TSVector scale
Property access to scale.