Пример #1
0
    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;
    }
Пример #2
0
    // Start is called before the first frame update
    public void Initialize(float _minX, float _maxX, float _minY, float _maxY, float _minZ, float _maxZ, float connectionStep, float colliderStep)
    {
        conMaxX = (float)(Math.Ceiling(_maxX / connectionStep) * connectionStep);
        conMinX = (float)(Math.Floor(_minX / connectionStep) * connectionStep);

        conMaxY = (float)(Math.Ceiling(_maxY / connectionStep) * connectionStep);
        conMinY = (float)(Math.Floor(_minY / connectionStep) * connectionStep);

        conMaxZ = (float)(Math.Ceiling(_maxZ / connectionStep) * connectionStep);
        conMinZ = (float)(Math.Floor(_minZ / connectionStep) * connectionStep);


        collMaxX = (float)(Math.Ceiling(_maxX / colliderStep) * colliderStep);
        collMinX = (float)(Math.Floor(_minX / colliderStep) * colliderStep);

        collMaxY = (float)(Math.Ceiling(_maxY / colliderStep) * colliderStep);
        collMinY = (float)(Math.Floor(_minY / colliderStep) * colliderStep);

        collMaxZ = (float)(Math.Ceiling(_maxZ / colliderStep) * colliderStep);
        collMinZ = (float)(Math.Floor(_minZ / colliderStep) * colliderStep);

        gameArea = new Region(_minX, _maxX, _minY, _maxY, _minZ, _maxZ);

        _conStep = connectionStep;

        _collStep = colliderStep;

        ConnectionVoxelContainer.Initialize(ConX, ConY, ConZ, ConOffsetX, ConOffsetY, ConOffsetZ);

        CollisionVoxelContainer.Initialize(CollX, CollY, CollZ, CollOffsetX, CollOffsetY, CollOffsetZ);
    }
Пример #3
0
    public static void UpdateData(byte[] data)
    {
        LoadAggregationContainer     container   = ReadData(data);
        Dictionary <int, GameObject> checkFrozen = new Dictionary <int, GameObject>();

        foreach (KeyValuePair <int, GameObject> pair in GlobalReferences.FrozenParts)
        {
            checkFrozen.Add(pair.Key, pair.Value);
        }

        foreach (PartSpawnData item in container.data)
        {
            GameObject checkGo = GlobalReferences.FrozenParts[item.id];
            if (checkGo == null)
            {
                Debug.LogError("Part " + item.id + " not found in check");
                continue;
            }

            Vector3           oldPos      = checkGo.transform.position;
            List <Connection> connections = checkGo.GetComponent <Part>().Connections;

            if (Vector3.Distance(oldPos, item.Position) > 0.001f)
            {
                CollisionVoxelContainer.RemoveGameObject(checkGo);

                foreach (Connection con in connections)
                {
                    ConnectionVoxelContainer.RemoveConnection(con);
                }
            }

            checkGo.transform.position = item.Position;
            checkGo.transform.rotation = item.Rotation;

            if (Vector3.Distance(oldPos, item.Position) > 0.001f)
            {
                CollisionVoxelContainer.StoreGameObject(checkGo);

                foreach (Connection con in connections)
                {
                    ConnectionVoxelContainer.StoreConnection(con);
                }
            }

            checkFrozen.Remove(item.id);
        }

        GameObject[] leftovers = checkFrozen.Values.ToArray <GameObject>();

        for (int i = leftovers.Length - 1; i >= 0; --i)
        {
            GameObject go       = leftovers[i];
            Part       leftover = go.GetComponent <Part>();
            Debug.LogError("Part " + leftover.ID + " was left over in check and will be deleted");
            leftover.LocalDelete();
        }
    }
Пример #4
0
    public static void AfterSimulation(TIExportAR exportItem = null, bool export = false)
    {
        ++handlerCallBack;
        if (handlerCallBack >= handlers.Count)
        {
            if (!export)
            {
                ConnectionVoxelContainer.RemoveAllConnections();
                CollisionVoxelContainer.RemoveAllGos();

                foreach (GameObject go in GlobalReferences.FrozenParts.Values)
                {
                    go.transform.parent = null;
                    Part part = go.GetComponent <Part>();

                    CollisionVoxelContainer.StoreGameObject(go);

                    foreach (int i in part.ActiveConnections)
                    {
                        ConnectionVoxelContainer.StoreConnection(part.Connections[i]);
                    }
                }

                for (int i = rbs.Count - 1; i >= 0; --i)
                {
                    MonoBehaviour.Destroy(rbs[i]);
                }

                simulationInProgress = false;
                simulationDone       = false;
            }
            else
            {
                string c = expCount.ToString();
                while (c.Length < countDigits)
                {
                    c = "0" + c;
                }
                SaveLoad.SaveGame(c + "_", true, folder);
            }


            for (int i = handlers.Count - 1; i >= 0; --i)
            {
                MonoBehaviour.Destroy(handlers[i]);
            }
            handlers.Clear();
            handlerCallBack = 0;

            if (export)
            {
                Export(exportItem);
            }
        }
    }
