private static JsonAssembly BuildAssembly(Dictionary <int, GameObject> parts, int ID) { Dictionary <string, JsonPart> jsonParts = new Dictionary <string, JsonPart>(); List <int> IDs = new List <int>(); 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)); foreach (KeyValuePair <int, GameObject> dicEntry in parts) { if (dicEntry.Value != null) { Part p = dicEntry.Value.GetComponent <Part>(); Matrix4x4 m = p.gameObject.transform.localToWorldMatrix; m = transM * m; JsonPart jsonP = new JsonPart(p.Parent, p.Name, new JsonTransform(m), p.ActiveConnections, p.Children, p.ParentCon, p.ChildCons); jsonParts.Add(dicEntry.Key.ToString(), jsonP); IDs.Add((int)p.ID); } } JsonAssembly assembly = new JsonAssembly(ID.ToString(), jsonParts); return(assembly); }
public static void ResetSimulation(JsonAssembly assembly = null) { rbs.Clear(); ConnectionVoxelContainer.RemoveAllConnections(); CollisionVoxelContainer.RemoveAllGos(); foreach (KeyValuePair <int, Vector3> pos in savePositions) { GameObject go = GlobalReferences.FrozenParts[pos.Key]; go.transform.parent = null; go.transform.position = pos.Value; go.transform.rotation = saveRotations[pos.Key]; Part part = go.GetComponent <Part>(); CollisionVoxelContainer.StoreGameObject(go); foreach (int i in part.ActiveConnections) { ConnectionVoxelContainer.StoreConnection(part.Connections[i]); } } savePositions.Clear(); saveRotations.Clear(); simulationDone = false; }
public static void Save(Dictionary <int, GameObject> parts, int ID, string path) { JsonAssembly assembly = BuildAssembly(parts, ID); StringBuilder jsonSb; jsonSb = new StringBuilder(JsonConvert.SerializeObject(assembly, Formatting.Indented)); using (FileStream stream = new FileStream(path, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(stream)) { sw.Write(jsonSb); } } }
private static JsonAssembly BuildAssembly(Dictionary <int, GameObject> parts, int ID) { Dictionary <string, JsonPart> jsonParts = new Dictionary <string, JsonPart>(); 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)); foreach (KeyValuePair <int, GameObject> dicEntry in parts) { if (dicEntry.Value != null) { if (BoltNetwork.IsRunning && !GlobalReferences.PartIDLedger.ContainsKey(dicEntry.Key)) { GlobalReferences.PartIDLedger.Add(dicEntry.Key, GlobalReferences.GetNextID()); Debug.LogError("PartIdLedger didn't contain a Key for " + dicEntry.Key + ", was changed to: " + GlobalReferences.PartIDLedger[dicEntry.Key]); } Part p = dicEntry.Value.GetComponent <Part>(); Matrix4x4 m = p.gameObject.transform.localToWorldMatrix; m = transM * m; m = m * Matrix4x4.TRS(p.PartOffset, Quaternion.identity, Vector3.one); string templateName = GlobalReferences.TemplateParts[p.TemplateID].GetComponent <Part>().name; if (!BoltNetwork.IsRunning) { JsonPart jsonP = new JsonPart(p.Parent, templateName, new JsonTransform(m), p.ParentCon, p.ConToParent, p.ActiveConnections, p.Children); jsonParts.Add(dicEntry.Key.ToString(), jsonP); } else { int parent = p.Parent; if (parent != -1 && BoltNetwork.IsRunning) { try { parent = GlobalReferences.PartIDLedger[p.Parent]; } catch { Debug.LogError("PartIdLedger didn't contain the parent: " + parent + " for part: " + dicEntry.Key + ", parent will be set to null"); parent = -1; } } List <int> children = new List <int>(); foreach (int i in p.Children) { if (BoltNetwork.IsRunning) { try { children.Add(GlobalReferences.PartIDLedger[i]); } catch { Debug.LogError("PartIdLedger didn't contain the child: " + i + " for part: " + dicEntry.Key + ", child will be ommited"); } } else { children.Add(i); } } JsonPart jsonP = new JsonPart(parent, templateName, new JsonTransform(m), p.ParentCon, p.ConToParent, p.ActiveConnections, children); if (BoltNetwork.IsRunning) { jsonParts.Add(GlobalReferences.PartIDLedger[dicEntry.Key].ToString(), jsonP); } else { jsonParts.Add(dicEntry.Key.ToString(), jsonP); } } } } JsonAssembly assembly = new JsonAssembly(ID.ToString(), jsonParts); return(assembly); }