示例#1
0
        private void InitializeRooms()
        {
            BoundsInt bounds = metaTileMap.GetBounds();

            int roomCounter = 1;

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            foreach (Vector3Int position in bounds.allPositionsWithin)
            {
                MetaDataNode node = metaDataLayer.Get(position, false);

                if ((node == null || node.Room == 0) && !metaTileMap.IsSpaceAt(position) && metaTileMap.IsAtmosPassableAt(position))
                {
                    if (FindRoom(position, roomCounter))
                    {
                        roomCounter++;
                    }
                }
            }

            sw.Stop();

            Debug.Log("Room init: " + sw.ElapsedMilliseconds + " ms");
        }
        private static void DrawGizmo(MetaTileMap scr, GizmoType gizmoType)
        {
            if (!DrawGizmos)
            {
                return;
            }

            Vector3Int start = Vector3Int.RoundToInt(Camera.current.ScreenToWorldPoint(Vector3.one * -32) - scr.transform.position);             // bottom left
            Vector3Int end   =
                Vector3Int.RoundToInt(Camera.current.ScreenToWorldPoint(new Vector3(Camera.current.pixelWidth + 32, Camera.current.pixelHeight + 32)) -
                                      scr.transform.position);

            start.z = 0;
            end.z   = 1;


            if (end.y - start.y > 100)
            {
                // avoid being zoomed out too much (creates too many objects)
                return;
            }

            Gizmos.matrix = scr.transform.localToWorldMatrix;


            Color blue = Color.blue;

            blue.a = 0.5f;

            Color red = Color.red;

            red.a = 0.5f;

            Color green = Color.green;

            red.a = 0.5f;

            if (room)
            {
                DrawRoom(scr);
            }
            else
            {
                foreach (Vector3Int position in new BoundsInt(start, end - start).allPositionsWithin)
                {
                    if (space)
                    {
                        if (scr.IsSpaceAt(position))
                        {
                            Gizmos.color = red;
                            Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                        }
                    }
                    else
                    {
                        if (corners)
                        {
                            if (scr.HasTile(position, LayerType.Walls))
                            {
                                Gizmos.color = green;

                                int corner_count = 0;
                                foreach (Vector3Int pos in new[] { Vector3Int.up, Vector3Int.left, Vector3Int.down, Vector3Int.right, Vector3Int.up })
                                {
                                    if (!scr.HasTile(position + pos, LayerType.Walls))
                                    {
                                        corner_count++;
                                    }
                                    else
                                    {
                                        corner_count = 0;
                                    }

                                    if (corner_count > 1)
                                    {
                                        Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Gizmos.color = blue;
                            if (passable)
                            {
                                if (north)
                                {
                                    if (!scr.IsPassableAt(position + Vector3Int.up, position))
                                    {
                                        Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                                    }
                                }
                                else if (south)
                                {
                                    if (!scr.IsPassableAt(position + Vector3Int.down, position))
                                    {
                                        Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                                    }
                                }
                                else if (!scr.IsPassableAt(position))
                                {
                                    Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                                }
                            }
                            else
                            {
                                if (!scr.IsAtmosPassableAt(position))
                                {
                                    Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                                }
                            }
                        }
                    }
                }
            }
        }
        private static void DrawRoom(MetaTileMap metaTileMap)
        {
            Vector3Int mousePos = Vector3Int.RoundToInt(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin);

            mousePos  -= Vector3Int.one;
            mousePos.z = 0;

            if (currentRoom == null || !currentRoom.Contains(mousePos))
            {
                currentRoom = rooms.Find(x => x.Contains(mousePos));

                if (currentRoom == null)
                {
                    if (metaTileMap.IsAtmosPassableAt(mousePos) && !metaTileMap.IsSpaceAt(mousePos))
                    {
                        currentRoom = new HashSet <Vector3Int>();

                        Queue <Vector3Int> posToCheck = new Queue <Vector3Int>();
                        posToCheck.Enqueue(mousePos);

                        while (posToCheck.Count > 0)
                        {
                            Vector3Int pos = posToCheck.Dequeue();
                            currentRoom.Add(pos);

                            foreach (Vector3Int dir in new[] { Vector3Int.up, Vector3Int.left, Vector3Int.down, Vector3Int.right })
                            {
                                Vector3Int neighbor = pos + dir;

                                if (!posToCheck.Contains(neighbor) && !currentRoom.Contains(neighbor))
                                {
                                    if (metaTileMap.IsSpaceAt(neighbor))
                                    {
                                        currentRoom.Clear();
                                        posToCheck.Clear();
                                        break;
                                    }

                                    if (metaTileMap.IsAtmosPassableAt(neighbor))
                                    {
                                        posToCheck.Enqueue(neighbor);
                                    }
                                }
                            }
                        }

                        rooms.Add(currentRoom);
                    }
                }
            }

            if (currentRoom != null)
            {
                Color color = Color.cyan;
                color.a      = 0.5f;
                Gizmos.color = color;

                foreach (Vector3Int pos in currentRoom)
                {
                    Gizmos.DrawCube(pos + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                }
            }
        }
示例#4
0
 public bool IsSpaceAt(Vector3Int position, bool isServer)
 {
     return(MetaTileMap.IsSpaceAt(position, isServer));
 }
示例#5
0
 public bool IsSpaceAt(Vector3Int position)
 {
     return(metaTileMap.IsSpaceAt(position));
 }
        static void DrawGizmo(MetaTileMap scr, GizmoType gizmoType)
        {
            if (!DrawGizmos)
            {
                return;
            }

            var start = Vector3Int.RoundToInt(Camera.current.ScreenToWorldPoint(Vector3.one * -32) - scr.transform.position); // bottom left
            var end   = Vector3Int.RoundToInt(Camera.current.ScreenToWorldPoint(new Vector3(Camera.current.pixelWidth + 32, Camera.current.pixelHeight + 32)) - scr.transform.position);

            start.z = 0;
            end.z   = 1;


            if (end.y - start.y > 100)
            {
                // avoid being zoomed out too much (creates too many objects)
                return;
            }

            Gizmos.matrix = scr.transform.localToWorldMatrix;


            var blue = Color.blue;

            blue.a = 0.5f;

            var red = Color.red;

            red.a = 0.5f;

            foreach (var position in new BoundsInt(start, end - start).allPositionsWithin)
            {
                if (!space)
                {
                    Gizmos.color = blue;
                    if (passable)
                    {
                        if (north)
                        {
                            if (!scr.IsPassableAt(position + Vector3Int.up, position))
                            {
                                Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                            }
                        }
                        else if (south)
                        {
                            if (!scr.IsPassableAt(position + Vector3Int.down, position))
                            {
                                Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                            }
                        }
                        else if (!scr.IsPassableAt(position))
                        {
                            Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                        }
                    }
                    else
                    {
                        if (!scr.IsAtmosPassableAt(position))
                        {
                            Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                        }
                    }
                }
                else
                {
                    if (scr.IsSpaceAt(position))
                    {
                        Gizmos.color = red;
                        Gizmos.DrawCube(position + new Vector3(0.5f, 0.5f, 0), Vector3.one);
                    }
                }
            }
        }