Ejemplo n.º 1
0
        public DungeonDomainBuilder SpawnRoom(int minSize, int maxSize, DungeonRoomShape shape, bool allowOverlap, int maxModifications = 20)
        {
            var width  = UnityEngine.Random.Range(minSize, maxSize + 1);
            var height = UnityEngine.Random.Range(minSize, maxSize + 1);

            if (width <= 2)
            {
                height = maxSize;
            }
            else if (height <= 2)
            {
                width = maxSize;
            }

            return(SpawnRoom(new int2(width, height), shape, allowOverlap, maxModifications));
        }
Ejemplo n.º 2
0
        public DungeonDomainBuilder SpawnRoom(int2 size, DungeonRoomShape shape, bool allowOverlap, int maxModifications = 20)
        {
            var width  = size.x;
            var height = size.y;

            if (shape == DungeonRoomShape.Random)
            {
                shape = (DungeonRoomShape)UnityEngine.Random.Range((int)DungeonRoomShape.Rectangular, (int)DungeonRoomShape.Round + 1);
            }
            Action($"Spawn room ({shape}:{width}x{height})");
            {
                Do((context) =>
                {
                    var room = new DungeonRoomMeta
                    {
                        Width            = width,
                        Height           = height,
                        Shape            = shape,
                        X                = 0,
                        Y                = 0,
                        CenterX          = 0,
                        CenterY          = 0,
                        Theme            = context.CurrentTheme,
                        MaxModifications = maxModifications,
                    };

                    var parentRoom = TryGetParentRoom(context, allowOverlap, out var validExits);
                    if (parentRoom != null)
                    {
                        var direction = context.CurrentBuilderDirection;
                        if (validExits.Contains(direction) == false && !allowOverlap)
                        {
                            direction = BuilderDirection.Random;
                        }

                        if (direction == BuilderDirection.Random)
                        {
                            if (validExits.Count > 0)
                            {
                                direction = validExits[UnityEngine.Random.Range(0, validExits.Count)];
                            }
                            else
                            {
                                direction = (BuilderDirection)UnityEngine.Random.Range((int)BuilderDirection.North, (int)BuilderDirection.West + 1);
                            }
                        }

                        var halfWidth        = width / 2;
                        var halfHeight       = height / 2;
                        var parentHalfWidth  = parentRoom.Width / 2;
                        var parentHalfHeight = parentRoom.Height / 2;

                        switch (direction)
                        {
                        case BuilderDirection.North:
                            {
                                room.X       = parentRoom.X;
                                room.Y       = parentRoom.Y - 1;
                                room.CenterX = parentRoom.CenterX;
                                room.CenterY = parentRoom.CenterY - parentHalfHeight - halfHeight;
                            } break;

                        case BuilderDirection.East:
                            {
                                room.X       = parentRoom.X + 1;
                                room.Y       = parentRoom.Y;
                                room.CenterX = parentRoom.CenterX + parentHalfWidth + halfWidth;
                                room.CenterY = parentRoom.CenterY;
                            } break;

                        case BuilderDirection.South:
                            {
                                room.X       = parentRoom.X;
                                room.Y       = parentRoom.Y + 1;
                                room.CenterX = parentRoom.CenterX;
                                room.CenterY = parentRoom.CenterY + parentHalfHeight + halfHeight;
                            } break;

                        case BuilderDirection.West:
                            {
                                room.X       = parentRoom.X - 1;
                                room.Y       = parentRoom.Y;
                                room.CenterX = parentRoom.CenterX - parentHalfWidth - halfWidth;
                                room.CenterY = parentRoom.CenterY;
                            } break;
                        }

                        parentRoom.Connections.Add(new DungeonRoomConnectionMeta
                        {
                            From      = parentRoom,
                            To        = room,
                            Direction = direction,
                        });
                        room.Connections.Add(new DungeonRoomConnectionMeta
                        {
                            From      = room,
                            To        = parentRoom,
                            Direction = Opposite(direction),
                        });

                        Debug.Log($"Spawn room[{room.Id}] ([{room.X},{room.Y}], {room.Shape}:{room.Width}x{room.Height}:{room.Theme}) to the {direction} of room[{parentRoom.Id}]\n");
                    }
                    else
                    {
                        Debug.Log($"Spawn first room[{room.Id}] ([{room.X},{room.Y}], {room.Shape}:{room.Width}x{room.Height}:{room.Theme})\n");
                    }

                    if (IsOccupied.ContainsKey(new int2(room.X, room.Y)) == false)
                    {
                        IsOccupied.Add(new int2(room.X, room.Y), true);
                    }

                    context.RoomStack.Push(room);
                    context.AllRooms.Add(room);
                    return(TaskStatus.Success);
                });
            }
            End();
            return(this);
        }