Beispiel #1
0
    /* LOAD SECTION */
    void LoadSavedSceneData(bool allData)
    {
        if (allData)
        {
            var posToken = JsonManager.Json()["playerData"]["playerPosition"];
            player.transform.position = posToken.ToObject <Vector3>();

            var rotToken = JsonManager.Json()["playerData"]["cameraRotation"];
            player.GetComponentInChildren <MouseLook>().SetRotation(rotToken.ToObject <Vector2>());
        }

        var healthToken = JsonManager.Json()["playerData"]["playerHealth"];

        player.GetComponent <HealthManager>().Health = (float)healthToken;

        switcher.currentLightObject = (int)JsonManager.Json()["itemSwitcherData"]["switcherLightObject"];
        switcher.weaponItem         = (int)JsonManager.Json()["itemSwitcherData"]["switcherWeaponItem"];

        //Deserialize ItemSwitcher Item Data
        foreach (var Item in switcher.ItemList)
        {
            JToken ItemToken = JsonManager.Json()["itemSwitcherData"]["switcher_item_" + Item.name.ToLower().Replace(" ", "_")];

            foreach (var Instance in Item.GetComponents <MonoBehaviour>().Where(x => typeof(ISaveableArmsItem).IsAssignableFrom(x.GetType())).ToArray())
            {
                (Instance as ISaveableArmsItem).OnLoad(ItemToken[Instance.GetType().Name.Replace(" ", "_")]);
            }
        }

        //Deserialize ItemSwitcher ActiveItem
        int switchID = (int)JsonManager.Json()["itemSwitcherData"]["switcherActiveItem"];

        if (switchID != -1)
        {
            switcher.ActivateItem(switchID);
        }

        //Deserialize Inventory Data
        StartCoroutine(DeserializeInventory(JsonManager.Json()["inventoryData"]));

        //Deserialize Objectives
        if (JsonManager.HasKey("objectivesData"))
        {
            Dictionary <int, Dictionary <string, string> > objectivesData = JsonManager.Json <Dictionary <int, Dictionary <string, string> > >(JsonManager.Json()["objectivesData"].ToString());

            foreach (var obj in objectivesData)
            {
                objectives.AddObjectiveModel(new ObjectiveModel(obj.Key, int.Parse(obj.Value["toComplete"]), bool.Parse(obj.Value["isCompleted"])));
            }
        }

        //Deserialize saveables
        if (allData)
        {
            foreach (var Pair in saveableDataPairs)
            {
                JToken token = JsonManager.Json()[Pair.BlockKey];

                if (token == null)
                {
                    continue;
                }

                if (Pair.BlockType == SaveableDataPair.DataBlockType.ISaveable)
                {
                    if (Pair.Instance.GetType() == typeof(SaveObject) && (Pair.Instance as SaveObject).saveType == SaveObject.SaveType.ObjectActive)
                    {
                        bool enabled = token["obj_enabled"].ToObject <bool>();
                        Pair.Instance.gameObject.SetActive(enabled);
                    }
                    else
                    {
                        (Pair.Instance as ISaveable).OnLoad(token);
                    }
                }
                else if (Pair.BlockType == SaveableDataPair.DataBlockType.Attribute)
                {
                    foreach (var Field in Pair.FieldData)
                    {
                        SetValue(Pair.Instance, Pair.Instance.GetType().GetField(Field), JsonManager.Json()[Pair.BlockKey][GetAttributeKey(Pair.Instance.GetType().GetField(Field))]);
                    }
                }
            }
        }
    }