Esempio n. 1
0
        // Save player data
        public static void savePlayerData()
        {
            Logger.log("DataManager.savePlayerData method starting.");

            string filePath = _playersDirectory + string.Format("player_data_{0}.xml", _playerSlot);

            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                XDocument          doc = new XDocument();
                InventoryComponent inventoryComponent = _entityManager.getComponent("global", PlayerSystem.PLAYER_ID, ComponentType.Inventory) as InventoryComponent;
                ToolbarComponent   toolbarComponent   = _entityManager.getComponent("global", PlayerSystem.PLAYER_ID, ComponentType.Toolbar) as ToolbarComponent;
                EquipmentSystem    equipmentSystem    = _systemManager.getSystem(SystemType.Equipment) as EquipmentSystem;
                XElement           inventoryData      = new XElement("InventoryState");
                XElement           toolbarData        = new XElement("ToolbarState");
                List <XElement>    customFlagsData    = new List <XElement>();
                List <XElement>    customValuesData   = new List <XElement>();
                List <XElement>    customStringsData  = new List <XElement>();

                // Basic player data
                _playerData = new XElement(
                    "PlayerData",
                    new XAttribute("name", _playerName),
                    new XAttribute("slot", _playerSlot));

                // World map states
                foreach (WorldMapState worldMapState in _worldMapManager.worldMapStates.Values)
                {
                    XElement worldMapStateData = new XElement(
                        "WorldMapState",
                        new XAttribute("world_map_uid", worldMapState.definition.uid),
                        new XAttribute("discovered", worldMapState.discovered));

                    foreach (LevelIconState levelIconState in worldMapState.levelIconStates)
                    {
                        worldMapStateData.Add(
                            new XElement("LevelIconState",
                                         new XAttribute("level_icon_uid", levelIconState.definition.uid),
                                         new XAttribute("discovered", levelIconState.discovered),
                                         new XAttribute("finished", levelIconState.finished)));
                    }

                    foreach (LevelPathState levelPathState in worldMapState.levelPathState)
                    {
                        worldMapStateData.Add(new XElement("LevelPathState", new XAttribute("discovered", levelPathState.discovered)));
                    }

                    _playerData.Add(worldMapStateData);
                }

                // Quest states
                _playerData.Add(_questManager.createData());

                // Inventory states
                inventoryData.SetAttributeValue("slots", inventoryComponent.slots);
                foreach (KeyValuePair <int, ItemComponent> slotItemPair in inventoryComponent.inventory)
                {
                    inventoryData.Add(
                        new XElement(
                            "ItemState",
                            new XAttribute("slot_id", slotItemPair.Key),
                            new XAttribute("item_uid", slotItemPair.Value.definition.uid),
                            new XAttribute("quantity", slotItemPair.Value.state.quantity),
                            new XAttribute("current_range_limit", slotItemPair.Value.state.currentRangeLimit)));
                }
                _playerData.Add(inventoryData);

                // Toolbar states
                toolbarData.SetAttributeValue("slots", toolbarComponent.slots);
                toolbarData.SetAttributeValue("selected_index", toolbarComponent.selectedIndex);
                foreach (KeyValuePair <int, ItemComponent> slotItemPair in toolbarComponent.inventory)
                {
                    if (slotItemPair.Value != null)
                    {
                        toolbarData.Add(
                            new XElement(
                                "Slot",
                                new XAttribute("slot_id", slotItemPair.Key),
                                new XAttribute("inventory_slot", equipmentSystem.getInventorySlot(inventoryComponent, slotItemPair.Value))));
                    }
                }
                _playerData.Add(toolbarData);

                // Custom flags
                foreach (KeyValuePair <string, bool> uidFlagPair in _customFlags)
                {
                    customFlagsData.Add(new XElement("CustomFlag", new XAttribute("uid", uidFlagPair.Key), new XAttribute("value", uidFlagPair.Value)));
                }
                _playerData.Add(customFlagsData);

                // Custom values
                foreach (KeyValuePair <string, int> uidValuePair in _customValues)
                {
                    customValuesData.Add(new XElement("CustomValue", new XAttribute("uid", uidValuePair.Key), new XAttribute("value", uidValuePair.Value)));
                }
                _playerData.Add(customValuesData);

                // Custom values
                foreach (KeyValuePair <string, string> uidStringPair in _customStrings)
                {
                    customStringsData.Add(new XElement("CustomString", new XAttribute("uid", uidStringPair.Key), new XAttribute("value", uidStringPair.Value)));
                }
                _playerData.Add(customStringsData);

                doc.Add(_playerData);
                doc.Save(fs);
            }

            Logger.log("DataManager.savePlayerData method finished.");
        }