diff --git a/Client/Unity/GameObjectManager.cs b/Client/Unity/GameObjectManager.cs new file mode 100644 index 0000000..6c2ce3a --- /dev/null +++ b/Client/Unity/GameObjectManager.cs @@ -0,0 +1,99 @@ +using Newtonsoft.Json; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class GameObjectManager : MonoBehaviour +{ + + /* + This MonoBehaviour Synchronizes the position of All the game objects recieved from other clients + + + */ + // ID GAMEOBJECT+TYPE + Dictionary inSceneGameObjects = new Dictionary(); + + void Start() + { + + } + + public class RecievedData + { + /* + { + "gameObjects": //list of CSNCObjects + [ + { + "id": "uuid here", + "type": "razorblade", + "transform": { + "position": [ + 0.0, + 0.53, + 0.0 + ], + "rotation": [ + 0.0, + 0.0, + 0.0, + 1.0 + ] + } + }, + + ] + } + */ + public RecievedData(List gameObjects) + { + this.gameObjects = gameObjects; + } + public List gameObjects; + } + + public class GameObjectDataWrapper + { + public GameObject gameObject; + public string type; + public GameObjectDataWrapper(string type, GameObject gameObject) + { + this.type = type; + this.gameObject = gameObject; + } + } + public void SyncObjects(RecievedData data) + { + + //Debug.Log(JsonConvert.SerializeObject(data)); + foreach (CSNC.CSNCObject obj in data.gameObjects) + { + if (inSceneGameObjects.ContainsKey(obj.id)) //if in registry update + { + Quaternion rot = new Quaternion(obj.transform.rotation[0], obj.transform.rotation[1], obj.transform.rotation[2], obj.transform.rotation[3]); + Debug.Log(JsonConvert.SerializeObject(obj.transform.position)); + Vector3 pos = new Vector3(obj.transform.position[0], obj.transform.position[1], obj.transform.position[2]); + IEnumerator cor = LerpGameObjectFromFixedUpdate(inSceneGameObjects[obj.id].gameObject, rot, pos); + StartCoroutine(cor); + + } else //otherwise add to registry + { + inSceneGameObjects.Add(obj.id, new GameObjectDataWrapper(obj.type,Instantiate(GameObjectRegistry.instance.registry[obj.type]))); //Dubious ahh line of code + + GameObject g = inSceneGameObjects[obj.id].gameObject; + g.SetActive(true); + g.transform.position = new Vector3(obj.transform.position[0], obj.transform.position[1], obj.transform.position[2]); //these 2 might be even more dubious than the last + g.transform.rotation = new Quaternion(obj.transform.rotation[0], obj.transform.rotation[1], obj.transform.rotation[2], obj.transform.rotation[3]); + } + } + } + IEnumerator LerpGameObjectFromFixedUpdate(GameObject obj, Quaternion rot, Vector3 pos) + { + obj.transform.position = pos; + obj.transform.rotation = rot; + + yield return null; + } + +}