public override void Restore(RestorableData rdata) { DynamicRestorableData data = rdata as DynamicRestorableData; if (data == null) { Debug.LogError("Object " + name + " has invalid data!"); return; } BaseController controller = GetComponent <BaseController>(); if (!controller) { Debug.LogWarning("Object " + name + " has no controller!"); } //restore object properties gameObject.SetActive(data.Active); if (controller) { controller.SetVisibility(data.Visible); //controller.Tags = new HashSet<string>(data.Tags); controller.Tags.Clear(); controller.Tags.UnionWith(data.Tags); if (controller.FormID != data.FormID) { Debug.LogWarning(string.Format("Saved form ID does not match (saved:{0} , object: {1})", data.FormID, controller.FormID)); } } //restore transform transform.position = data.Position; transform.rotation = data.Rotation; transform.localScale = data.Scale; //restore rigidbody Rigidbody rigidbody = GetComponent <Rigidbody>(); if (rigidbody) { rigidbody.velocity = data.Velocity; rigidbody.angularVelocity = data.AngularVelocity; rigidbody.mass = data.Mass; rigidbody.isKinematic = data.IsKinematic; } else { //eh, do nothing } //restore extradata if (controller) { controller.RestoreEntityData(data.ExtraData); } }
protected void RestorePlayer(GameState gs) { MetaState mgs = MetaState.Instance; GameObject player = WorldUtils.GetPlayerObject(); RestorableData prd = gs.PlayerWorldState; if (prd != null) { if (player == null) { //spawn the player object in player = Instantiate(CoreUtils.LoadResource <GameObject>("Entities/" + "spec_player"), transform) as GameObject; player.name = "Player"; if (mgs.TransitionType == SceneTransitionType.LoadGame) { player.GetComponent <PlayerRestorableComponent>().Restore(prd); } else { // get intent and move RestorePlayerToIntent(mgs, player); } } else { //restore player if relevant, warn either way if (mgs.TransitionType == SceneTransitionType.LoadGame) { player.GetComponent <PlayerRestorableComponent>().Restore(prd); Debug.LogWarning("Player already exists, restoring anyway"); } else { //if an intent exists, move RestorePlayerToIntent(mgs, player); Debug.LogWarning("Player already exists"); } } } else { if (player == null) { player = Instantiate(CoreUtils.LoadResource <GameObject>("Entities/" + "spec_player"), transform) as GameObject; player.name = "Player"; } RestorePlayerToIntent(mgs, player); //warn that no player data exists Debug.LogWarning("No player world data exists!"); } ScriptingModule.CallHooked(ScriptHook.OnPlayerSpawn, this, player); }
public override void Restore(RestorableData data) { base.Restore(data); }
public override RestorableData Save() { RestorableData data = base.Save(); return(data); }
public override void Commit() { GameState gs = GameState.Instance; Scene scene = SceneManager.GetActiveScene(); string name = scene.name; gs.CurrentScene = name; Debug.Log("Saving scene: " + name); //get restorable components List <RestorableComponent> rcs = new List <RestorableComponent>(); rcs.AddRange(transform.gameObject.GetComponentsInChildren <RestorableComponent>()); //WorldUtils.GetComponentsInDescendants(transform, rcs); //purge local object state Dictionary <string, RestorableData> localState; if (gs.LocalObjectState.ContainsKey(name)) { localState = gs.LocalObjectState[name]; localState.Clear(); } else { localState = new Dictionary <string, RestorableData>(); gs.LocalObjectState[name] = localState; } foreach (RestorableComponent rc in rcs) { try { if (localState.ContainsKey(rc.gameObject.name)) { Debug.LogWarning($"Committed an object with duplicate TID ({rc.gameObject.name})"); } RestorableData rd = rc.Save(); if (rc is LocalRestorableComponent || rc is BlankRestorableComponent) { localState[rc.gameObject.name] = rd; } else if (rc is MotileRestorableComponent) { gs.MotileObjectState[rc.gameObject.name] = rd; } else if (rc is PlayerRestorableComponent) { gs.PlayerWorldState = rd; } else { Debug.LogWarning("Unknown restorable type in " + rc.gameObject.name); } } catch (Exception e) { Debug.LogError("Failed to save an object!"); Debug.LogException(e); } } //purge and copy local data store if (gs.LocalDataState.ContainsKey(name)) { gs.LocalDataState.Remove(name); } gs.LocalDataState.Add(name, LocalStore); }
public abstract void Restore(RestorableData data);