public void OnLevelLoaded(SceneSerializationType type)
    {
//		Debug.Log("On level loaded delegate firing");
        loadingDialogue.SetActive(false);
//		WebGLComm.inst.Debug("UTY.levelloader.onlevel loaded (deleagte)");
        GameManager.inst.RestartLevelTimer();

        if (onLevelLoadedDelegate != null)
        {
//			WebGLComm.inst.Debug("UTY.levelloader delegate:"+onLevelLoadedDelegate.ToString());
            onLevelLoadedDelegate();
        }
        if (LevelBuilder.inst.levelBuilderIsShowing && LevelBuilder.inst.firstTimeOpened)
        {
            LevelBuilder.inst.ActionCenterOnPlayer();
        }

        GameManager.inst.NewLevelWasLoaded();


        if (LevelBuilder.inst.levelBuilderIsShowing)
        {
            GameManager.inst.SetGameState(GameState.Editing);
        }
        else
        {
            GameManager.inst.SetGameState(GameState.Playing);
        }

        state = LoadingState.Ready;

//		StartCoroutine(GameStartedAfterSeconds(.2f)); // can't do this same frame because old stuff will get touched and new instantiated stuff isnt there yet.
//		Player.inst.SetPlayerLocationForGameStarted();
    }
    public SimpleJSON.JSONClass SaveLevel(SceneSerializationType type)
    {
        if (type == SceneSerializationType.Class)
        {
            JsonLevelLoader.inst.state = LoadingState.SavingClass;
        }
        if (type == SceneSerializationType.Instance)
        {
            JsonLevelLoader.inst.state = LoadingState.SavingInstance;
        }
        SimpleJSON.JSONClass N = null;
        if (type == SceneSerializationType.Instance)
        {
            N = SaveLevelInstance();
        }
        else if (type == SceneSerializationType.Class)
        {
            N = SaveLevelClass();
        }

//		Debug.Log("json save:"+N.ToString());
//		Debug.Log("string2");
//		string js = N.ToString();
//		if (js.Length > 20) js = js.Substring(0,20);
//		Debug.Log("saving:"+type.ToString()+",json:"+js);
        return(N);
    }
    bool ObjectIsSerializeable(UserEditableObject ueo, SceneSerializationType serializationType)
    {
        switch (serializationType)
        {
        case SceneSerializationType.Class:
            if (ueo.isSerializeableForClass)
            {
                return(true);
            }
            break;

        case SceneSerializationType.Instance:
            if (ueo.IsSerializeableForSceneInstance())
            {
                return(true);
            }
            break;

        default:
            return(false);

            break;
        }
        return(false);
    }
    public void LoadLevel(string json, SceneSerializationType type = SceneSerializationType.Class, bool loadingFromPipe = false, bool centerOnPlayer = true, string previousLevelCode = "")
    {
//		Debug.Log("<color=#fff> BREAKPOINT </color>");
        Player.inst.SlowFadeInBlack();
        SceneManager.inst.ReloadSceneNow();
//		Application.LoadLevel(Application.loadedLevel);
        StartCoroutine(LoadLevelE(json, type, loadingFromPipe, centerOnPlayer, previousLevelCode));
    }
Beispiel #5
0
    // Freshly populate the placed objects
