Пример #1
0
        /// <summary>
        /// Check the Segment Collision and Remove colliding Cells with there later Generations
        /// </summary>
        /// <param name="_cellsToCheck">The List of Cells to Check the Collision with</param>
        /// <returns></returns>
        public HashSet <int> CheckCollision(List <Cell> _cellsToCheck)
        {
            List <Cell>   PolyPolyCheckList   = new List <Cell>();
            HashSet <int> StreetIDsToRecreate = new HashSet <int>();

            //Sphere Sphere Collsion | fast but not precies | Save the Cells that can possible collide for a more precies check
            for (int i = 0; i < _cellsToCheck.Count; i++)
            {
                if (!_cellsToCheck[i].IsBlocked &&
                    MyCollision.SphereSphere(m_Center, m_CollisionRadius, _cellsToCheck[i].m_PosCenter, _cellsToCheck[i].m_Radius))
                {
                    PolyPolyCheckList.Add(_cellsToCheck[i]);
                }
            }

            //Poly Poly Collision | slow but more precies
            for (int i = 0; i < PolyPolyCheckList.Count; i++)
            {
                if (!PolyPolyCheckList[i].IsBlocked && MyCollision.PolyPoly(PolyPolyCheckList[i].m_Corner, m_CornerPos))
                {
                    PolyPolyCheckList[i].Delete(); //Delete the Colliding Cell and the later Generations
                    int id = PolyPolyCheckList[i].m_Street.ID;
                    StreetIDsToRecreate.Add(id);   //Saves the Street ID to recreate the Grid Mesh
                }
            }

            return(StreetIDsToRecreate);
        }
Пример #2
0
        /// <summary>
        /// Check if the HitPos is over an Building/Area
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        protected bool CheckAreaCollision(out ABuilding b)
        {
            List <Area> SphereSphere = new List <Area>();

            b = null;

            foreach (Area area in BuildingManager.m_AllAreas)
            {
                if (MyCollision.SphereSphere(new Vector2(m_hitPos.x, m_hitPos.z), 0.8f, new Vector2(area.m_OP.Position.x, area.m_OP.Position.z), area.m_Radius))
                {
                    SphereSphere.Add(area);
                }
            }
            if (SphereSphere.Count == 0)
            {
                return(false);
            }
            else
            {
                int   index           = -1;
                float closestDistance = float.MaxValue;
                for (int i = 0; i < SphereSphere.Count; i++)
                {
                    float distance = Vector3.SqrMagnitude(SphereSphere[i].m_OP.Position - m_hitPos);
                    if (distance < closestDistance)
                    {
                        closestDistance = distance;
                        index           = i;
                    }
                }
                b = SphereSphere[index].m_Building;
                return(true);
            }
        }
Пример #3
0
        /// <summary>
        /// Check für Collider with a OverlapSphere
        /// </summary>
        /// <returns>return true if another Cell from a diffrent Street ID collid</returns>
        public bool CheckForCollision()
        {
            List<Cell> cellToCheck = new List<Cell>();
            List<StreetSegment> segToCheck = new List<StreetSegment>();
            List<Cross> crossToCheck = new List<Cross>();

            //Sphere Sphere
            //Cell Cell
            foreach (Cell c in GridManager.m_AllCells)
                if (c.ID != this.ID && MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, c.m_PosCenter, c.m_Radius))
                    cellToCheck.Add(c);

            List<StreetComponent> allComponetns = StreetComponentManager.GetAllStreetComponents();
            //Cell Segment
            for (int i = 0; i < allComponetns.Count; i++)
            {
                StreetComponent comp = allComponetns[i];

                if (comp.ID == this.ID) continue;
                if (comp is Street)
                {
                    Street s = (Street)comp;
                    foreach (StreetSegment seg in s.m_Segments)
                        if (MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, seg.m_Center, seg.m_CollisionRadius))
                            segToCheck.Add(seg);
                }
                else if (comp is Cross)
                {
                    Cross c = (Cross)comp;
                    if (MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, c.m_center, 1.7f))
                        crossToCheck.Add(c);
                }
            }

            //Poly Poly
            //Cell Cross
            foreach (Cross c in crossToCheck)
                if (MyCollision.PolyPoly(this.m_Corner, c.m_corners))
                    return true;

            //Cell Segment
            foreach (StreetSegment seg in segToCheck)
                if (MyCollision.PolyPoly(this.m_Corner, seg.m_CornerPos))
                    return true;

            //Cell Cell
            foreach (Cell c in cellToCheck)
                if (MyCollision.PolyPoly(this.m_Corner, c.m_Corner))
                    return true;

            return false;
        }
