Ejemplo n.º 1
0
    public static void SpawnBlockParent(PartTokenParent token, Vector3 pos, Quaternion rot, int owner)
    {
        if (GlobalReferences.FrozenParts.ContainsKey(token.ID))
        {
            Debug.LogError("Frozen Parts already containts Key " + token.ID);
            return;
        }

        GameObject newBlock = GlobalReferences.PartSpawner.SpawnGhostPart(token.TemplateID, pos, rot);

        Destroy(newBlock.GetComponent <ConstantForce>());
        Destroy(newBlock.GetComponent <Rigidbody>());

        Part newPart = newBlock.GetComponent <Part>();

        newPart.SetInactive(token.Con);
        newPart.Parent    = token.Parent;
        newPart.ParentCon = token.ParentCon;
        newPart.PartOwner = owner;

        if (token.Parent != -1 && GlobalReferences.FrozenParts.ContainsKey(token.Parent))
        {
            Part parentPart = GlobalReferences.FrozenParts[token.Parent].GetComponent <Part>();
            parentPart.Children.Add(token.ID);
            parentPart.ChildCons.Add(token.Con);
            try
            {
                parentPart.SetInactive(parentPart.Connections[token.ParentCon]);
            }
            catch
            {
                Debug.LogError("parentPart: " + parentPart.ID + " doesnt contain connection: " + token.ParentCon);
            }
        }
        else if (token.Parent != -1)
        {
            var wait = newBlock.AddComponent <NetworkPartWaitForParent>();
            wait.Initialize(token.Parent, token.ID, token.Con, token.ParentCon);
        }

        newPart.FreezePart(token.ID);

        if (!GlobalReferences.PartIDLedger.ContainsKey(token.ID))
        {
            GlobalReferences.PartIDLedger.Add(token.ID, GlobalReferences.GetNextID());
        }

        if (token.Disabled)
        {
            newPart.Disable();
        }

        //Debug.Log("#43 Block Transform Done: " + token.ID + ", Position = " + newBlock.transform.position.ToString("F2") + ", Rotation = " + newBlock.transform.rotation.eulerAngles.ToString("F2"));
    }
Ejemplo n.º 2
0
    //Events
    #region
    public override void OnEvent(CheckBlockFreeze evnt)
    {
        if (BoltNetwork.IsServer)
        {
            Part p          = gameObject.GetComponent <Part>();
            Part parentPart = GlobalReferences.FrozenParts[evnt.ParentID].GetComponent <Part>();

            Connection bestOnPart        = p.Connections[evnt.ConnectionID];
            Connection closestConnection = parentPart.Connections[evnt.ParentCon];

            gameObject.transform.position = evnt.BlockPosition;
            gameObject.transform.rotation = evnt.BlockRotation;
            //AlignPlane.Orient(bestOnPart.Pln, closestConnection.Pln, gameObject);

            if (!ConnectionScanning.CollisionDetection(gameObject) && CheckOriginAlignment(bestOnPart, closestConnection, evnt.BlockPosition, evnt.BlockRotation))// && CheckConncetionAlignment(bestOnPart, closestConnection))//, transform, parentPart.transform))
            {
                var token = new PartTokenParent();
                token.TemplateID = p.TemplateID;

                token.ID = UnityEngine.Random.Range(int.MinValue, int.MaxValue);

                while (GlobalReferences.FrozenParts.ContainsKey(token.ID))
                {
                    token.ID = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
                }


                token.Parent    = evnt.ParentID;
                token.ParentCon = evnt.ParentCon;

                token.Con = evnt.ConnectionID;

                var spawn = SpawnFrozenBlock.Create();
                spawn.token    = token;
                spawn.Position = evnt.BlockPosition;
                spawn.Rotation = evnt.BlockRotation;
                spawn.Send();

                var destroy = BlockDestroy.Create(entity);
                destroy.Send();

                //Debug.Log("#43 Spawning Block: " + token.ID + ", Position = " + evnt.BlockPosition.ToString("F2") + ", Rotation = " + evnt.BlockRotation.eulerAngles.ToString("F2"));
            }
            else
            {
                var decline = DeclineBlockFreeze.Create(entity);
                decline.OldBlockPosition = evnt.OldBlockPosition;
                decline.OldBlockRotation = evnt.OldBlockRotation;

                decline.Send();
            }
        }
    }
Ejemplo n.º 3
0
    void FreezeObject()
    {
        if (BoltNetwork.IsRunning)
        {
            Vector3    pos  = childProxies[childIndex].transform.position;
            Quaternion rot  = childProxies[childIndex].transform.rotation;
            Part       part = childProxies[childIndex].GetComponent <Part>();

            int id = Random.Range(int.MinValue, int.MaxValue);
            while (GlobalReferences.FrozenParts.ContainsKey(id) && id == -1)
            {
                id = Random.Range(int.MinValue, int.MaxValue);
            }

            Part parentPart = chosenPart.GetComponent <Part>();
            int  parentID   = parentPart.Connections.IndexOf(chosenConnection);

            PartTokenParent token = new PartTokenParent(part.TemplateID, id, parentPart.ID, parentID, conIndex, false);

            var spawn = SpawnFrozenBlock.Create();
            spawn.token    = token;
            spawn.Position = pos;
            spawn.Rotation = rot;
            if (BoltNetwork.IsServer)
            {
                spawn.Owner = 0;
            }
            else
            {
                spawn.Owner = (int)BoltNetwork.Server.ConnectionId;
            }
            spawn.Send();
        }
        else
        {
            Part part = childProxies[childIndex].GetComponent <Part>();
            part.FreezePart();

            Part parentPart = chosenPart.GetComponent <Part>();

            part.Parent      = parentPart.ID;
            part.ParentCon   = parentPart.Connections.IndexOf(chosenConnection);
            part.ConToParent = conIndex;

            parentPart.Children.Add(part.ID);
            parentPart.ChildCons.Add(conIndex);

            Destroy(part.gameObject.GetComponent <CheckGrowCollision>());
        }
    }
Ejemplo n.º 4
0
    public PartSpawnData(IProtocolToken _token, Vector3 _position, Quaternion _rotation, int _owner)
    {
        posX = _position.x;
        posY = _position.y;
        posZ = _position.z;

        rotX = _rotation.x;
        rotY = _rotation.y;
        rotZ = _rotation.z;
        rotW = _rotation.w;

        owner = _owner;


        if (_token.GetType() == typeof(PartToken))
        {
            PartToken token = (PartToken)_token;

            templateID = token.TemplateID;
            id         = token.ID;
            disabled   = token.Disabled;
            parent     = -1;
            parentCon  = -1;
            con        = -1;
        }
        else
        {
            PartTokenParent token = (PartTokenParent)_token;

            templateID = token.TemplateID;
            id         = token.ID;
            parent     = token.Parent;
            parentCon  = token.ParentCon;
            con        = token.Con;
            disabled   = token.Disabled;
        }
    }
Ejemplo n.º 5
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;
        }
    }