TrueSync
TrueSyncManager.cs
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
5 
6 namespace TrueSync {
10  [AddComponentMenu("")]
11  public class TrueSyncManager : MonoBehaviour {
12 
13  private const float JitterTimeFactor = 0.001f;
14 
15  private const string serverSettingsAssetFile = "TrueSyncGlobalConfig";
16 
17  private enum StartState { BEHAVIOR_INITIALIZED, FIRST_UPDATE, STARTED };
18 
19  private StartState startState;
20 
24  public GameObject[] playerPrefabs;
25 
26  public static TrueSyncConfig _TrueSyncGlobalConfig;
27 
28  public static TrueSyncConfig TrueSyncGlobalConfig {
29  get {
30  if (_TrueSyncGlobalConfig == null) {
31  _TrueSyncGlobalConfig = (TrueSyncConfig) Resources.Load(serverSettingsAssetFile, typeof(TrueSyncConfig));
32  }
33 
34  return _TrueSyncGlobalConfig;
35  }
36  }
37 
38  public static TrueSyncConfig TrueSyncCustomConfig = null;
39 
40  public TrueSyncConfig customConfig;
41 
45  private AbstractLockstep lockstep;
46 
47  private FP lockedTimeStep;
48 
52  private List<TrueSyncManagedBehaviour> generalBehaviours;
53 
57  private Dictionary<byte, List<TrueSyncManagedBehaviour>> behaviorsByPlayer;
58 
62  private CoroutineScheduler scheduler;
63 
67  private List<TrueSyncManagedBehaviour> queuedBehaviours = new List<TrueSyncManagedBehaviour>();
68 
69  private Dictionary<ITrueSyncBehaviour, TrueSyncManagedBehaviour> mapBehaviorToManagedBehavior = new Dictionary<ITrueSyncBehaviour, TrueSyncManagedBehaviour>();
70 
74  public static FP DeltaTime {
75  get {
76  if (instance == null) {
77  return 0;
78  }
79 
80  return instance.lockstep.deltaTime;
81  }
82  }
83 
87  public static FP Time {
88  get {
89  if (instance == null || instance.lockstep == null) {
90  return 0;
91  }
92 
93  return instance.lockstep.time;
94  }
95  }
96 
100  public static int Ticks {
101  get {
102  if (instance == null || instance.lockstep == null) {
103  return 0;
104  }
105 
106  return instance.lockstep.Ticks;
107  }
108  }
109 
113  public static int LastSafeTick {
114  get {
115  if (instance == null || instance.lockstep == null) {
116  return 0;
117  }
118 
119  return instance.lockstep.LastSafeTick;
120  }
121  }
122 
126  public static TSVector Gravity {
127  get {
128  if (instance == null) {
129  return TSVector.zero;
130  }
131 
132  return instance.ActiveConfig.gravity3D;
133  }
134  }
135 
139  public static List<TSPlayerInfo> Players {
140  get {
141  if (instance == null || instance.lockstep == null) {
142  return null;
143  }
144 
145  List<TSPlayerInfo> allPlayers = new List<TSPlayerInfo>();
146  foreach (TSPlayer tsp in instance.lockstep.Players.Values) {
147  if (!tsp.dropped) {
148  allPlayers.Add(tsp.playerInfo);
149  }
150  }
151 
152  return allPlayers;
153  }
154  }
155 
159  public static TSPlayerInfo LocalPlayer {
160  get {
161  if (instance == null || instance.lockstep == null) {
162  return null;
163  }
164 
165  return instance.lockstep.LocalPlayer.playerInfo;
166  }
167  }
168 
172  public static TrueSyncConfig Config {
173  get {
174  if (instance == null) {
175  return null;
176  }
177 
178  return instance.ActiveConfig;
179  }
180  }
181 
182  private static TrueSyncManager instance;
183 
184  private TrueSyncConfig ActiveConfig {
185  get {
186  if (TrueSyncCustomConfig != null) {
187  customConfig = TrueSyncCustomConfig;
188  TrueSyncCustomConfig = null;
189  }
190 
191  if (customConfig != null) {
192  return customConfig;
193  }
194 
195  return TrueSyncGlobalConfig;
196  }
197  }
198 
199  void Awake() {
200  TrueSyncConfig currentConfig = ActiveConfig;
201  lockedTimeStep = currentConfig.lockedTimeStep;
202 
203  StateTracker.Init();
204 
205  if (currentConfig.physics2DEnabled || currentConfig.physics3DEnabled) {
206  PhysicsManager.New(currentConfig);
207  PhysicsManager.instance.LockedTimeStep = lockedTimeStep;
208  PhysicsManager.instance.Init();
209  }
210  }
211 
212  void Start() {
213  instance = this;
214  Application.runInBackground = true;
215 
216  if (ReplayRecord.replayMode == ReplayMode.LOAD_REPLAY) {
217  ReplayRecord replayRecord = ReplayRecord.replayToLoad;
218  if (replayRecord == null) {
219  Debug.LogError("Replay Record can't be loaded");
220  gameObject.SetActive(false);
221  return;
222  }
223  }
224 
225  ICommunicator communicator = null;
226  if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) {
227  Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode.");
228  } else {
229  communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer);
230  }
231 
232  TrueSyncConfig activeConfig = ActiveConfig;
233 
234  lockstep = AbstractLockstep.NewInstance(
235  lockedTimeStep,
236  communicator,
238  activeConfig.syncWindow,
239  activeConfig.panicWindow,
240  activeConfig.rollbackWindow,
241  OnGameStarted,
242  OnGamePaused,
243  OnGameUnPaused,
244  OnGameEnded,
245  OnPlayerDisconnection,
246  OnStepUpdate,
247  GetLocalData
248  );
249 
250  if (activeConfig.showStats) {
251  this.gameObject.AddComponent<TrueSyncStats>().Lockstep = lockstep;
252  }
253 
254  scheduler = new CoroutineScheduler(lockstep);
255 
256  if (ReplayRecord.replayMode != ReplayMode.LOAD_REPLAY) {
257  if (communicator == null) {
258  lockstep.AddPlayer(0, "Local_Player", true);
259  } else {
260  List<PhotonPlayer> players = new List<PhotonPlayer>(PhotonNetwork.playerList);
261  players.Sort(UnityUtils.playerComparer);
262 
263  foreach (PhotonPlayer p in players) {
264  lockstep.AddPlayer((byte)p.ID, p.name, p.isLocal);
265  }
266  }
267  }
268 
269  generalBehaviours = new List<TrueSyncManagedBehaviour>();
270  foreach (TrueSyncBehaviour tsb in FindObjectsOfType<TrueSyncBehaviour>()) {
271  generalBehaviours.Add(NewManagedBehavior(tsb));
272  }
273 
274  initBehaviors();
275  initGeneralBehaviors(generalBehaviours, false);
276 
277  PhysicsManager.instance.OnRemoveBody(OnRemovedRigidBody);
278 
279  startState = StartState.BEHAVIOR_INITIALIZED;
280  }
281 
282  private TrueSyncManagedBehaviour NewManagedBehavior(ITrueSyncBehaviour trueSyncBehavior) {
283  TrueSyncManagedBehaviour result = new TrueSyncManagedBehaviour(trueSyncBehavior);
284  mapBehaviorToManagedBehavior[trueSyncBehavior] = result;
285 
286  return result;
287  }
288 
289  private void initBehaviors() {
290  behaviorsByPlayer = new Dictionary<byte, List<TrueSyncManagedBehaviour>>();
291 
292  foreach (TSPlayer p in lockstep.Players.Values) {
293  List<TrueSyncManagedBehaviour> behaviorsInstatiated = new List<TrueSyncManagedBehaviour>();
294 
295  foreach (GameObject prefab in playerPrefabs) {
296  GameObject prefabInst = Instantiate(prefab);
297  InitializeGameObject(prefabInst, prefabInst.transform.position.ToTSVector(), prefabInst.transform.rotation.ToTSQuaternion());
298 
299  TrueSyncBehaviour[] behaviours = prefabInst.GetComponentsInChildren<TrueSyncBehaviour>();
300  foreach (TrueSyncBehaviour behaviour in behaviours) {
301  behaviour.owner = p.playerInfo;
302  behaviour.localOwner = lockstep.LocalPlayer.playerInfo;
303  behaviour.numberOfPlayers = lockstep.Players.Count;
304 
305  behaviorsInstatiated.Add(NewManagedBehavior(behaviour));
306  }
307  }
308 
309  behaviorsByPlayer.Add(p.ID, behaviorsInstatiated);
310  }
311  }
312 
313  private void initGeneralBehaviors(IEnumerable<TrueSyncManagedBehaviour> behaviours, bool realOwnerId) {
314  List<TSPlayer> playersList = new List<TSPlayer>(lockstep.Players.Values);
315  List<TrueSyncManagedBehaviour> itemsToRemove = new List<TrueSyncManagedBehaviour>();
316 
317  foreach (TrueSyncManagedBehaviour tsmb in behaviours) {
318  if (!(tsmb.trueSyncBehavior is TrueSyncBehaviour)) {
319  continue;
320  }
321 
322  TrueSyncBehaviour bh = (TrueSyncBehaviour)tsmb.trueSyncBehavior;
323 
324  if (realOwnerId) {
325  bh.ownerIndex = bh.owner.Id;
326  } else {
327  if (bh.ownerIndex >= 0 && bh.ownerIndex < playersList.Count) {
328  bh.ownerIndex = playersList[bh.ownerIndex].ID;
329  }
330  }
331 
332  if (behaviorsByPlayer.ContainsKey((byte)bh.ownerIndex)) {
333  bh.owner = lockstep.Players[(byte)bh.ownerIndex].playerInfo;
334 
335  behaviorsByPlayer[(byte)bh.ownerIndex].Add(tsmb);
336  itemsToRemove.Add(tsmb);
337  } else {
338  bh.ownerIndex = -1;
339  }
340 
341  bh.localOwner = lockstep.LocalPlayer.playerInfo;
342  bh.numberOfPlayers = lockstep.Players.Count;
343  }
344 
345  foreach (TrueSyncManagedBehaviour bh in itemsToRemove) {
346  generalBehaviours.Remove(bh);
347  }
348  }
349 
350  private void CheckQueuedBehaviours() {
351  if (queuedBehaviours.Count > 0) {
352  generalBehaviours.AddRange(queuedBehaviours);
353  initGeneralBehaviors(queuedBehaviours, true);
354 
355  foreach (TrueSyncManagedBehaviour tsmb in queuedBehaviours) {
356  tsmb.SetGameInfo(lockstep.LocalPlayer.playerInfo, lockstep.Players.Count);
357  tsmb.OnSyncedStart();
358  }
359 
360  queuedBehaviours.Clear();
361  }
362  }
363 
364  void Update() {
365  if (lockstep != null && startState != StartState.STARTED) {
366  if (startState == StartState.BEHAVIOR_INITIALIZED) {
367  startState = StartState.FIRST_UPDATE;
368  } else if (startState == StartState.FIRST_UPDATE) {
369  lockstep.RunSimulation(true);
370  startState = StartState.STARTED;
371  }
372  }
373  }
374 
378  public static void RunSimulation() {
379  if (instance != null && instance.lockstep != null) {
380  instance.lockstep.RunSimulation(false);
381  }
382  }
383 
387  public static void PauseSimulation() {
388  if (instance != null && instance.lockstep != null) {
389  instance.lockstep.PauseSimulation();
390  }
391  }
392 
396  public static void EndSimulation() {
397  if (instance != null && instance.lockstep != null) {
398  instance.lockstep.EndSimulation();
399  }
400  }
401 
405  public static void UpdateCoroutines() {
406  if (instance != null && instance.lockstep != null) {
407  instance.scheduler.UpdateAllCoroutines();
408  }
409  }
410 
416  public static void SyncedStartCoroutine(IEnumerator coroutine) {
417  if (instance != null && instance.lockstep != null) {
418  instance.scheduler.StartCoroutine(coroutine);
419  }
420  }
421 
427  public static GameObject SyncedInstantiate(GameObject prefab) {
428  return SyncedInstantiate(prefab, prefab.transform.position.ToTSVector(), prefab.transform.rotation.ToTSQuaternion());
429  }
430 
438  public static GameObject SyncedInstantiate(GameObject prefab, TSVector position, TSQuaternion rotation) {
439  if (instance != null && instance.lockstep != null && (prefab.GetComponentInChildren<TSTransform>() != null || prefab.GetComponentInChildren<TSTransform2D>() != null)) {
440  GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject;
441 
442  foreach (MonoBehaviour bh in go.GetComponentsInChildren<MonoBehaviour>()) {
443  if (bh is ITrueSyncBehaviour) {
444  instance.queuedBehaviours.Add(instance.NewManagedBehavior((ITrueSyncBehaviour)bh));
445  }
446  }
447 
448  InitializeGameObject(go, position, rotation);
449 
450  return go;
451  }
452 
453  return null;
454  }
455 
456  private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation) {
457  ICollider[] tsColliders = go.GetComponentsInChildren<ICollider>();
458  if (tsColliders != null) {
459  foreach (ICollider tsCollider in tsColliders) {
460  PhysicsManager.instance.AddBody(tsCollider);
461  }
462  }
463 
464  TSTransform rootTSTransform = go.GetComponent<TSTransform>();
465  if (rootTSTransform != null) {
466  rootTSTransform.Initialize();
467 
468  rootTSTransform.position = position;
469  rootTSTransform.rotation = rotation;
470  }
471 
472  TSTransform[] tsTransforms = go.GetComponentsInChildren<TSTransform>();
473  if (tsTransforms != null) {
474  foreach (TSTransform tsTransform in tsTransforms) {
475  if (tsTransform != rootTSTransform) {
476  tsTransform.Initialize();
477  }
478  }
479  }
480 
481  TSTransform2D rootTSTransform2D = go.GetComponent<TSTransform2D>();
482  if (rootTSTransform2D != null) {
483  rootTSTransform2D.Initialize();
484 
485  rootTSTransform2D.position = new TSVector2(position.x, position.y);
486  rootTSTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z;
487  }
488 
489  TSTransform2D[] tsTransforms2D = go.GetComponentsInChildren<TSTransform2D>();
490  if (tsTransforms2D != null) {
491  foreach (TSTransform2D tsTransform2D in tsTransforms2D) {
492  if (tsTransform2D != rootTSTransform2D) {
493  tsTransform2D.Initialize();
494  }
495  }
496  }
497  }
498 
506  public static GameObject SyncedInstantiate(GameObject prefab, TSVector2 position, TSQuaternion rotation) {
507  return SyncedInstantiate(prefab, new TSVector(position.x, position.y, 0), rotation);
508  }
509 
517  public static void SyncedDestroy(GameObject gameObject) {
518  if (instance != null && instance.lockstep != null) {
519  SyncedDisableBehaviour(gameObject);
520 
521  TSCollider[] tsColliders = gameObject.GetComponentsInChildren<TSCollider>();
522  if (tsColliders != null) {
523  foreach (TSCollider tsCollider in tsColliders) {
524  DestroyTSRigidBody(tsCollider.gameObject, tsCollider.Body);
525  }
526  }
527 
528  TSCollider2D[] tsColliders2D = gameObject.GetComponentsInChildren<TSCollider2D>();
529  if (tsColliders2D != null) {
530  foreach (TSCollider2D tsCollider2D in tsColliders2D) {
531  DestroyTSRigidBody(tsCollider2D.gameObject, tsCollider2D.Body);
532  }
533  }
534  }
535  }
536 
540  public static void SyncedDisableBehaviour(GameObject gameObject) {
541  foreach (MonoBehaviour tsb in gameObject.GetComponentsInChildren<MonoBehaviour>()) {
542  if (tsb is ITrueSyncBehaviour && instance.mapBehaviorToManagedBehavior.ContainsKey((ITrueSyncBehaviour)tsb)) {
543  instance.mapBehaviorToManagedBehavior[(ITrueSyncBehaviour)tsb].disabled = true;
544  }
545  }
546  }
547 
553  private static void DestroyTSRigidBody(GameObject tsColliderGO, IBody body) {
554  tsColliderGO.gameObject.SetActive(false);
555  instance.lockstep.Destroy(body);
556  }
557 
563  public static void RegisterITrueSyncBehaviour(ITrueSyncBehaviour trueSyncBehaviour) {
564  if (instance != null && instance.lockstep != null) {
565  instance.queuedBehaviours.Add(instance.NewManagedBehavior(trueSyncBehaviour));
566  }
567  }
568 
574  public static void RegisterIsReadyChecker(TrueSyncIsReady IsReadyChecker) {
575  if (instance != null && instance.lockstep != null) {
576  instance.lockstep.GameIsReady += IsReadyChecker;
577  }
578  }
579 
585  public static void RemovePlayer(int playerId) {
586  if (instance != null && instance.lockstep != null) {
587  foreach (TrueSyncManagedBehaviour tsmb in instance.behaviorsByPlayer[(byte)playerId]) {
588  tsmb.disabled = true;
589 
590  TSCollider[] tsColliders = ((TrueSyncBehaviour)tsmb.trueSyncBehavior).gameObject.GetComponentsInChildren<TSCollider>();
591  if (tsColliders != null) {
592  foreach (TSCollider tsCollider in tsColliders) {
593  if (!tsCollider.Body.TSDisabled) {
594  DestroyTSRigidBody(tsCollider.gameObject, tsCollider.Body);
595  }
596  }
597  }
598 
599  TSCollider2D[] tsCollider2Ds = ((TrueSyncBehaviour)tsmb.trueSyncBehavior).gameObject.GetComponentsInChildren<TSCollider2D>();
600  if (tsCollider2Ds != null) {
601  foreach (TSCollider2D tsCollider2D in tsCollider2Ds) {
602  if (!tsCollider2D.Body.TSDisabled) {
603  DestroyTSRigidBody(tsCollider2D.gameObject, tsCollider2D.Body);
604  }
605  }
606  }
607  }
608  }
609  }
610 
611  private FP tsDeltaTime = 0;
612 
613  void FixedUpdate() {
614  if (lockstep != null) {
615  tsDeltaTime += UnityEngine.Time.deltaTime;
616 
617  if (tsDeltaTime >= (lockedTimeStep - JitterTimeFactor)) {
618  tsDeltaTime = 0;
619 
620  instance.scheduler.UpdateAllCoroutines();
621  lockstep.Update();
622  }
623  }
624  }
625 
626  void GetLocalData(InputData playerInputData) {
627  TrueSyncInput.CurrentInputData = playerInputData;
628 
629  if (behaviorsByPlayer.ContainsKey(playerInputData.ownerID)) {
630  foreach (TrueSyncManagedBehaviour bh in behaviorsByPlayer[playerInputData.ownerID]) {
631  if (bh != null && !bh.disabled) {
632  bh.OnSyncedInput();
633  }
634  }
635  }
636 
637  TrueSyncInput.CurrentInputData = null;
638  }
639 
640  void OnStepUpdate(InputData[] allInputData) {
641  TrueSyncInput.GetAllInputs().Clear();
642 
643  if (generalBehaviours != null) {
644  foreach (TrueSyncManagedBehaviour bh in generalBehaviours) {
645  if (bh != null && !bh.disabled) {
646  bh.OnPreSyncedUpdate();
647  instance.scheduler.UpdateAllCoroutines();
648  }
649  }
650  }
651 
652  foreach (InputData playerInputData in allInputData) {
653  if (behaviorsByPlayer.ContainsKey(playerInputData.ownerID)) {
654  foreach (TrueSyncManagedBehaviour bh in behaviorsByPlayer[playerInputData.ownerID]) {
655  if (bh != null && !bh.disabled) {
656  bh.OnPreSyncedUpdate();
657  instance.scheduler.UpdateAllCoroutines();
658  }
659  }
660  }
661  }
662 
663  TrueSyncInput.GetAllInputs().AddRange(allInputData);
664 
665  TrueSyncInput.CurrentSimulationData = null;
666  if (generalBehaviours != null) {
667  foreach (TrueSyncManagedBehaviour bh in generalBehaviours) {
668  if (bh != null && !bh.disabled) {
669  bh.OnSyncedUpdate();
670  instance.scheduler.UpdateAllCoroutines();
671  }
672  }
673  }
674 
675  foreach (InputData playerInputData in allInputData) {
676  if (behaviorsByPlayer.ContainsKey(playerInputData.ownerID)) {
677  TrueSyncInput.CurrentSimulationData = playerInputData;
678 
679  foreach (TrueSyncManagedBehaviour bh in behaviorsByPlayer[playerInputData.ownerID]) {
680  if (bh != null && !bh.disabled) {
681  bh.OnSyncedUpdate();
682  instance.scheduler.UpdateAllCoroutines();
683  }
684  }
685  }
686 
687  TrueSyncInput.CurrentSimulationData = null;
688  }
689 
690  CheckQueuedBehaviours();
691  }
692 
693  private void OnRemovedRigidBody(IBody body) {
694  GameObject go = PhysicsManager.instance.GetGameObject(body);
695 
696  if (go != null) {
697  List<TrueSyncBehaviour> behavioursToRemove = new List<TrueSyncBehaviour>(go.GetComponentsInChildren<TrueSyncBehaviour>());
698  RemoveFromTSMBList(queuedBehaviours, behavioursToRemove);
699  RemoveFromTSMBList(generalBehaviours, behavioursToRemove);
700 
701  foreach (List<TrueSyncManagedBehaviour> listBh in behaviorsByPlayer.Values) {
702  RemoveFromTSMBList(listBh, behavioursToRemove);
703  }
704  }
705  }
706 
707  private void RemoveFromTSMBList(List<TrueSyncManagedBehaviour> tsmbList, List<TrueSyncBehaviour> behaviours) {
708  List<TrueSyncManagedBehaviour> toRemove = new List<TrueSyncManagedBehaviour>();
709  foreach (TrueSyncManagedBehaviour tsmb in tsmbList) {
710  if ((tsmb.trueSyncBehavior is TrueSyncBehaviour) && behaviours.Contains((TrueSyncBehaviour)tsmb.trueSyncBehavior)) {
711  toRemove.Add(tsmb);
712  }
713  }
714 
715  foreach (TrueSyncManagedBehaviour tsmb in toRemove) {
716  tsmbList.Remove(tsmb);
717  }
718  }
719 
720  void OnPlayerDisconnection(byte playerId) {
721  GenericOnGameCall("TrueSyncManagedBehaviour.OnPlayerDisconnection", new object[] { (int)playerId });
722  }
723 
724  void OnGameStarted() {
725  GenericOnGameCall("TrueSyncManagedBehaviour.OnSyncedStart");
726  CheckQueuedBehaviours();
727  }
728 
729  void OnGamePaused() {
730  GenericOnGameCall("TrueSyncManagedBehaviour.OnGamePaused");
731  }
732 
733  void OnGameUnPaused() {
734  GenericOnGameCall("TrueSyncManagedBehaviour.OnGameUnPaused");
735  }
736 
737  void OnGameEnded() {
738  GenericOnGameCall("TrueSyncManagedBehaviour.OnGameEnded");
739  }
740 
741  void GenericOnGameCall(string callbackName) {
742  GenericOnGameCall(callbackName, null);
743  }
744 
745  void GenericOnGameCall(string callbackName, object[] parameter) {
746  if (generalBehaviours != null) {
747  foreach (TrueSyncManagedBehaviour bh in generalBehaviours) {
748  UnityUtils.methodInfoByName[callbackName].Invoke(bh, parameter);
749  instance.scheduler.UpdateAllCoroutines();
750  }
751  }
752 
753  foreach (List<TrueSyncManagedBehaviour> behaviors in behaviorsByPlayer.Values) {
754  foreach (TrueSyncManagedBehaviour bh in behaviors) {
755  UnityUtils.methodInfoByName[callbackName].Invoke(bh, parameter);
756  instance.scheduler.UpdateAllCoroutines();
757  }
758  }
759  }
760 
761  void OnApplicationQuit() {
762  EndSimulation();
763  }
764 
765  }
766 
767 }
int rollbackWindow
Rollback window size.
Manages creation of player prefabs and lockstep execution.
FP y
The Y component of the vector.
Definition: TSVector.cs:39
TSPlayerInfo owner
Basic info about the owner of this behaviour.
TSPlayerInfo localOwner
Basic info about the local player.
static void UpdateCoroutines()
Update all coroutines created.
static void RegisterITrueSyncBehaviour(ITrueSyncBehaviour trueSyncBehaviour)
Registers an implementation of ITrueSyncBehaviour to be included in the simulation.
static List< TSPlayerInfo > Players
Returns the list of players connected.
Provides a few utilities to be used on TrueSync exposed classes.
Definition: UnityUtils.cs:10
GameObject[] playerPrefabs
Player prefabs to be instantiated in each machine.
A deterministic version of Unity&#39;s Transform component for 2D physics.
Definition: TSTransform2D.cs:9
static FP Time
Returns the time elapsed since the beginning of the simulation.
static Dictionary< string, MethodInfo > methodInfoByName
A few MethodInfo dictionary to allow reusable method calls.
Definition: UnityUtils.cs:63
static void RemovePlayer(int playerId)
Removes objets related to a provided player.
static int LastSafeTick
Returns the last safe simulated tick.
TSVector2 position
Property access to position.
static void RegisterIsReadyChecker(TrueSyncIsReady IsReadyChecker)
Register a TrueSyncIsReady delegate to that returns true if the game can proceed or false otherwise...
A deterministic version of Unity&#39;s Transform component for 3D physics.
Definition: TSTransform.cs:9
Represents each player&#39;s behaviour simulated on every machine connected to the game.
static IPhysicsManager instance
Returns a proper implementation of IPhysicsManager.
A vector structure.
Definition: TSVector.cs:29
static TrueSyncConfig Config
Returns the active TrueSyncConfig used by the TrueSyncManager.
static PlayerComparer playerComparer
Instance of a PlayerComparer.
Definition: UnityUtils.cs:26
Truesync&#39;s ICommunicator implementation based on PUN.
TSQuaternion rotation
Property access to rotation.
Definition: TSTransform.cs:50
bool physics3DEnabled
Indicates if the 3D physics engine should be enabled.
FP rotation
Property access to rotation.
static readonly TSVector zero
A vector with components (0,0,0);
Definition: TSVector.cs:47
TSVector gravity3D
Represents the simulated gravity.
static TSPlayerInfo LocalPlayer
Returns the local player.
FP z
The Z component of the vector.
Definition: TSVector.cs:41
static IPhysicsManager New(TrueSyncConfig trueSyncConfig)
Instantiates a new IPhysicsManager.
static int Ticks
Returns the number of the last simulated tick.
Abstract collider for 3D shapes.
Definition: TSCollider.cs:12
Represents a set of configurations for TrueSync.
static TSVector Gravity
Returns the simulated gravity.
static void SyncedStartCoroutine(IEnumerator coroutine)
Starts a new coroutine.
int numberOfPlayers
Number of players connected to the game.
bool TSDisabled
If true the body doesn&#39;t interfere in physics simulation.
Definition: IBody.cs:13
int panicWindow
Maximum number of ticks to wait until all other players inputs arrive.
bool showStats
When true shows a debug interface with a few information.
TSVector position
Property access to position.
Definition: TSTransform.cs:23
void Initialize()
Initializes internal properties based on whether there is a TSCollider2D attached.
void Initialize()
Initializes internal properties based on whether there is a TSCollider attached.
Definition: TSTransform.cs:314
FP lockedTimeStep
Time between each TrueSync&#39;s frame.
TrueSync&#39;s communicator interface.
Definition: ICommunicator.cs:9
Manages physics simulation.
static GameObject SyncedInstantiate(GameObject prefab)
Instantiate a new prefab in a deterministic way.
static void PauseSimulation()
Pauses the game simulation.
FP x
The X component of the vector.
Definition: TSVector.cs:37
int ownerIndex
Index of the owner at initial players list.
static GameObject SyncedInstantiate(GameObject prefab, TSVector position, TSQuaternion rotation)
Instantiates a new prefab in a deterministic way.
IBody3D Body
Returns the body linked to this collider.
Definition: TSCollider.cs:94
A Quaternion representing an orientation.
Definition: TSQuaternion.cs:29
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:11
static FP DeltaTime
Returns the deltaTime between two simulation calls.
static void RunSimulation()
Run/Unpause the game simulation.
bool physics2DEnabled
Indicates if the 2D physics engine should be enabled.
static void EndSimulation()
End the game simulation.
int syncWindow
Synchronization window size.
IBody2D Body
Returns RigidBody associated to this TSRigidBody.
Definition: TSCollider2D.cs:47
static void SyncedDisableBehaviour(GameObject gameObject)
Disables &#39;OnSyncedInput&#39; and &#39;OnSyncUpdate&#39; calls to every ITrueSyncBehaviour attached.
Represents a common interface to 2D and 3D bodies.
Definition: IBody.cs:6
static void SyncedDestroy(GameObject gameObject)
Destroys a GameObject in a deterministic way.
static GameObject SyncedInstantiate(GameObject prefab, TSVector2 position, TSQuaternion rotation)
Instantiates a new prefab in a deterministic way.