Пример #4
0
        public void CheckGridCollision()
        {
            m_center     = new Vector2(transform.position.x, transform.position.z);
            m_corners[0] = m_center + new Vector2(1.2f, 1.2f);
            m_corners[1] = m_center + new Vector2(-1.2f, 1.2f);
            m_corners[2] = m_center + new Vector2(-1.2f, -1.2f);
            m_corners[3] = m_center + new Vector2(1.2f, -1.2f);

            HashSet <int> StreetsToRecreate = new HashSet <int>();
            List <Cell>   PolyPolyCheckList = new List <Cell>();
            List <Cell>   CellsToCheck      = GridManager.m_AllCells;

            //Sphere Sphere Coll
            for (int i = 0; i < CellsToCheck.Count; i++)
            {
                if (MyCollision.SphereSphere(m_center, 1.7f, CellsToCheck[i].m_PosCenter, CellsToCheck[i].m_Radius))
                {
                    PolyPolyCheckList.Add(CellsToCheck[i]);
                }
            }

            //Poly Poly Coll
            for (int i = 0; i < PolyPolyCheckList.Count; i++)
            {
                if (MyCollision.PolyPoly(PolyPolyCheckList[i].m_Corner, m_corners))
                {
                    PolyPolyCheckList[i].Delete();
                    int id = PolyPolyCheckList[i].m_Street.ID;
                    StreetsToRecreate.Add(id);
                }
            }

            //Recreate Grid
            foreach (int i in StreetsToRecreate)
            {
                Street s = StreetComponentManager.GetStreetByID(i);
                GridManager.RemoveGridMesh(s);
                MeshGenerator.CreateGridMesh(s, s.m_GridObj.GetComponent <MeshFilter>(), s.m_GridRenderer);
            }
        }
Пример #5
0
        public override void ToolUpdate()
        {
            if (Input.GetMouseButton(0))
            {
                foreach (Cell c in GridManager.m_FirstGenCells)
                {
                    if (MyCollision.SphereSphere(new Vector2(m_hitPos.x, m_hitPos.z), 0.8f, c.m_PosCenter, c.m_Radius))
                    {
                        if (Input.GetKey(KeyCode.LeftShift)) //Multi Fill
                        {
                            Street      s   = c.m_Street;
                            Vector2Int  pos = c.Pos;
                            EAssignment firstCellAssigment = c.m_CellAssignment;
                            //Go y+
                            do
                            {
                                if (!s.m_StreetCells.ContainsKey(pos))
                                {
                                    return;
                                }

                                Cell currCell      = s.m_StreetCells[pos];
                                int  materialIndex = currCell.Pos.y;
                                if (currCell.Pos.x < 0)
                                {
                                    materialIndex += currCell.m_Street.m_RowAmount;
                                }

                                ChanageMaterial(m_CurrendAssignment, materialIndex, currCell.m_Street.m_GridRenderer, currCell.m_Street.m_Coll_GridRenderer);
                                c.m_Street.ChangeCellAssigtment(currCell.Pos, m_CurrendAssignment);
                                pos.y++;
                                if (s.m_StreetCells.ContainsKey(pos) && s.m_StreetCells[pos].m_CellAssignment != firstCellAssigment)
                                {
                                    break;
                                }
                            }while (s.m_StreetCells.ContainsKey(pos));

                            pos = c.Pos;
                            //Go y--
                            do
                            {
                                Cell currCell      = s.m_StreetCells[pos];
                                int  materialIndex = currCell.Pos.y;
                                if (currCell.Pos.x < 0)
                                {
                                    materialIndex += currCell.m_Street.m_RowAmount;
                                }

                                ChanageMaterial(m_CurrendAssignment, materialIndex, currCell.m_Street.m_GridRenderer, currCell.m_Street.m_Coll_GridRenderer);
                                c.m_Street.ChangeCellAssigtment(currCell.Pos, m_CurrendAssignment);
                                pos.y--;
                                if (s.m_StreetCells.ContainsKey(pos) && s.m_StreetCells[pos].m_CellAssignment != firstCellAssigment)
                                {
                                    break;
                                }
                            }while (s.m_StreetCells.ContainsKey(pos));
                        }
                        else //Normal Fill
                        {
                            int materialIndex = c.Pos.y;
                            if (c.Pos.x < 0)
                            {
                                materialIndex += c.m_Street.m_RowAmount;
                            }

                            ChanageMaterial(m_CurrendAssignment, materialIndex, c.m_Street.m_GridRenderer, c.m_Street.m_Coll_GridRenderer);
                            c.m_Street.ChangeCellAssigtment(c.Pos, m_CurrendAssignment);
                        }
                    }
                }
            }

            if (Input.GetKeyDown(KeyCode.W))
            {
                m_CurrendAssignment = EAssignment.LIVING;
                UIManager.Instance.HighlightButton(UIManager.Instance.LivingButton);
            }
            if (Input.GetKeyDown(KeyCode.G))
            {
                m_CurrendAssignment = EAssignment.BUSINESS;
                UIManager.Instance.HighlightButton(UIManager.Instance.BusinessButton);
            }
            if (Input.GetKeyDown(KeyCode.I))
            {
                m_CurrendAssignment = EAssignment.INDUSTRY;
                UIManager.Instance.HighlightButton(UIManager.Instance.IndustryButton);
            }
        }