private void ReturnMainData() { Player player = GameObject.FindWithTag(Tags.player).GetComponent <Player>(); PlayerInput playerInput = GameObject.FindWithTag(Tags.gameEngine).GetComponent <PlayerInput>(); MainCamera mainCamera = GameObject.FindWithTag(Tags.mainCamera).GetComponent <MainCamera>(); RuntimeInventory runtimeInventory = this.GetComponent <RuntimeInventory>(); RuntimeVariables runtimeVariables = runtimeInventory.GetComponent <RuntimeVariables>(); SceneChanger sceneChanger = this.GetComponent <SceneChanger>(); if (player && playerInput && mainCamera && runtimeInventory && runtimeVariables) { sceneChanger.previousScene = saveData.mainData.previousScene; player.transform.position = new Vector3(saveData.mainData.playerLocX, saveData.mainData.playerLocY, saveData.mainData.playerLocZ); player.transform.eulerAngles = new Vector3(0f, saveData.mainData.playerRotY, 0f); player.SetLookDirection(Vector3.zero, true); // Active path player.Halt(); Paths savedPath = Serializer.returnComponent <Paths> (saveData.mainData.playerActivePath); if (savedPath) { player.SetLockedPath(savedPath); } // Active screen arrows playerInput.RemoveActiveArrows(); ArrowPrompt loadedArrows = Serializer.returnComponent <ArrowPrompt> (saveData.mainData.playerActiveArrows); if (loadedArrows) { loadedArrows.TurnOn(); } // Active conversation playerInput.activeConversation = Serializer.returnComponent <Conversation> (saveData.mainData.playerActiveConversation); playerInput.isUpLocked = saveData.mainData.playerUpLock; playerInput.isDownLocked = saveData.mainData.playerDownLock; playerInput.isLeftLocked = saveData.mainData.playerLeftlock; playerInput.isRightLocked = saveData.mainData.playerRightLock; playerInput.runLock = (PlayerMoveLock)saveData.mainData.playerRunLock; runtimeInventory.isLocked = saveData.mainData.playerInventoryLock; playerInput.timeScale = saveData.mainData.timeScale; mainCamera.SetGameCamera(Serializer.returnComponent <GameCamera> (saveData.mainData.gameCamera)); mainCamera.transform.position = new Vector3(saveData.mainData.mainCameraLocX, saveData.mainData.mainCameraLocY, saveData.mainData.mainCameraLocZ); mainCamera.transform.eulerAngles = new Vector3(saveData.mainData.mainCameraRotX, saveData.mainData.mainCameraRotY, saveData.mainData.mainCameraRotZ); if (mainCamera.attachedCamera) { mainCamera.attachedCamera.MoveCameraInstant(); } else { Debug.LogWarning("MainCamera has no attached GameCamera"); } // Inventory AssignInventory(runtimeInventory, saveData.mainData.inventoryData); if (saveData.mainData.selectedInventoryID > -1) { runtimeInventory.SelectItemByID(saveData.mainData.selectedInventoryID); } else { runtimeInventory.SetNull(); } // Variables AssignVariables(runtimeVariables, saveData.mainData.variablesData); // StateHandler StateHandler stateHandler = runtimeInventory.GetComponent <StateHandler>(); if (playerInput.activeConversation) { stateHandler.gameState = GameState.DialogOptions; } else { stateHandler.gameState = GameState.Normal; } // Fade in camera mainCamera.FadeIn(0.5f); playerInput.ResetClick(); } else { if (player == null) { Debug.LogWarning("Load failed - no Player found."); } if (playerInput == null) { Debug.LogWarning("Load failed - no PlayerInput found."); } if (mainCamera == null) { Debug.LogWarning("Load failed - no MainCamera found."); } if (runtimeInventory == null) { Debug.LogWarning("Load failed - no RuntimeInventory found."); } if (runtimeVariables == null) { Debug.LogWarning("Load failed - no RuntimeVariables found."); } if (sceneChanger == null) { Debug.LogWarning("Load failed - no SceneChanger found."); } } }
public void SaveGame(int slot) { #if !UNITY_WEBPLAYER if (!HasAutoSave() || slot == -1) { slot++; } levelStorage.StoreCurrentLevelData(); saveData = new SaveData(); Player player = GameObject.FindWithTag(Tags.player).GetComponent <Player>(); PlayerInput playerInput = GameObject.FindWithTag(Tags.gameEngine).GetComponent <PlayerInput>(); MainCamera mainCamera = GameObject.FindWithTag(Tags.mainCamera).GetComponent <MainCamera>(); RuntimeInventory runtimeInventory = this.GetComponent <RuntimeInventory>(); RuntimeVariables runtimeVariables = runtimeInventory.GetComponent <RuntimeVariables>(); SceneChanger sceneChanger = this.GetComponent <SceneChanger>(); if (player && playerInput && mainCamera && runtimeInventory && runtimeVariables && sceneChanger) { // Assign "Main Data" saveData.mainData.currentScene = Application.loadedLevel; saveData.mainData.previousScene = sceneChanger.previousScene; saveData.mainData.playerLocX = player.transform.position.x; saveData.mainData.playerLocY = player.transform.position.y; saveData.mainData.playerLocZ = player.transform.position.z; saveData.mainData.playerRotY = player.transform.eulerAngles.y; if (player.GetPath() && player.lockedPath && player.GetPath() != player.GetComponent <Paths>()) { saveData.mainData.playerActivePath = Serializer.GetConstantID(player.GetPath().gameObject); } if (playerInput.activeArrows) { saveData.mainData.playerActiveArrows = Serializer.GetConstantID(playerInput.activeArrows.gameObject); } if (playerInput.activeConversation) { saveData.mainData.playerActiveConversation = Serializer.GetConstantID(playerInput.activeConversation.gameObject); } saveData.mainData.playerUpLock = playerInput.isUpLocked; saveData.mainData.playerDownLock = playerInput.isDownLocked; saveData.mainData.playerLeftlock = playerInput.isLeftLocked; saveData.mainData.playerRightLock = playerInput.isRightLocked; saveData.mainData.playerRunLock = (int)playerInput.runLock; saveData.mainData.playerInventoryLock = runtimeInventory.isLocked; saveData.mainData.timeScale = playerInput.timeScale; if (mainCamera.attachedCamera) { saveData.mainData.gameCamera = Serializer.GetConstantID(mainCamera.attachedCamera.gameObject); } saveData.mainData.mainCameraLocX = mainCamera.transform.position.x; saveData.mainData.mainCameraLocY = mainCamera.transform.position.y; saveData.mainData.mainCameraLocZ = mainCamera.transform.position.z; saveData.mainData.mainCameraRotX = mainCamera.transform.eulerAngles.x; saveData.mainData.mainCameraRotY = mainCamera.transform.eulerAngles.y; saveData.mainData.mainCameraRotZ = mainCamera.transform.eulerAngles.z; saveData.mainData.inventoryData = CreateInventoryData(runtimeInventory); saveData.mainData.selectedInventoryID = runtimeInventory.selectedID; saveData.mainData.variablesData = CreateVariablesData(runtimeVariables); string mainData = Serializer.SerializeObjectBinary(saveData); string levelData = Serializer.SerializeObjectBinary(levelStorage.allLevelData); string allData = mainData + "||" + levelData; Serializer.CreateSaveFile(GetSaveFileName(slot), allData); } else { if (player == null) { Debug.LogWarning("Save failed - no Player found."); } if (playerInput == null) { Debug.LogWarning("Save failed - no PlayerInput found."); } if (mainCamera == null) { Debug.LogWarning("Save failed - no MainCamera found."); } if (runtimeInventory == null) { Debug.LogWarning("Save failed - no RuntimeInventory found."); } if (runtimeVariables == null) { Debug.LogWarning("Save failed - no RuntimeVariables found."); } if (sceneChanger == null) { Debug.LogWarning("Save failed - no SceneChanger found."); } } #endif }