예제 #1
0
 // Down-Left:
 private void CheckPositionView_4(LevelObject objToCheck)
 {
     if (objToCheck.IsViewable == true)
     {
         if (objToCheck.GetType() != (new Wall()).GetType() && objToCheck.GetType() != (new Door()).GetType())
         {
             if (toDisplay_4.Any(x => x.Position_x == objToCheck.Position_x - 1 && x.Position_y == objToCheck.Position_y))
             {
                 toDisplay_4[toDisplay_4.FindIndex(x => x.Position_x == objToCheck.Position_x - 1 && x.Position_y == objToCheck.Position_y)].IsViewable = true;
             }
             if (toDisplay_4.Any(x => x.Position_x == objToCheck.Position_x && x.Position_y == objToCheck.Position_y + 1))
             {
                 toDisplay_4[toDisplay_4.FindIndex(x => x.Position_x == objToCheck.Position_x && x.Position_y == objToCheck.Position_y + 1)].IsViewable = true;
             }
         }
     }
 }
예제 #2
0
        public void Generator()
        {
            //Console.WriteLine("Loading, Please Wait");
            Objects.Clear();
            //int GeneratorPos_x = rnd.Next(0, LevelLength);
            //int GeneratorPos_y = rnd.Next(0, LevelLength);
            int GeneratorPos_x = LevelLength / 2;
            int GeneratorPos_y = LevelLength / 2;
            // Variable Floors determines how many floor elements there are:
            int Floors = 0;

            // We cover the whole level with walls (stones or whatever is under the ground):
            for (int l = 0; l < LevelLength; l++)
            {
                for (int w = 0; w < LevelLength; w++)
                {
                    Objects.Add(new Wall()
                    {
                        Position_x = l, Position_y = w
                    });
                }
            }
            // Than we add single floor elements until 80% of the level is floor
            while (Floors < Objects.Count * 0.8)
            {
                int newPos_x = 0;
                int newPos_y = 0;
                // Determine the next position:
                int k = rnd.Next(0, 4);
                //
Up:
                if (k == 0)
                {
                    newPos_x = GeneratorPos_x;
                    newPos_y = GeneratorPos_y - 1;
                }
                // Move Down:
                if (k == 1)
                {
                    newPos_x = GeneratorPos_x;
                    newPos_y = GeneratorPos_y + 1;
                }
                // Move Right:
                if (k == 2)
                {
                    newPos_x = GeneratorPos_x + 1;
                    newPos_y = GeneratorPos_y;
                }
                // Move Left:
                if (k == 3)
                {
                    newPos_x = GeneratorPos_x - 1;
                    newPos_y = GeneratorPos_y;
                }

                if (LevelLength - newPos_x < 28 || newPos_x < 28 || LevelLength - newPos_y < 28 || newPos_y < 28)
                {
                    continue;
                }

                // Check if there is a room:
                bool roomCheck = false;
                foreach (Room room in Rooms)
                {
                    if (room.Objects.Any(x => (x.Position_x == newPos_x && x.Position_y == newPos_y)))
                    {
                        roomCheck = true;
                        break;
                    }
                }

                if (roomCheck)
                {
                    continue;
                }

                k = rnd.Next(0, 11);
                // Generate floor
                if (k < 11)
                {
                    if (PutFloor(newPos_x, newPos_y))
                    {
                        GeneratorPos_x = newPos_x;
                        GeneratorPos_y = newPos_y;
                        Floors++;
                    }
                }
            }

            // Generating from 9 to 23 rooms:

            List <LevelObject> floors = Objects.FindAll(x => x.GetType() == (new Floor()).GetType());

            for (int i = 0; i < rnd.Next(9, 24);)
            {
                LevelObject floorToChange = floors[rnd.Next(0, floors.Count)];
                if (floorToChange.Position_x > 22 && floorToChange.Position_y > 22 && LevelLength - floorToChange.Position_x > 22 && LevelLength - floorToChange.Position_y > 22)
                {
                    // Check if the is no evident way to the first door:
                    if (Objects.Find(obj => (obj.Position_x == floorToChange.Position_x + 1 && obj.Position_y == floorToChange.Position_y)).GetType() == (new Wall()).GetType())
                    {
                        continue;
                    }
                    if (Objects.Find(obj => (obj.Position_x == floorToChange.Position_x - 1 && obj.Position_y == floorToChange.Position_y)).GetType() == (new Wall()).GetType())
                    {
                        continue;
                    }
                    if (Objects.Find(obj => (obj.Position_x == floorToChange.Position_x && obj.Position_y == floorToChange.Position_y + 1)).GetType() == (new Wall()).GetType())
                    {
                        continue;
                    }
                    if (Objects.Find(obj => (obj.Position_x == floorToChange.Position_x && obj.Position_y == floorToChange.Position_y - 1)).GetType() == (new Wall()).GetType())
                    {
                        continue;
                    }
                    Room room = GenRoom(floorToChange.Position_x, floorToChange.Position_y);
                    if (!floors.Any(x => x.Position_x == room.D2_Pos_x && x.Position_y == room.D2_Pos_y))
                    {
                        continue;
                    }
                    // Check if it intersects with an existing room:
                    bool cont = false;
                    foreach (Room room_to_check in Rooms)
                    {
                        foreach (LevelObject obj in room_to_check.Objects)
                        {
                            if (room.Objects.Any(x => x.Position_x == obj.Position_x && x.Position_y == obj.Position_y))
                            {
                                cont = true;
                                break;
                            }
                        }
                        if (cont)
                        {
                            break;
                        }
                    }

                    // Check if the second door leads to nowhere:
                    if (Objects.Find(obj => (obj.Position_x == room.D2_Pos_x + 1 && obj.Position_y == room.D2_Pos_y)).GetType() == (new Wall()).GetType())
                    {
                        cont = true;
                    }
                    if (Objects.Find(obj => (obj.Position_x == room.D2_Pos_x - 1 && obj.Position_y == room.D2_Pos_y)).GetType() == (new Wall()).GetType())
                    {
                        cont = true;
                    }
                    if (Objects.Find(obj => (obj.Position_x == room.D2_Pos_x && obj.Position_y == room.D2_Pos_y + 1)).GetType() == (new Wall()).GetType())
                    {
                        cont = true;
                    }
                    if (Objects.Find(obj => (obj.Position_x == room.D2_Pos_x && obj.Position_y == room.D2_Pos_y - 1)).GetType() == (new Wall()).GetType())
                    {
                        cont = true;
                    }

                    if (cont)
                    {
                        continue;
                    }


                    Rooms.Add(room);
                    foreach (LevelObject obj in room.Objects)
                    {
                        Objects.RemoveAll(x => x.Position_x == obj.Position_x && x.Position_y == obj.Position_y);
                        Objects.Add(obj);
                    }
                    i++;
                }
                else
                {
                    continue;
                }
            }
        }
