public Part InitializeNetworkedPart(int tempID, int id = -1) { if (GlobalReferences.TemplateParts.Count == 0) { return(null); } GameObject template = GlobalReferences.TemplateParts[tempID]; gameObject.AddComponent <MeshRenderer>().material = MaterialHolder.UnaffectedMat; gameObject.AddComponent <MeshFilter>().sharedMesh = template.GetComponent <MeshFilter>().sharedMesh; part = gameObject.AddComponent <Part>(); MeshCollider[] cols = template.GetComponents <MeshCollider>(); foreach (MeshCollider mc in cols) { MeshCollider newMc = gameObject.AddComponent <MeshCollider>(); newMc.convex = true; newMc.sharedMesh = mc.sharedMesh; } PartsHolder.ResetPart(gameObject, GlobalReferences.TemplateParts[tempID], id); gameObject.layer = 8; GlobalReferences.Parts.Add(gameObject); GlobalReferences.FreeParts.Add(gameObject); return(part); }
//Placement methods #region private void SpawnObject(int id) { GameObject go = PartsHolder.SpawnPart(id); GlobalReferences.FreeParts.Remove(go); carryGo[id] = go; go.GetComponent <MeshRenderer>().material = frozenMat; go.GetComponent <Rigidbody>().isKinematic = true; go.transform.parent = gameObject.transform; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.Euler(Vector3.zero); }
private void SpawnObject(int id) { GameObject go = Instantiate(GlobalReferences.TemplateParts[id]); PartsHolder.ResetPart(go, GlobalReferences.TemplateParts[id], -1); carryGo[id] = go; go.GetComponent <MeshRenderer>().material = MaterialHolder.FrozenMat; go.GetComponent <Rigidbody>().isKinematic = true; go.transform.parent = target.transform; go.SetActive(false); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; }
public PartsHolder(int _numParts, string loadPath, byte[] loadData = null) { holder = this; InitializeGameArea initArea = new InitializeGameArea(); Environment environment; if (loadPath != null) { environment = ImplementWasp.Initialize(loadPath); } else { environment = ImplementWasp.Initialize(null, loadData); } minX = environment.GameArea.minX; maxX = environment.GameArea.maxX; minY = environment.GameArea.minZ; maxY = environment.GameArea.maxZ; minZ = environment.GameArea.minY; maxZ = environment.GameArea.maxY; TIExportField.Resolution = environment.FieldResolution; GlobalReferences.TemplateParts = ImplementWasp.Load(); Debug.Log("Template Parts:" + GlobalReferences.TemplateParts.Count + " Parts were loaded"); floor = GameObject.Find("Floor"); floor.transform.position = new Vector3(0, minY, 0); floor.SetActive(environment.GroundPlane); GameObject areaProxy = GameObject.Find("GameAreaProxy"); if (areaProxy != null) { areaProxy.GetComponent <GameAreaProxy>().Initialize(new Region(minX, maxX, minY, maxY, minZ, maxZ)); } initArea.Initialize(minX, maxX, minY, maxY, minZ, maxZ, ConnectionScanning.ConnectionThreshold * 1.5f, ComputeColGridSize()); numParts = _numParts; SpawnMultiple(NumParts); }
private void Awake() { partsHolder = gameObject.GetComponent <PartsHolder>(); initArea = gameObject.GetComponent <InitializeGameArea>(); LoadJson(Application.dataPath + "/Resources/config.json"); initArea.Initialize(minX, maxX, minY, maxY, minZ, maxZ, ConRes, ColRes); partsHolder.Initialize(minX, maxX, minY, maxY, minZ, maxZ, LoadLoc, NumParts); ConnectionScanning.ConnectionThreshold = SnapThresh; SaveLoadTool.SavePath = SaveLoc; InitializeController(VrFps); }
private void RealizeConnection() { if (lastDistAngle < connectionThreshold && ConnectionVoxelContainer.RevealConnections(bestOnPart).Contains(closestConnection)) { Vector3 pos = gameObject.transform.position; Quaternion rot = gameObject.transform.rotation; AlignPlane.Orient(bestOnPart.Pln, closestConnection.Pln, gameObject); if (!CollisionDetection()) { Part p = gameObject.GetComponent <Part>(); p.FreezePart(); p.Parent = closestConnection.ParentPart.ID; p.ParentCon = closestConnection.ParentPart.Connections.IndexOf(closestConnection); ConnectionVoxelContainer.RemoveConnection(closestConnection); ConnectionVoxelContainer.RemoveConnection(bestOnPart); bestOnPart.ParentPart.SetInactive(bestOnPart); closestConnection.ParentPart.SetInactive(closestConnection); closestConnection.ParentPart.ChildCons.Add(bestOnPart.ParentPart.Connections.IndexOf(bestOnPart)); closestConnection.ParentPart.Children.Add((int)p.ID); GameObject _g = PartsHolder.SpawnPart(p.TemplateID); _g.SetActive(true); if (GlobalReferences.PlacementType == PlacementTypeTool.PlaceChoreo.Choreo) { GlobalReferences.AffectPart(_g); GlobalReferences.FreeParts.Remove(_g); } ConnectionScanningHandler handler = gameObject.GetComponent <ConnectionScanningHandler>(); if (handler != null) { handler.TerminateConnection(); } } else { gameObject.transform.position = pos; gameObject.transform.rotation = rot; } } }
private static void NewGame() { List <GameObject> tempGos = new List <GameObject>(); tempGos.AddRange(GlobalReferences.FreeParts); tempGos.AddRange(GlobalReferences.AffectedParts); foreach (GameObject go in GlobalReferences.FrozenParts.Values) { tempGos.Add(go); } GlobalReferences.FreeParts.Clear(); GlobalReferences.FrozenParts.Clear(); GlobalReferences.AffectedParts.Clear(); GlobalReferences.NumOfParts = 0; for (int i = tempGos.Count - 1; i >= 0; --i) { Destroy(tempGos[i]); } PartsHolder.SpawnMultiple(PartsHolder.NumParts); }
//Loading public static void Load(string path) { JsonAssembly assembly; string jsonSr; int numParts = 0; Matrix4x4 transM = new Matrix4x4(new Vector4(1, 0, 0, 0), new Vector4(0, 0, 1, 0), new Vector4(0, 1, 0, 0), new Vector4(0, 0, 0, 1)); using (FileStream stream = new FileStream(path, FileMode.Open)) { using (StreamReader sr = new StreamReader(stream)) { jsonSr = sr.ReadToEnd(); } } assembly = JsonConvert.DeserializeObject <JsonAssembly>(jsonSr); foreach (KeyValuePair <string, JsonPart> dicEntry in assembly.parts) { int id = int.Parse(dicEntry.Key) + GlobalReferences.NumOfParts; JsonPart part = dicEntry.Value; int templateId = GlobalReferences.TemplateIDFromName(part.name); if (templateId == -1) { throw new System.Exception("Couldn't find Part from Name"); } GameObject go = MonoBehaviour.Instantiate(GlobalReferences.TemplateParts[templateId]); go.name = part.name + "_" + (id + GlobalReferences.NumOfParts); go.SetActive(true); PartsHolder.ResetPart(go, GlobalReferences.TemplateParts[templateId], id + GlobalReferences.NumOfParts); Part p = go.GetComponent <Part>(); foreach (int child in part.children) { p.Children.Add(child + GlobalReferences.NumOfParts); } if (part.parent != null) { p.Parent = part.parent + GlobalReferences.NumOfParts; } else { p.Parent = null; } p.ParentCon = part.parentCon; p.ChildCons = part.childCons; p.ActiveConnections = part.activeConnections; Matrix4x4 m = part.transform.GetMatrix(); m = transM * m; go.transform.localScale = JsonTransform.MatrixToScale(m); go.transform.rotation = JsonTransform.MatrixToRotation(m); go.transform.position = JsonTransform.MatrixToPosition(m); /* * go.transform.localScale = JsonTransform.MatrixToScale(m); * go.transform.rotation = JsonTransform.MatrixToRotation(m); * go.transform.position = JsonTransform.MatrixToPosition(m); * go.transform.RotateAround(Vector3.zero, Vector3.right, -90); * GameObject _go = new GameObject(); * go.transform.SetParent(_go.transform); * _go.transform.localScale = new Vector3(1, 1, -1); * go.transform.SetParent(null); * MonoBehaviour.Destroy(_go); */ p.FreezePart(id); CollisionVoxelContainer.StoreGameObject(go); if (!GlobalReferences.Parts.Contains(go)) { GlobalReferences.Parts.Add(go); } if (id >= numParts) { numParts = id + GlobalReferences.NumOfParts + 1; } } GlobalReferences.NumOfParts = numParts; }
public static void LoadFromString(string jsonSr) { Dictionary <int, int> localPartIDLedger = new Dictionary <int, int>(); Dictionary <int, PartSpawnData> spawnPartContainer = new Dictionary <int, PartSpawnData>(); Dictionary <int, List <int> > childContainer = new Dictionary <int, List <int> >(); Dictionary <int, List <int> > childConContainer = new Dictionary <int, List <int> >(); JsonAssembly assembly; int numParts = 0; Matrix4x4 transM = new Matrix4x4(new Vector4(1, 0, 0, 0), new Vector4(0, 0, 1, 0), new Vector4(0, 1, 0, 0), new Vector4(0, 0, 0, 1)); assembly = JsonConvert.DeserializeObject <JsonAssembly>(jsonSr); foreach (KeyValuePair <string, JsonPart> dicEntry in assembly.parts) { int id; if (!BoltNetwork.IsRunning) { id = int.Parse(dicEntry.Key) + GlobalReferences.NumOfParts; } else { id = int.Parse(dicEntry.Key); int newID = Random.Range(int.MinValue, int.MaxValue); while (localPartIDLedger.ContainsValue(newID) || GlobalReferences.FrozenParts.ContainsKey(newID)) { newID = Random.Range(int.MinValue, int.MaxValue); } localPartIDLedger.Add(id, newID); } JsonPart part = dicEntry.Value; int templateId = GlobalReferences.TemplateIDFromName(part.name); if (templateId == -1) { throw new System.Exception("Couldn't find Part from Name"); } Matrix4x4 m = part.transform.GetMatrix(); m = m * Matrix4x4.TRS(-GlobalReferences.TemplateParts[templateId].GetComponent <Part>().PartOffset, Quaternion.identity, Vector3.one); m = transM * m; Vector3 scale = JsonTransform.MatrixToScale(m); Quaternion rot = JsonTransform.MatrixToRotation(m); Vector3 pos = JsonTransform.MatrixToPosition(m); string name = part.name + "_" + (id); int parent = -1; if (part.parent != null) { parent = (int)part.parent; parent += GlobalReferences.NumOfParts; } int parentCon = -1; if (part.parentCon != null) { parentCon = (int)part.parentCon; } int con = -1; if (part.conToParent != null) { con = (int)part.conToParent; } if (BoltNetwork.IsRunning) { var token = new PartTokenParent(); token.TemplateID = templateId; token.ID = id; token.Parent = -1; if (part.parent != null) { token.Parent = (int)part.parent; } token.ParentCon = parentCon; token.Con = con; int owner = 0; if (BoltNetwork.IsClient) { owner = (int)BoltNetwork.Server.ConnectionId; } spawnPartContainer.Add(id, new PartSpawnData(token, pos, rot, owner)); } else { GameObject go = MonoBehaviour.Instantiate(GlobalReferences.TemplateParts[templateId]); go.name = name; go.SetActive(true); PartsHolder.ResetPart(go, GlobalReferences.TemplateParts[templateId], id); Part p = go.GetComponent <Part>(); p.Parent = parent; p.ParentCon = parentCon; p.ConToParent = con; go.transform.rotation = rot; go.transform.position = pos; p.FreezePart(id); if (!GlobalReferences.Parts.Contains(go)) { GlobalReferences.Parts.Add(go); } if (id >= numParts) { numParts = id + 1; } } } if (BoltNetwork.IsRunning) { foreach (PartSpawnData spawnData in spawnPartContainer.Values) { //PartTokenParent token = (PartTokenParent)spawnData.Token; if (spawnData.parent != -1 && localPartIDLedger.ContainsKey(spawnData.parent)) { spawnData.parent = localPartIDLedger[spawnData.parent]; } spawnData.id = localPartIDLedger[spawnData.id]; NetworkPartSpawner.LoadData.data.Add(spawnData); } } else { foreach (GameObject go in GlobalReferences.FrozenParts.Values) { Part part = go.GetComponent <Part>(); if (part.Parent != -1) { Part parentPart = GlobalReferences.FrozenParts[part.Parent].GetComponent <Part>(); parentPart.Children.Add(part.ID); parentPart.ChildCons.Add(part.ConToParent); } } GlobalReferences.NumOfParts = numParts; } }