Пример #1
0
        public GameMap(Game game, string Zone)
            : base(game)
        {
            changesMade = false;

            mapFile = zonesFolder + "\\" + Zone + "\\" + mapFile;

            //Loading the Map
            xmlLoaderMap = XElement.Load(mapFile);
            IEnumerable <XElement> mapData = from element in xmlLoaderMap.Elements() select element;
            int elementCount = 0;

            int w = 0;
            int h = 0;

            slotList = new List <Slot>();

            foreach (XElement data in mapData)
            {
                numberOfItemsToRead += data.Elements().Count();
            }

            List <Slot> historySlotList = ProfilesManager.LoadZoneHistory();

            numberOfItemsToRead += historySlotList.Count;

            foreach (XElement data in mapData) //3 categories: Map Dimensions, Map Elements, Slots
            {
                switch (elementCount)
                {
                case 0:     //reads the map dimensions and loades the ground

                    w = Convert.ToInt32(data.Element("Width").Value);
                    h = Convert.ToInt32(data.Element("Height").Value);

                    for (int i = 0; i < w; i++)
                    {
                        for (int j = 0; j < h; j++)
                        {
                            Sprite ground = new Sprite(game, GraphicsCollection.GetPack("ground"));
                            ground.StackOrder  = stackOrder_map;
                            ground.XRelative   = ground.Width * i;
                            ground.YRelative   = ground.Height * j;
                            ground.FrameNumber = 0;
                            AddChild(ground);

                            width  = w * ground.Width;
                            height = h * ground.Height;
                        }
                    }
                    numberOfItemsRead++;
                    DisplayManager.ChangePreloaderPercent(PercentCreated);
                    break;

                case 1:     //reads the map elements
                    IEnumerable <XElement> mapElements = from element in data.Elements() select element;

                    foreach (XElement mapElement in mapElements)
                    {
                        string TexturePack = Convert.ToString(mapElement.Element("TexturePack").Value);
                        int    frameNumber = Convert.ToInt32(mapElement.Element("FrameNumber").Value);
                        int    X           = Convert.ToInt32(mapElement.Element("X").Value);
                        int    Y           = Convert.ToInt32(mapElement.Element("Y").Value);

                        Sprite mapSpriteElement = new Sprite(game, GraphicsCollection.GetPack(TexturePack).Frames[frameNumber]);
                        mapSpriteElement.StackOrder  = stackOrder_map;
                        mapSpriteElement.XRelative   = X;
                        mapSpriteElement.YRelative   = Y;
                        mapSpriteElement.FrameNumber = 0;
                        AddChild(mapSpriteElement);

                        numberOfItemsRead++;
                        DisplayManager.ChangePreloaderPercent(PercentCreated);
                    }
                    break;

                case 2:     //reads the slots
                    IEnumerable <XElement> slotsData = from element in data.Elements() select element;

                    foreach (XElement slotElement in slotsData)
                    {
                        ConstructionType slot_constructionType = (ConstructionType)Enum.Parse(typeof(ConstructionType), Convert.ToString(slotElement.Element("ConstructionType").Value));

                        int X = Convert.ToInt32(slotElement.Element("X").Value);
                        int Y = Convert.ToInt32(slotElement.Element("Y").Value);

                        Slot slot;
                        slot             = new Slot(game, slot_constructionType);
                        slot.StackOrder  = stackOrder_slots;
                        slot.XSlotCenter = X;
                        slot.YSlotCenter = Y;
                        slot.Visible     = false;
                        slot.OnSelected += new EventHandler(slot_OnSelected);
                        AddChild(slot);
                        slotList.Add(slot);

                        numberOfItemsRead++;
                        DisplayManager.ChangePreloaderPercent(PercentCreated);
                    }
                    break;
                    //case 3, se det slot-ul dupa x si y si se face instanta slot
                    // se face instanta reservation
                    //se trimit SetConstructionReservation(slot+reservation)
                }
                elementCount++;
            }

            //reading zone history
            for (int i = 0; i < slotList.Count; i++)
            {
                for (int j = 0; j < historySlotList.Count; j++)
                {
                    if (slotList[i].ConstructionType == historySlotList[j].ConstructionType &&
                        slotList[i].XSlotCenter == historySlotList[j].XSlotCenter &&
                        slotList[i].YSlotCenter == historySlotList[j].YSlotCenter)
                    {
                        slotList[i].ReservationList = historySlotList[j].ReservationList;
                        numberOfItemsRead++;
                        DisplayManager.ChangePreloaderPercent(PercentCreated);
                        break;
                    }
                }
            }

            Game.Services.AddService(typeof(GameMap), this);

            //test only, the population will be loaded from the saved game
            GameManager.LoadHistory();
            changesMade = true;
            GameManager.UpdateYear(2010);

            HideSlots();

            DisplayManager.GameMap_HidePreloader();
        }