Exemple #1
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;
    }
Exemple #2
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;
        }
    }