Пример #1
0
        public async Task Genrate()
        {
            _startingpos = (StartingPosition)NumGen.Next(0, 5) /*StartingPosition.Center*/;
            LogToUiThread($"Starting position : {_startingpos}");

            int roomcount = _dp.RoomNumber.Next();

            LogToUiThread($"Generating {roomcount} rooms");
            for (int i = 0; i < roomcount; i++)
            {
                bool      isFirst = i == 0;
                Direction dir     = Direction.North;
                if (isFirst)
                {
                    dir = GetDirByStartupPos();
                }
                else
                {
                    dir = (Direction)NumGen.Next(0, 4);
                }


                var r = await GenerateRoom(dir, (Corridors.Count > 0)?Corridors[i - 1] : null, (isFirst));

                Rooms.Add(r);

                //si dernière salle, pas couloir
                if (i < roomcount - 1)
                {
                    var c = await GenerateCorridor(r);

                    Corridors.Add(c);
                }
            }
        }
Пример #2
0
        private Direction GetDirByStartupPos()
        {
            switch (_startingpos)
            {
            case StartingPosition.LowerLeft:
                return((Direction)NumGen.Next(0, 2));

            case StartingPosition.UpperLeft:
                return((Direction)NumGen.Next(1, 3));

            case StartingPosition.UpperRight:
                return((Direction)NumGen.Next(2, 4));

            case StartingPosition.LowerRight:
                int[] dirs = new int[2] {
                    (int)Direction.North, (int)Direction.West
                };
                return((Direction)dirs[NumGen.Next(0, dirs.Length)]);

            case StartingPosition.Center:
                return((Direction)NumGen.Next(0, 4));

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #3
0
        private async Task <Room> VerifyRoom(bool isfirst, Corridor corridor)
        {
            return(await Task.Run(() =>
            {
                Room room = null;
                bool retry = true;
                int tries = 0;
                while (retry)
                {
                    room = new Room
                    {
                        Height = GetRoomHeight(),
                        Width = GetRoomWidth(),
                    };
                    if (isfirst)
                    {
                        switch (_startingpos)
                        {
                        case StartingPosition.LowerLeft:
                            room.Xpos = 1;
                            room.YPos = _dp.AreaHeight - 1 - room.Height;
                            break;

                        case StartingPosition.LowerRight:
                            room.Xpos = _dp.AreaWidth - 1 - room.Width;
                            room.YPos = _dp.AreaHeight - 1 - room.Height;
                            break;

                        case StartingPosition.UpperRight:
                            room.Xpos = _dp.AreaWidth - 1 - room.Width;
                            room.YPos = 1;
                            break;

                        case StartingPosition.UpperLeft:
                            room.Xpos = 1;
                            room.YPos = 1;
                            break;

                        case StartingPosition.Center:
                            room.Xpos = _dp.AreaWidth / 2 - room.Width / 2;
                            room.YPos = _dp.AreaHeight / 2 - room.Height / 2;
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                    else if (corridor != null)
                    {
                        int entrance;
                        switch (corridor.Direction)
                        {
                        case Direction.North:
                            entrance = NumGen.Next(0, room.Width);
                            room.YPos = corridor.YPos - room.Height;

                            room.Xpos = corridor.Xpos - entrance;
                            break;

                        case Direction.East:
                            entrance = NumGen.Next(0, room.Height);

                            room.Xpos = corridor.Xpos + corridor.Width;
                            room.YPos = corridor.YPos - entrance;
                            break;

                        case Direction.South:
                            entrance = NumGen.Next(0, room.Width);

                            room.YPos = corridor.YPos + corridor.Height;

                            room.Xpos = corridor.Xpos - entrance;
                            break;

                        case Direction.West:
                            entrance = NumGen.Next(0, room.Height);
                            room.Xpos = corridor.Xpos - room.Width;

                            room.YPos = corridor.YPos - entrance;
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                        room.Entrance = entrance;
                    }

                    retry = CheckArea(room);
                    tries++;
                    if (tries >= maxtriesRoom)
                    {
                        LogToUiThread($"Verify Room tries : {tries}");

                        throw new GenerationabortedException("Generation canceled du to too many tries");
                    }
                    Thread.Sleep(10);
                }

                LogToUiThread($"Verify Room tries : {tries}");

                return room;
            }));
        }
Пример #4
0
        private async Task <Corridor> VerifyCorridor(Room room)
        {
            return(await Task.Run(() =>
            {
                bool retry = true;
                Corridor c = null;
                int tries = 0;
                while (retry)
                {
                    c = new Corridor();
                    var dir = room.ExitDirection;
                    c.Direction = dir;
                    switch (dir)
                    {
                    case Direction.North:
                        c.Width = NumGen.Next(1, 2);
                        c.Height = _dp.CorridorLength.Next();
                        c.Xpos = room.ExitXPos;
                        c.YPos = room.ExitYPos - c.Height;
                        //c.Height = Helpers.Clamp(c.Height, 1, _dp.AreaHeight - 1);
                        break;

                    case Direction.East:
                        c.Xpos = room.ExitXPos + 1;
                        c.YPos = room.ExitYPos;
                        c.Height = NumGen.Next(1, 2);
                        c.Width = _dp.CorridorLength.Next();
                        //c.Width = Helpers.Clamp(c.Width, 1, _dp.AreaWidth - 1);
                        break;

                    case Direction.South:
                        c.Xpos = room.ExitXPos;
                        c.YPos = room.ExitYPos + 1;
                        c.Width = NumGen.Next(1, 2);
                        c.Height = _dp.CorridorLength.Next();
                        //c.Height = Helpers.Clamp(c.Height, 1, _dp.AreaHeight - 1);
                        break;

                    case Direction.West:
                        c.Height = NumGen.Next(1, 2);
                        c.Width = _dp.CorridorLength.Next();
                        c.Xpos = room.ExitXPos - c.Width;
                        c.YPos = room.ExitYPos;
                        //c.Width = Helpers.Clamp(c.Width, 1, _dp.AreaWidth - 1);

                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    retry = CheckArea(c);
                    tries++;
                    if (tries >= maxtriesCorridor)
                    {
                        LogToUiThread($"Verify corridor tries : {tries}");

                        throw new GenerationabortedException("Generation canceled du to too many tries");
                    }
                    Thread.Sleep(10);
                }

                LogToUiThread($"Verify corridor tries : {tries}");

                return c;
            })
                   );
        }
Пример #5
0
 private Direction GetExitDirection()
 {
     return((Direction)NumGen.Next(0, 4));
 }