コード例 #1
0
    public override Vector2Int[] GetConnectionPoints(Primitives other)
    {
        int xDis = GetXDistance(other);
        int yDis = GetYDistance(other);

        if (other is RectAnglePrimitive)
        {
            RectAnglePrimitive otherRect = (RectAnglePrimitive)other;
            if (xDis < 0)
            {
                // Primitivs overlap in the x axis.
                int xTunnel = 0;
                if (origin.x < otherRect.origin.x)
                {
                    xTunnel = (int)((otherRect.origin.x + origin.x + size.x) / 2);
                }
                else
                {
                    xTunnel = (int)((origin.x + otherRect.origin.x + otherRect.size.x) / 2);
                }

                if (origin.y < otherRect.origin.y)
                {
                    return(new Vector2Int[] { new Vector2Int(xTunnel, origin.y + size.y), new Vector2Int(xTunnel, otherRect.origin.y) });
                }
                else
                {
                    return(new Vector2Int[] { new Vector2Int(xTunnel, otherRect.origin.y + otherRect.size.y), new Vector2Int(xTunnel, origin.y) });
                }
            }
            else if (yDis < 0)
            {
                // Primitivs overlap in the x axis.
                int yTunnel = 0;
                if (origin.y < otherRect.origin.y)
                {
                    yTunnel = (int)((otherRect.origin.y + origin.y + size.y) / 2);
                }
                else
                {
                    yTunnel = (int)((origin.y + otherRect.origin.y + otherRect.size.y) / 2);
                }

                if (origin.x < otherRect.origin.x)
                {
                    return(new Vector2Int[] { new Vector2Int(origin.x + size.x, yTunnel), new Vector2Int(otherRect.origin.x, yTunnel) });
                }
                else
                {
                    return(new Vector2Int[] { new Vector2Int(otherRect.origin.x + otherRect.size.x, yTunnel), new Vector2Int(origin.x, yTunnel) });
                }
            }
        }
        return(null);
    }
コード例 #2
0
    private void AddRoom()
    {
        int width  = UnityEngine.Random.Range(roomWidth / 2, roomWidth);
        int height = UnityEngine.Random.Range(roomHeight / 2, roomHeight);

        int xOrigin = UnityEngine.Random.Range(1, currentMap.GetLength(0) - width - 1);
        int yOrigin = UnityEngine.Random.Range(1, currentMap.GetLength(1) - height - 1);

        RectAnglePrimitive newRect = new RectAnglePrimitive(new Vector2Int(xOrigin, yOrigin), new Vector2Int(width, height));

        Room newRoom = new Room(newRect);

        // Check if this room poses any problems.
        List <Room> overlappinRooms = new List <Room>();
        // Adds up all primitives that the new potential room would have.
        int primitiveCountOfNewRoom = 1;
        int sizeOfNewRoom           = newRect.GetSize();

        foreach (Room r in rooms)
        {
            if (newRoom.DoesRoomOverlap(r))
            {
                // Found an overlapping room that can't be merged.
                if (!r.canMerge)
                {
                    return;
                }

                primitiveCountOfNewRoom += r.primitives.Count;
                overlappinRooms.Add(r);
                sizeOfNewRoom += r.size;
            }
        }

        // Check if the new room would be to big.
        if (sizeOfNewRoom > maxRoomSize)
        {
            return;
        }
        // Check if the new room would have more primitives that allowed.
        if (primitiveCountOfNewRoom > maxRoomPrimitives)
        {
            return;
        }


        foreach (Room r in overlappinRooms)
        {
            rooms.Remove(r);
            newRoom.MergeRooms(r);
        }

        rooms.Add(newRoom);
    }
コード例 #3
0
 public override int GetYDistance(Primitives other)
 {
     if (other is RectAnglePrimitive)
     {
         RectAnglePrimitive otherRect = (RectAnglePrimitive)other;
         if (origin.y < otherRect.origin.y)
         {
             return(otherRect.origin.y - origin.y - size.y);
         }
         else
         {
             return(origin.y - otherRect.origin.y - otherRect.size.y);
         }
     }
     return(int.MaxValue);
 }
コード例 #4
0
    public bool RectangleCollision(RectAnglePrimitive other)
    {
        // Checks the collision between to rectancles by checking each axis seperatately.
        // If there is one axis in which the rectangles don't overlap, the rectalgles themself do therefore not collider.

        // The walls around the rectangles are still considered to be part of the room and
        // are therefore also calculated in this method.

        // Check x axis.
        if (origin.x + size.x + 1 <= other.origin.x - 1 || other.origin.x + other.size.x + 1 <= origin.x - 1)
        {
            return(false);
        }
        // Check y axis.
        if (origin.y + size.y + 1 <= other.origin.y - 1 || other.origin.y + other.size.y + 1 <= origin.y - 1)
        {
            return(false);
        }

        // Rectangles overlap in both axis and therefore they overlap as a whole.
        return(true);
    }
コード例 #5
0
    public override Primitives Copy()
    {
        RectAnglePrimitive newRect = new RectAnglePrimitive(origin, size);

        return(newRect);
    }
コード例 #6
0
    private void CheckVerticalDoubleTunnel(int x, int y, int tunnelWidth)
    {
        int  endY            = 0;
        bool failedToConnect = false;

        int startRoomIndex = currentMap[x, y];

        if (startRoomIndex < 0 || startRoomIndex >= rooms.Count)
        {
            return;
        }

        Room startRoom = rooms[startRoomIndex];

        Room endRoom      = null;
        int  endRoomIndex = 0;


        // Check in x direction.
        // Go throught for the whole width of the tunnel.
        for (int i = 0; i < tunnelWidth && x + i < currentMap.GetLength(0) && !failedToConnect; i++)
        {
            // Check if this a tile next to a wall and a potential connecting point for a tunnel.
            if (currentMap[x + i, y + 1] != -1)
            {
                break;
            }

            bool canConnectInThisRow = false;

            // Try to find an other room within the maximum tunnel legth.
            for (int j = 0; j < straightTunnelMax && j + y < currentMap.GetLength(1) && !failedToConnect; j++)
            {
                int value = currentMap[x + i, y + j];

                if (value == -1 || value == startRoomIndex)
                {
                    continue;
                }

                // First to hit an other room.
                if (endRoom == null)
                {
                    if (startRoom.IsConnectedTo(rooms[value]))
                    {
                        failedToConnect = true;
                        break;
                    }

                    endRoomIndex        = value;
                    endRoom             = rooms[endRoomIndex];
                    endY                = y + j;
                    canConnectInThisRow = true;
                    break;
                }
                else
                {
                    // Check if the connection point has the same y as the previous one.
                    if (endY != y + j)
                    {
                        failedToConnect = true;
                    }
                    else
                    {
                        canConnectInThisRow = true;
                    }
                    break;
                }
            }

            if (!canConnectInThisRow)
            {
                failedToConnect = true;
            }
        }

        if (!failedToConnect && startRoom != null && endRoom != null)
        {
            float ran = UnityEngine.Random.Range(0, 100);

            if (ran < tunnelRate)
            {
                rooms[startRoomIndex].ConnectNewRoom(rooms[endRoomIndex]);
                rooms[endRoomIndex].ConnectNewRoom(rooms[startRoomIndex]);

                RectAnglePrimitive rext = new RectAnglePrimitive(new Vector2Int(x, y + 1), new Vector2Int(tunnelWidth, endY - y - 1));
                floors.Add(new Tunnel(new Primitives[] { rext }, TunnelDirection.Horizontal));
            }
        }
    }