Пример #5
0
    private bool CollisionDetection()
    {
        bool isCollide = false;

        List <GameObject> collisionGo = new List <GameObject>();

        MeshCollider[] selfColliders = gameObject.GetComponents <MeshCollider>();


        collisionGo = CollisionVoxelContainer.RevealCloseGos(gameObject);

        foreach (GameObject closeGo in collisionGo)
        {
            if (closeGo != null)
            {
                MeshCollider[] otherColliders = closeGo.GetComponents <MeshCollider>();
                foreach (MeshCollider otherCol in otherColliders)
                {
                    foreach (MeshCollider selfCol in selfColliders)
                    {
                        Vector3 vec = Vector3.up;
                        float   f   = 0;
                        if (Physics.ComputePenetration(selfCol, gameObject.transform.position, gameObject.transform.rotation, otherCol, closeGo.transform.position, closeGo.transform.rotation, out vec, out f))
                        {
                            isCollide = true;
                            return(isCollide);
                        }
                    }
                }
            }
        }


        if (true)
        {
            foreach (GameObject go in GlobalReferences.AdditionalGeometry)
            {
                foreach (MeshCollider mc in selfColliders)
                {
                    Vector3 vec = Vector3.up;
                    float   f   = 0;
                    if (go.GetComponent <MeshCollider>() != null && Physics.ComputePenetration(mc, gameObject.transform.position, gameObject.transform.rotation, go.GetComponent <MeshCollider>(), go.transform.position, go.transform.rotation, out vec, out f))
                    {
                        isCollide = true;
                        return(isCollide);
                    }
                }
            }
        }


        return(isCollide);
    }
Пример #6
0
    public override void OnEvent(EndSimulation evnt)
    {
        if (!evnt.FromSelf)
        {
            ConnectionVoxelContainer.RemoveAllConnections();
            CollisionVoxelContainer.RemoveAllGos();

            foreach (GameObject go in GlobalReferences.FrozenParts.Values)
            {
                Part part = go.GetComponent <Part>();

                CollisionVoxelContainer.StoreGameObject(go);

                foreach (int i in part.ActiveConnections)
                {
                    ConnectionVoxelContainer.StoreConnection(part.Connections[i]);
                }
            }
        }
        PlacementReferences.Scanning = true;
    }
Пример #7
0
    public void FreezePart(int?_id = null)
    {
        placedMat = Resources.Load <Material>("Materials/PlacedMaterial");
        gameObject.GetComponent <MeshRenderer>().material = placedMat;
        gameObject.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
        gameObject.layer = 9;
        Destroy(gameObject.GetComponent <PartBehaviour>());
        Destroy(gameObject.GetComponent <ConnectionScanning>());

        foreach (int i in ActiveConnections)
        {
            ConnectionVoxelContainer.StoreConnection(connections[i]);
        }

        CollisionVoxelContainer.StoreGameObject(gameObject);

        if (GlobalReferences.AffectedParts.Contains(gameObject))
        {
            GlobalReferences.AffectedParts.Remove(gameObject);
        }

        if (GlobalReferences.FreeParts.Contains(gameObject))
        {
            GlobalReferences.FreeParts.Remove(gameObject);
        }

        if (_id == null)
        {
            id = GlobalReferences.NumOfParts;
            ++GlobalReferences.NumOfParts;
        }
        else
        {
            id = _id;
        }

        GlobalReferences.FrozenParts.Add((int)ID, gameObject);
        gameObject.name = name + "_" + id;
    }
Пример #8
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;
    }
Пример #9
0
    public void FreezePart(int _id = -1)
    {
        if (!TestDeleteSpawn())
        {
            Destroy(gameObject);
            return;
        }

        placedMat = Resources.Load <Material>("Materials/" + MaterialHolder.MatSet + "/PlacedMaterial");
        gameObject.GetComponent <MeshRenderer>().material = placedMat;

        gameObject.layer = 9;

        if (GetComponent <ConstantForce>() != null)
        {
            Destroy(gameObject.GetComponent <ConstantForce>());
        }

        if (GetComponent <Rigidbody>() != null)
        {
            Destroy(gameObject.GetComponent <Rigidbody>());
        }

        if (GetComponent <PartBehaviour>() != null)
        {
            Destroy(gameObject.GetComponent <PartBehaviour>());
        }

        if (GetComponent <ConnectionScanning>() != null)
        {
            Destroy(gameObject.GetComponent <ConnectionScanning>());
        }

        if (GetComponent <KillTimer>() != null)
        {
            Destroy(GetComponent <KillTimer>());
        }

        gameObject.isStatic = true;

        foreach (int i in ActiveConnections)
        {
            ConnectionVoxelContainer.StoreConnection(connections[i]);
        }

        CollisionVoxelContainer.StoreGameObject(gameObject);


        if (GlobalReferences.AffectedParts.Contains(gameObject))
        {
            GlobalReferences.AffectedParts.Remove(gameObject);
        }

        if (GlobalReferences.FreeParts.Contains(gameObject))
        {
            GlobalReferences.FreeParts.Remove(gameObject);
        }

        if (_id == -1)
        {
            id = GlobalReferences.GetNextID();
        }
        else
        {
            id = _id;
        }

        GlobalReferences.FrozenParts.Add((int)ID, gameObject);
        gameObject.name = name + "_" + id;

        if (TICtrlZ.LastID != ID)
        {
            TICtrlZ.History.Push(new HistoryItem(ID));
        }

        //Debug.Log("#43 Block Transform Frozen: " + ID + ", Position = " + transform.position.ToString("F2") + ", Rotation = " + transform.rotation.eulerAngles.ToString("F2"));
    }