/* SAVE GAME SECTION */ public void SaveGame(bool allData) { JsonManager.ClearArray(); Dictionary <string, object> playerData = new Dictionary <string, object>(); Dictionary <string, object> slotData = new Dictionary <string, object>(); Dictionary <string, object> shortcutData = new Dictionary <string, object>(); Dictionary <string, object> objectivesData = new Dictionary <string, object>(); /* PLAYER PAIRS */ if (allData) { JsonManager.AddPair("scene", UnityEngine.SceneManagement.SceneManager.GetActiveScene().name); JsonManager.AddPair("dateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); playerData.Add("playerPosition", player.transform.position); playerData.Add("cameraRotation", player.GetComponentInChildren <MouseLook>().GetRotation()); } playerData.Add("playerHealth", player.GetComponent <HealthManager>().Health); /* END PLAYER PAIRS */ /* ITEMSWITCHER PAIRS */ Dictionary <string, object> switcherData = new Dictionary <string, object> { { "switcherActiveItem", switcher.currentItem }, { "switcherLightObject", switcher.currentLightObject }, { "switcherWeaponItem", switcher.weaponItem } }; foreach (var Item in switcher.ItemList) { Dictionary <string, object> ItemInstances = new Dictionary <string, object>(); foreach (var Instance in Item.GetComponents <MonoBehaviour>().Where(x => typeof(ISaveableArmsItem).IsAssignableFrom(x.GetType())).ToArray()) { ItemInstances.Add(Instance.GetType().Name.Replace(" ", "_"), (Instance as ISaveableArmsItem).OnSave()); switcherData.Add("switcher_item_" + Item.name.ToLower().Replace(" ", "_"), ItemInstances); } } /* END ITEMSWITCHER PAIRS */ /* INVENTORY PAIRS */ foreach (var slot in inventory.Slots) { if (slot.GetComponent <InventorySlot>().itemData != null) { InventoryItemData itemData = slot.GetComponent <InventorySlot>().itemData; Dictionary <string, object> itemDataArray = new Dictionary <string, object> { { "slotID", itemData.slotID }, { "itemID", itemData.item.ID }, { "itemAmount", itemData.m_amount }, { "itemData", itemData.customData } }; slotData.Add("inv_slot_" + inventory.Slots.IndexOf(slot), itemDataArray); } else { slotData.Add("inv_slot_" + inventory.Slots.IndexOf(slot), "null"); } } Dictionary <string, object> inventoryData = new Dictionary <string, object> { { "inv_slots_count", inventory.Slots.Count }, { "slotsData", slotData } }; /* INVENTORY SHORTCUTS PAIRS */ if (inventory.Shortcuts.Count > 0) { Dictionary <string, object> shortcutsData = new Dictionary <string, object>(); foreach (var shortcut in inventory.Shortcuts) { Dictionary <string, object> shortcutsDataPairs = new Dictionary <string, object> { { "itemID", shortcut.item.ID }, { "shortcutKey", shortcut.shortcutKey.ToString() } }; shortcutsData.Add("shortcut_" + shortcut.slot, shortcutsDataPairs); } inventoryData.Add("shortcutsData", shortcutsData); } /* INVENTORY FIXED CONTAINER PAIRS */ if (inventory.FixedContainerData.Count > 0) { inventoryData.Add("fixedContainerData", inventory.GetFixedContainerData()); } /* END INVENTORY PAIRS */ /* OBJECTIVE PAIRS */ if (objectives.objectiveCache.Count > 0) { foreach (var obj in objectives.objectiveCache) { Dictionary <string, object> objectiveData = new Dictionary <string, object> { { "toComplete", obj.toComplete }, { "isCompleted", obj.isCompleted } }; objectivesData.Add(obj.identifier.ToString(), objectiveData); } } /* END OBJECTIVE PAIRS */ //Add data pairs to serialization buffer JsonManager.AddPair("playerData", playerData); JsonManager.AddPair("itemSwitcherData", switcherData); JsonManager.AddPair("inventoryData", inventoryData); JsonManager.AddPair("objectivesData", objectivesData); //Add all saveables if (allData && saveableDataPairs.Length > 0) { foreach (var Pair in saveableDataPairs) { if (Pair.BlockType == SaveableDataPair.DataBlockType.ISaveable) { var data = (Pair.Instance as ISaveable).OnSave(); if (data != null) { JsonManager.AddPair(Pair.BlockKey, data); } } else if (Pair.BlockType == SaveableDataPair.DataBlockType.Attribute) { Dictionary <string, object> attributeFieldPairs = new Dictionary <string, object>(); if (Pair.FieldData.Length > 0) { foreach (var Field in Pair.FieldData) { FieldInfo fieldInfo = Pair.Instance.GetType().GetField(Field); if (fieldInfo.FieldType == typeof(Color) || fieldInfo.FieldType == typeof(KeyCode)) { if (fieldInfo.FieldType == typeof(Color)) { attributeFieldPairs.Add(GetAttributeKey(fieldInfo), string.Format("#{0}", ColorUtility.ToHtmlStringRGBA((Color)Pair.Instance.GetType().InvokeMember(Field, BindingFlags.GetField, null, Pair.Instance, null)))); } else { attributeFieldPairs.Add(GetAttributeKey(fieldInfo), Pair.Instance.GetType().InvokeMember(Field, BindingFlags.GetField, null, Pair.Instance, null).ToString()); } } else { attributeFieldPairs.Add(GetAttributeKey(fieldInfo), Pair.Instance.GetType().InvokeMember(Field, BindingFlags.GetField, null, Pair.Instance, null)); } } } else { Debug.LogError("Empty Fields Data: " + Pair.BlockKey); } JsonManager.AddPair(Pair.BlockKey, attributeFieldPairs); } } } //Serialize all pairs from buffer SerializeSaveData(!allData); }