Example #1
0
        public void LoadLevel(string fileName)
        {
            try
            {   // Open the text file using a stream reader.
                using (StreamReader sr = new StreamReader(fileName))
                {
                    // Read the stream to a string, and write the string to the console.
                    String data = sr.ReadToEnd();

                    var rawlines = Regex.Split(data, "\r\n");
                    Debug.WriteLine(rawlines.Count() + " ligne(s) chargée(s) à partir du fichier.");

                    // création d'une nouvelle liste de lignes + ajout des lignes non-vide
                    List <string> lines = new List <string>();
                    for (int y = 0; y < rawlines.Count(); y++)
                    {
                        if (!String.IsNullOrEmpty(rawlines[y]))
                        {
                            lines.Add(rawlines[y]);
                        }
                    }

                    if (lines.Count <= 0)
                    {
                        return;
                    }

                    var blockCount = Regex.Split(lines[0], ";").Count();

                    Debug.WriteLine(lines.Count() + " ligne(s) conservée(s).");
                    Debug.WriteLine(blockCount + " block(s) par ligne.");

                    CurrentLevel       = new Level();
                    CurrentLevel.Rooms = new Room[blockCount, lines.Count()];

                    for (int y = 0; y < lines.Count(); y++)
                    {
                        Debug.WriteLine(String.Format("Traitement de la ligne {0} sur {1}.", y, lines.Count() - 1));
                        var blocks = Regex.Split(lines[y], ";");

                        for (int x = 0; x < blocks.Count(); x++)
                        {
                            var block = blocks[x];

                            switch (block)
                            {
                            case "I":
                            case "R":
                            case "O":
                                var room = Room.CreateRoom(11, 9);
                                room.Position = new Position()
                                {
                                    X = x, Y = y
                                };
                                CurrentLevel.Rooms[x, y] = room;
                                break;

                            default:
                                break;
                            }
                        }
                    }

                    // ajout des portes
                    for (int y = 0; y < lines.Count(); y++)
                    {
                        for (int x = 0; x < blockCount; x++)
                        {
                            var room = CurrentLevel.Rooms[x, y];

                            Room nextRoom = null;

                            if (room == null)
                            {
                                continue;
                            }

                            // check si room à gauche
                            if (x > 0)
                            {
                                nextRoom = CurrentLevel.Rooms[x - 1, y];
                                if (nextRoom != null)
                                {
                                    var door = new Door();
                                    door.IsOpen  = true;
                                    door.X       = 0;
                                    door.Y       = 4;
                                    door.LeadsTo = nextRoom;
                                    room.AddDoor(door);
                                }
                            }
                            if (y > 0)
                            {
                                // check si room en haut
                                nextRoom = CurrentLevel.Rooms[x, y - 1];
                                if (nextRoom != null)
                                {
                                    var door = new Door();
                                    door.IsOpen  = true;
                                    door.X       = 5;
                                    door.Y       = 0;
                                    door.LeadsTo = nextRoom;
                                    room.AddDoor(door);
                                }
                            }
                            if (y < lines.Count() - 1)
                            {
                                // check si room en bas
                                nextRoom = CurrentLevel.Rooms[x, y + 1];
                                if (nextRoom != null)
                                {
                                    var door = new Door();
                                    door.IsOpen  = true;
                                    door.X       = 5;
                                    door.Y       = 8;
                                    door.LeadsTo = nextRoom;
                                    room.AddDoor(door);
                                }
                            }

                            if (x < blockCount - 1)
                            {
                                // check si room à droite
                                nextRoom = CurrentLevel.Rooms[x + 1, y];
                                if (nextRoom != null)
                                {
                                    var door = new Door();
                                    door.IsOpen  = true;
                                    door.X       = 10;
                                    door.Y       = 4;
                                    door.LeadsTo = nextRoom;
                                    room.AddDoor(door);
                                }
                            }


                            // ajout un mob aléatoirement
                            if (GameEngine.GenerateRandomNumber(5) != 1 && x > 0 && y > 0)
                            {
                                var mob = new Mob();
                                mob.Position.X = 2 + GameEngine.GenerateRandomNumber(blockCount - 4);
                                mob.Position.Y = 2 + GameEngine.GenerateRandomNumber(lines.Count() - 4);

                                mob.Sprite = GameEngine.GetInstance().MobSprites.ElementAt(GameEngine.GenerateRandomNumber(GameEngine.GetInstance().MobSprites.Count)).Value;

                                room.AddMob(mob);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("ERROR - The file could not be read : " + e.Message);
            }
        }
Example #2
0
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     GameEngine.Init(rootCanvas);
     GameEngine.GetInstance().ShowMainMenu();
     GameEngine.GetInstance().LoadLevel(@"..\..\Levels\Level1.csv");
 }