Exemple #1
0
        /// konstruktor - tworzy Mapę z danej tablicy charów
        public Map(char[,] ker)
        {
            World = new Room[ker.GetLength(0), ker.GetLength(1)];

            for (int i = 0; i < ker.GetLength(0); ++i)
            {
                for (int j = 0; j < ker.GetLength(1); ++j)
                {
                    Bandage bandage = null;
                    Weapon  weapon  = null;
                    Enemy   enemy   = null;
                    switch (ker[i, j])
                    {
                    case 'W':
                        weapon      = new Weapon();
                        World[i, j] = new Room(null, null, weapon, i, j);
                        break;

                    case 'B':
                        bandage     = new Bandage();
                        World[i, j] = new Room(null, bandage, null, i, j);
                        break;

                    case '1':
                        enemy       = new Goblin();
                        World[i, j] = new Room(enemy, null, null, i, j);
                        break;

                    case '2':
                        enemy       = new Orc();
                        World[i, j] = new Room(enemy, null, null, i, j);
                        break;

                    case '3':
                        enemy       = new OrcMage();
                        bandage     = new Bandage();
                        World[i, j] = new Room(enemy, bandage, null, i, j);
                        break;

                    case '*':
                        World[i, j] = new Room(null, null, null, i, j);
                        break;

                    case 'S':
                        startX      = i;
                        startY      = j;
                        World[i, j] = new Room(null, null, null, i, j);
                        break;

                    case 'E':
                        endX        = i;
                        endY        = j;
                        World[i, j] = new Room(null, null, null, i, j);
                        break;

                    default:
                        break;
                    }
                }
            }
        }
        public static Dungeon MakeDungeon(Hero hero, string filename)
        {
            Dungeon dungeon = new Dungeon();

            string[] lines = System.IO.File.ReadAllLines(filename);

            dungeon.PointScaleFactor = Int32.Parse(lines[0]);
            foreach (string line in lines.Skip(1))
            {
                string[] lineInfo = line.Split(' ');

                if (lineInfo[0] == "room:" || lineInfo[0] == "door:")
                {
                    Point topLeft = new Point(
                        Int32.Parse(lineInfo[1]), Int32.Parse(lineInfo[2]));
                    Point bottomRight = new Point(
                        Int32.Parse(lineInfo[3]), Int32.Parse(lineInfo[4]));

                    if (lineInfo[0] == "room:")
                    {
                        dungeon.Size = Math.Max(Math.Max(bottomRight.X, bottomRight.Y), dungeon.Size);

                        Room r = new Room(topLeft, bottomRight);
                        dungeon.Rooms.Add(r);
                    }
                    else
                    {
                        Room room1 = null;
                        Room room2 = null;

                        // each door has two rooms
                        foreach (Room room in dungeon.Rooms)
                        {
                            if (room.isInside(topLeft))
                            {
                                room1 = room;
                            }
                            else if (room.isInside(bottomRight))
                            {
                                room2 = room;
                            }
                        }

                        Point doorPoint;
                        if (room1.isInside(topLeft) && room2.isInside(bottomRight))
                        {
                            room1.AddDoor(topLeft, room2);
                            room2.AddDoor(bottomRight, room1);
                        }
                    }
                }
                else
                {
                    Point loc = new Point(Int32.Parse(lineInfo[1]), Int32.Parse(lineInfo[2]));

                    foreach (Room room in dungeon.Rooms)
                    {
                        if (room.isInside(loc))
                        {
                            if (lineInfo[0] == "monster:")
                            {
                                Monster monster;
                                if (lineInfo[3] == "O")
                                {
                                    monster = new Orc();
                                }
                                else
                                {
                                    monster = new Goblin();
                                }

                                monster.SetDungeon(dungeon);
                                monster.Pos = loc;
                                room.AddMonster(monster);
                            }
                            else if ((lineInfo[0] == "item:"))
                            {
                                Item item;
                                if (lineInfo[3] == "A")
                                {
                                    item = new Armor();
                                    ((Armor)item).Resistance = Int32.Parse(lineInfo[4]);
                                }
                                else
                                {
                                    item = new Weapon();
                                    ((Weapon)item).Damage = Int32.Parse(lineInfo[4]);
                                }

                                item.Pos = loc;
                                room.AddItem(item);
                            }
                        }
                    }
                }
            }

            List <Point> posWrap = new List <Point>();
            int          len     = 0;

            foreach (var room in dungeon.Rooms)
            {
                for (int i = room.TopLeft.X; i < room.BottomRight.X; i++)
                {
                    for (int j = room.TopLeft.Y; j < room.BottomRight.Y; j++)
                    {
                        var point = new Point(i, j);

                        bool bad = false;
                        foreach (Monster monster in room.Monsters)
                        {
                            if (monster.Pos.X == point.X && monster.Pos.Y == point.Y)
                            {
                                bad = true;
                            }
                        }

                        if (!bad)
                        {
                            posWrap.Add(point);
                            len++;
                        }
                    }
                }
            }

            Random ran = new Random();
            Point  pos = posWrap[ran.Next(0, len - 1)];

            hero.SetDungeon(dungeon);
            hero.Pos = pos;

            foreach (Room room in dungeon.Rooms)
            {
                if (room.isInside(hero.Pos))
                {
                    hero.Room = room;
                    break;
                }
            }

            dungeon.Hero = hero;

            return(dungeon);
        }