100 lines
3.4 KiB
C#
100 lines
3.4 KiB
C#
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<string, GameObjectDataWrapper> inSceneGameObjects = new Dictionary<string, GameObjectDataWrapper>();
|
|
|
|
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<CSNC.CSNCObject> gameObjects)
|
|
{
|
|
this.gameObjects = gameObjects;
|
|
}
|
|
public List<CSNC.CSNCObject> 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;
|
|
}
|
|
|
|
}
|