예제 #3
0
        public void Generate()
        {
            Random rnd = new Random();

            Length = (2 * rnd.Next(3, 6)) - 1;
            Width  = (2 * rnd.Next(3, 6)) - 1;
            int z = rnd.Next(0, 2);

            if (z == 0)
            {
                Center_x = ((Length - 1) / 2) + D1_Pos_x;
            }
            else
            {
                Center_x = ((Length - 1) / 2) + D1_Pos_x - (Length - 1);
            }
            Center_y = (Width - 1) / 2 + D1_Pos_y - rnd.Next(1, Width - 2);
            // Стены по горизонтали:
            // Horizontal walls:
            for (int l = 0; l < Length; l++)
            {
                Wall wall_up = new Surroundings.Wall()
                {
                    Position_x = Convert.ToInt32(Center_x - ((Length - 1) * 0.5)) + l, Position_y = Convert.ToInt32(Center_y - ((Width - 1) * 0.5))
                };
                Wall wall_down = new Surroundings.Wall()
                {
                    Position_x = Convert.ToInt32(Center_x - ((Length - 1) * 0.5)) + l, Position_y = Convert.ToInt32(Center_y + ((Width - 1) * 0.5))
                };
                Objects.Add(wall_up);
                Objects.Add(wall_down);
            }
            // Стены по вертикали:
            // Vertical walls:
            for (int w = 0; w < Width; w++)
            {
                Wall wall_left = new Surroundings.Wall()
                {
                    Position_x = Convert.ToInt32(Center_x - ((Length - 1) * 0.5)), Position_y = Convert.ToInt32(Center_y - ((Width - 1) * 0.5)) + w
                };
                Wall wall_rigth = new Surroundings.Wall()
                {
                    Position_x = Convert.ToInt32(Center_x + ((Length - 1) * 0.5)), Position_y = Convert.ToInt32(Center_y - ((Width - 1) * 0.5)) + w
                };
                Objects.Add(wall_left);
                Objects.Add(wall_rigth);
            }
            // Заполнение полом:
            // Populating with floor:
            for (int w = 0; w < Width - 2; w++)
            {
                for (int l = 0; l < Length - 2; l++)
                {
                    Floor floor = new Floor()
                    {
                        Position_x = Convert.ToInt32(Center_x - (((Length - 1) * 0.5) - 1)) + l, Position_y = Convert.ToInt32(Center_y - (((Width - 1) * 0.5) - 1)) + w
                    };
                    Objects.Add(floor);
                }
            }
            // Добавление дверей (ПОКА ТОЛЬКО 2):
            // Adding doors (ONLY 2 DOORS FOR NOW):

            // Первая дверь:
            // The first door:

            Objects.RemoveAll(x => x.Position_x == D1_Pos_x && x.Position_y == D1_Pos_y);
            Objects.Add(new Door()
            {
                Position_x = D1_Pos_x, Position_y = D1_Pos_y
            });


            // Делаем контейнер всех стен комнаты, чтобы выбрать из них случаный для замены на дверь:
            // We create a container for all walls the room has to choose a random wall and replace it with a door:

            List <LevelObject> walls = Objects.FindAll(x => x.GetType() == (new Wall()).GetType());
            bool check = false;

            while (!check)
            {
                LevelObject wallToDoor = walls[(rnd.Next(0, walls.Count))];
                // Проверяем, нет ли там уже двери и не выбрана ли стена в углу:
                // Check if there is a door already and if the chosen wall is in the corner
                if (
                    (((wallToDoor.Position_x == Convert.ToInt32(Center_x - ((Length - 1) * 0.5)))) && (wallToDoor.Position_y == Convert.ToInt32(Center_y - ((Width - 1) * 0.5)))) ||
                    (((wallToDoor.Position_x == Convert.ToInt32(Center_x + ((Length - 1) * 0.5)))) && (wallToDoor.Position_y == Convert.ToInt32(Center_y - ((Width - 1) * 0.5)))) ||
                    (((wallToDoor.Position_x == Convert.ToInt32(Center_x + ((Length - 1) * 0.5)))) && (wallToDoor.Position_y == Convert.ToInt32(Center_y + ((Width - 1) * 0.5)))) ||
                    (((wallToDoor.Position_x == Convert.ToInt32(Center_x - ((Length - 1) * 0.5)))) && (wallToDoor.Position_y == Convert.ToInt32(Center_y + ((Width - 1) * 0.5))))
                    )
                {
                    check = false;
                    continue;
                }
                //// Chech if the door leads to a wall:
                if ((Objects.Find(x => (x.Position_x == wallToDoor.Position_x && x.Position_y != wallToDoor.Position_y)).GetType() != (new Door()).GetType()))
                {
                    Door door = new Door()
                    {
                        Position_x = wallToDoor.Position_x, Position_y = wallToDoor.Position_y, IsUsed = false
                    };
                    Objects.RemoveAll(x => x.Position_x == wallToDoor.Position_x && x.Position_y == wallToDoor.Position_y);
                    Objects.Add(door);
                    //Doors.Add(door);
                    D2_Pos_x = door.Position_x;
                    D2_Pos_y = door.Position_y;
                    check    = true;
                }
                else
                {
                    check = false;
                }
            }
        }