Esempio n. 1
0
    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);
    }
Esempio n. 2
0
    //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;
    }
Esempio n. 3
0
    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;
        }
    }
Esempio n. 4
0
    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);
    }