//	public List<LevelBuilderSelectableObject> GetPlacedObjects(SceneSerializationType type){
//		LevelBuilderSelectableObject
//		return ueos;
//	}

    public UserEditableObject PlaceObject(SimpleJSON.JSONClass N, SceneSerializationType type, int uuid = -1)
    {
//		Debug.Log("placing:"+N["name"]);
        GameObject objToPlace     = GetPrefabInstanceFromName(N["name"].Value);
        bool       objActiveState = true;

        if (N.GetKeys().Contains("active"))
        {
//			Debug.Log("obj active;"+N["active"].ToString());
            objActiveState = N["active"].AsBool;             // if the object has this information, it might have been inactive when it was serialized.
        }
        if (objToPlace == null)
        {
            Debug.Log("<color=red>obj null</color>:" + N["name"]);
            return(null);
        }
        objToPlace.transform.position = JsonUtil.GetRealPositionFromTruncatedPosition(N);
//		objToPlace.name += Random.Range(0,100000);
        objToPlace.transform.rotation = JsonUtil.GetRealRotationFromJsonRotation(N);


        SimpleJSON.JSONClass props = (SimpleJSON.JSONClass)N["properties"];
        UserEditableObject   ueo   = objToPlace.GetComponent <UserEditableObject>();

        if (uuid != -1)
        {
            props[UserEditableObject.uuidKey].AsInt = uuid;
        }
        else if (N.GetKeys().Contains(UserEditableObject.uuidKey))
        {
            props[UserEditableObject.uuidKey].AsInt = N[UserEditableObject.uuidKey].AsInt;
        }

        ueo.OnLevelBuilderObjectCreated();
        ueo.SetProperties(props);

        if (LevelBuilder.inst.levelBuilderIsShowing)
        {
            ueo.OnLevelBuilderObjectPlaced();
        }
        if (!objActiveState)
        {
            objToPlace.SetActive(objActiveState);
        }
//		Debug.Log("Built;"+ueo.name+"at :"+ueo.transform.position);
        return(ueo);
    }
 // GetExtraProperties, GetFullProeperties, GetDeepProperties
 public static SimpleJSON.JSONClass GetUeoBaseProps(SimpleJSON.JSONClass obj, UserEditableObject ueo, SceneSerializationType serializationType)
 {
     // oops -- we are setting "all" the properties including rotation and position. This SHOULD be warpped in USerEditableObject but we didn't do it that way
     // and a refactor would risk breaking existing levels,
     // so for now we leave ueo.setprops / ueo.getprops as a "higher level" properties, where as THIS BaseProps is the "full" properties of the object
     if (obj == null)
     {
         Debug.LogError("null obj");
         return(obj);
     }
     else if (ueo == null)
     {
         Debug.LogError("ueo null for " + obj["name"] + ", str;" + obj.ToString());
         return(obj);
     }
     obj["name"]          = ueo.GetName;
     obj["position"]      = JsonUtil.GetTruncatedPosition(ueo.transform);
     obj["rotation"]      = JsonUtil.GetRotation(ueo.transform);
     obj["active"].AsBool = ueo.gameObject.activeSelf;
     if (serializationType == SceneSerializationType.Instance)
     {
         obj[JsonUtil.scaleKey].AsInt = JsonUtil.GetScaleAsInt(ueo.transform);
     }
     obj["properties"] = ueo.GetProperties();
     return(obj);
 }
    IEnumerator LoadLevelE(string json, SceneSerializationType type = SceneSerializationType.Class, bool loadingFromPipe = false, bool centerOnPlayer = true, string previousLevelCode = "")
    {
        yield return(new WaitForEndOfFrame());        // wAit for scene to reload (needs 2 frames)

        yield return(new WaitForEndOfFrame());        // wAit for scene to reload

        if (type == SceneSerializationType.Class)
        {
            state = LoadingState.LoadingClass;
        }
        if (type == SceneSerializationType.Instance)
        {
            state = LoadingState.LoadingInstance;
        }
        GameManager.inst.EndGame("json level loader e");
        WebGLComm.inst.Debug("<color=#0f0>Loading</color> json of len:" + json.Length + ", sertype;" + type + ",str;");
        if (type == SceneSerializationType.Class)
        {
            GameManager.inst.SetGameState(GameState.LoadingClass);
        }
        else if (type == SceneSerializationType.Instance)
        {
            GameManager.inst.SetGameState(GameState.LoadingInstance);                                                       // we are duplicating LoadingState.LoadingInstance .. heh
        }
        SimpleJSON.JSONClass N = new SimpleJSON.JSONClass();


//		Debug.Log("LOAD:"+json);
//		WebGLComm.inst.Debug("UTY load level type:"+type);

//		Debug.Log("level being loaded");
        Player.inst.ClearPlayerStartPositions();
        if (LevelBuilder.inst.levelBuilderIsShowing)
        {
            loadingDialogue.SetActive(true);
        }

//		if (json.Length > 25) WebGLComm.inst.Debug("Load level:"+type.ToString()+",json:"+json.Substring(0,45)+" cp;"+centerOnPlayer+", loadfrompipe:"+loadingFromPipe+",oldcode:"+previousLevelCode);
//		else WebGLComm.inst.Debug("Load level but no json:"+type.ToString()+",loadfrompipe;"+loadingFromPipe);


        GameManager.inst.DestroyAllEphemeralObjects();         // inventory destroyed here.
        if (json == "")
        {
            // Assume a new
            MapManager.inst.SelectTerrainByName("Flatland");
            JsonLevelSaver.inst.ResetLevelNameAndDescription();
            WebGLComm.inst.Debug("json null, loading flatland");
            LevelBuilder.inst.ActionCenterOnPlayer();
//			LevelBuilder.inst.loadingLevelObjectScreen.SetActive(false);
        }
        else
        {
//			N =
            N = JsonUtil.GetJsonFromString(json);
//			yield break;

            SetJsonVersion(N);
//			Debug.Log("<color=#f0f>OBJS LD:"+N["Objects"].Count+"</color>");


            //		WebGLComm.inst.Debug("N tostring:"+N.ToString());
            if (type == SceneSerializationType.Instance)
            {
                centerOnPlayer = false;
                LoadLevelInstance(N, loadingFromPipe, previousLevelCode);
//				WebGLComm.inst.GetPlayerInventory();
            }
            else if (type == SceneSerializationType.Class)
            {
                LoadLevelClass(N, centerOnPlayer);
            }
        }
        centerOnPlayer     = !firstLoadCompleted;     // only center on player on the first open of levlebuilder.
        firstLoadCompleted = true;
        SetGlobalLevelProperties(N, centerOnPlayer);

        if (N.GetKeys().Contains("BackgroundAudio"))
        {
            string ba = N["BackgroundAudio"];
            BackgroundAudioManager.inst.SetGameBackgroundAudio(ba);
//			WebGLComm.inst.Debug("got background audio:"+ba+" for load lev:"+type);
            //				BackgroundAudioManager.inst.PlayEnvironmentAudio(N["BackgroundAudio"]);
        }
        else
        {
//			WebGLComm.inst.Debug("no key for bg audio on load lev "+type);
        }
        if (type == SceneSerializationType.Instance || !LevelBuilder.inst.levelBuilderIsShowing)
        {
            // If we loaded an instance, play audio immediately.
            BackgroundAudioManager.inst.PlayGameBackgroundAudio();
        }

        if (N.GetKeys().Contains(LevelBuilderGroupManager.groupManagerKey))
        {
            LevelBuilderGroupManager.inst.SetProperties(N[LevelBuilderGroupManager.groupManagerKey].AsArray);
        }



        OnLevelLoaded(type);
        if (type == SceneSerializationType.Class)
        {
            // awkward way to make sure the json we just loaded is now saved locally.
            JsonLevelLoader.inst.SetTempLevelJsonPlayerPrefs(json);
        }
    }
    public SimpleJSON.JSONArray SerializeUserEditableObjects(List <UserEditableObject> ueos, SceneSerializationType serializationType = SceneSerializationType.Class)
    {
        WebGLComm.inst.Debug("<color=#09f>Saver:</color>.SerializeUserEditableObjects()");
        SimpleJSON.JSONArray objs = new SimpleJSON.JSONArray();         // Will be a list of objects

        foreach (UserEditableObject ueo in ueos)
        {
            SimpleJSON.JSONClass obj = new SimpleJSON.JSONClass();             // Create a separate json sub-tree of user placed objects, which will be added to the main json after it's populated.
            obj = JsonUtil.GetUeoBaseProps(obj, ueo, serializationType);
            objs.Add(obj);
        }

        return(objs);
    }
    public SimpleJSON.JSONClass JsonSerializeSceneObjects(SimpleJSON.JSONClass N = null, SceneSerializationType serializationType = SceneSerializationType.Class)
    {
        WebGLComm.inst.Debug("<color=#09f>Saver:</color>serializing scene with type: " + serializationType);
        if (N == null)
        {
            N = new SimpleJSON.JSONClass();
        }
        // Learn more about nesting json objects here: http://answers.unity3d.com/questions/1083933/simplejson-and-c-how-to-write-json-from-multiple-o.html
        // You need to figure out how to go from sipmlejson node to string http://answers.unity3d.com/questions/753058/get-a-json-string-from-a-simplejson-node.html

        // Make an empty list to be populated with objects we will be serializing
        List <UserEditableObject> ueos = new List <UserEditableObject>();

//		Debug.Log("ct before;"+ueos.Count);
        if (serializationType == SceneSerializationType.Class)
        {
//			List<LevelBuilderSelectableObject> lbsos = new List<LevelBuilderSelectableObject>();
//			lbsos.AddRange(FindObjectsOfType<LevelBuilderSelectableObject>());
//			Debug.Log("bos:"+lbsos.Count);
            foreach (LevelBuilderSelectableObject lbso in FindObjectsOfType <LevelBuilderSelectableObject>())
            {
//				Debug.Log("add:"+lbso.name+", count:"+ueos.Count);

                ueos.Add(lbso.GetComponent <UserEditableObject>());
            }
        }
        else if (serializationType == SceneSerializationType.Instance)
        {
//			Debug.Log("serializing for INSTANCE");
//			Debug.Log("ct;"+ueos.Count);
            // Scene instance serialization looks at every possible object it can serialize
            foreach (UserEditableObject ueo in Utils.FindObjectsOfTypeInScene <UserEditableObject>())
            {
                if (ueo.IsSerializeableForSceneInstance())
                {
                    ueos.Add(ueo);
                }
            }
        }

//		Debug.Log("ct after;"+ueos.Count);

        N["Objects"] = SerializeUserEditableObjects(ueos);


        return(N);
    }