Beispiel #1
0
        private List <BuilderDirection> GetValidExits(DungeonContext context, DungeonRoomMeta r)
        {
            var result = context.Factory.CreateList <BuilderDirection>();

            result.Clear();

            if (IsOccupied.ContainsKey(new int2(r.X, r.Y - 1)) == false)
            {
                result.Add(BuilderDirection.North);
            }
            if (IsOccupied.ContainsKey(new int2(r.X + 1, r.Y)) == false)
            {
                result.Add(BuilderDirection.East);
            }
            if (IsOccupied.ContainsKey(new int2(r.X, r.Y + 1)) == false)
            {
                result.Add(BuilderDirection.South);
            }
            if (IsOccupied.ContainsKey(new int2(r.X - 1, r.Y)) == false)
            {
                result.Add(BuilderDirection.West);
            }

            return(result);
        }
 public void SetMeta(DungeonRoomMeta meta)
 {
     _meta = meta;
     transform.position = new Vector3(_meta.CenterX, _meta.CenterY);
     if (_roomTile != null)
     {
         SetSprite(_roomTile, new Dungeon.MapValue {
             Theme = _meta.Theme, Index = DungeonTile.Index.WorldMap
         }, false, false);
     }
 }
Beispiel #3
0
        public DungeonRoom GetRoom(DungeonRoomMeta meta)
        {
            foreach (var room in Rooms)
            {
                if (room.Meta.Id == meta.Id)
                {
                    return(room);
                }
            }

            return(null);
        }
Beispiel #4
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);
        }