save position and cam rotation
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
public float camTargetRotation { get; private set; } = 0;
|
||||
private bool isCamRotating = false;
|
||||
|
||||
public Transform playerCamHome;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleCamRotation();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trigger cam rotation
|
||||
/// </summary>
|
||||
public void RotateCam()
|
||||
{
|
||||
if (!isCamRotating)
|
||||
{
|
||||
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
|
||||
camTargetRotation = currentY + 90f;
|
||||
isCamRotating = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set cam rotation immediately
|
||||
/// </summary>
|
||||
public void SnapToRotation(float rotation)
|
||||
{
|
||||
playerCamHome.RotateAround(
|
||||
transform.position,
|
||||
Vector3.up,
|
||||
rotation
|
||||
);
|
||||
|
||||
camTargetRotation = rotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move cam to desired rotation (every frame)
|
||||
/// </summary>
|
||||
void HandleCamRotation()
|
||||
{
|
||||
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
|
||||
|
||||
float step = 200 * Time.deltaTime;
|
||||
|
||||
float newY = Mathf.MoveTowardsAngle(currentY, camTargetRotation, step);
|
||||
|
||||
playerCamHome.RotateAround(
|
||||
transform.position,
|
||||
Vector3.up,
|
||||
newY - currentY
|
||||
);
|
||||
|
||||
if (Mathf.Abs(Mathf.DeltaAngle(newY, camTargetRotation)) < 0.01f)
|
||||
{
|
||||
isCamRotating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34a2af7866b6ecc4ab5f2b6222f7dbe2
|
||||
@@ -15,11 +15,12 @@ public class GameManager : MonoBehaviour
|
||||
public Inventory Inventory { get; private set; }
|
||||
public Storybools Storybools { get; private set; }
|
||||
public PauseMenu PauseMenu { get; private set; }
|
||||
public PlayerController PlayerController { get; private set; }
|
||||
public Image BlackScreen { get; private set; }
|
||||
|
||||
// Are we currently in a scene transition?
|
||||
private bool isTransitioningScenes = false;
|
||||
private Image blackScreen;
|
||||
private PlayerController playerController;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -38,7 +39,10 @@ private void Awake()
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
ReloadReferences();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
SaveSystem.Load();
|
||||
SaveSystem.Save(); // save off any corruption fixes
|
||||
}
|
||||
@@ -48,8 +52,8 @@ private void Awake()
|
||||
/// </summary>
|
||||
private void ReloadReferences()
|
||||
{
|
||||
Instance.blackScreen = GameObject.FindWithTag("BlackScreen").GetComponent<Image>();
|
||||
Instance.playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
|
||||
Instance.BlackScreen = GameObject.FindWithTag("BlackScreen").GetComponent<Image>();
|
||||
Instance.PlayerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
|
||||
Instance.DialogueManager = GetComponent<DialogueManager>();
|
||||
Instance.DialogueManager.ReloadReferences();
|
||||
|
||||
@@ -77,7 +81,7 @@ public void GoToMapPoint(MapPoint mapPoint)
|
||||
private IEnumerator GoToMapPointCoroutine(MapPoint mapPoint)
|
||||
{
|
||||
Instance.isTransitioningScenes = true;
|
||||
Instance.playerController.SetCharacterControl(false);
|
||||
Instance.PlayerController.SetCharacterControl(false);
|
||||
|
||||
// Fade to black
|
||||
float fadeDuration = 0.2f; // how long to fade to/from black
|
||||
@@ -88,7 +92,7 @@ private IEnumerator GoToMapPointCoroutine(MapPoint mapPoint)
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
|
||||
blackScreen.color = blackScreenColor;
|
||||
Instance.BlackScreen.color = blackScreenColor;
|
||||
fadeTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
@@ -105,18 +109,18 @@ private IEnumerator GoToMapPointCoroutine(MapPoint mapPoint)
|
||||
ReloadReferences();
|
||||
|
||||
// Make sure screen is black in new scene
|
||||
blackScreen.color = blackScreenColor;
|
||||
Instance.BlackScreen.color = blackScreenColor;
|
||||
|
||||
if (Instance.playerController != null && GameSceneManager.Instance != null)
|
||||
if (Instance.PlayerController != null && GameSceneManager.Instance != null)
|
||||
{
|
||||
Instance.playerController.CharacterControllerMove(mapPoint.SpawnPosition - playerController.transform.position);
|
||||
Instance.PlayerController.CharacterControllerMove(mapPoint.SpawnPosition - PlayerController.transform.position);
|
||||
}
|
||||
|
||||
// Fade back in
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
|
||||
Instance.blackScreen.color = blackScreenColor;
|
||||
Instance.BlackScreen.color = blackScreenColor;
|
||||
fadeTime += Time.deltaTime;
|
||||
|
||||
yield return null;
|
||||
@@ -132,7 +136,7 @@ private IEnumerator GoToMapPointCoroutine(MapPoint mapPoint)
|
||||
fadeTime = 0;
|
||||
|
||||
Instance.isTransitioningScenes = false;
|
||||
Instance.playerController.SetCharacterControl(true);
|
||||
Instance.PlayerController.SetCharacterControl(true);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -151,7 +155,7 @@ public void EnterSceneDoor(string scene, int door)
|
||||
private IEnumerator EnterSceneDoorCoroutine(string scene, int doorId)
|
||||
{
|
||||
Instance.isTransitioningScenes = true;
|
||||
Instance.playerController.SetCharacterControl(false);
|
||||
Instance.PlayerController.SetCharacterControl(false);
|
||||
|
||||
// Fade to black
|
||||
float fadeDuration = 0.2f; // how long to fade to/from black
|
||||
@@ -162,7 +166,7 @@ private IEnumerator EnterSceneDoorCoroutine(string scene, int doorId)
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
|
||||
blackScreen.color = blackScreenColor;
|
||||
Instance.BlackScreen.color = blackScreenColor;
|
||||
fadeTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
@@ -179,16 +183,16 @@ private IEnumerator EnterSceneDoorCoroutine(string scene, int doorId)
|
||||
ReloadReferences();
|
||||
|
||||
// Make sure screen is black in new scene
|
||||
blackScreen.color = blackScreenColor;
|
||||
Instance.BlackScreen.color = blackScreenColor;
|
||||
|
||||
SceneDoor door = GameSceneManager.Instance.GetDoorWithId(doorId);
|
||||
|
||||
if (Instance.playerController != null && GameSceneManager.Instance != null)
|
||||
if (Instance.PlayerController != null && GameSceneManager.Instance != null)
|
||||
{
|
||||
if (door != null)
|
||||
{
|
||||
Instance.playerController.CharacterControllerMove(door.gameObject.transform.position - playerController.transform.position);
|
||||
Instance.playerController.DougBody.transform.rotation = door.gameObject.transform.rotation;
|
||||
Instance.PlayerController.CharacterControllerMove(door.gameObject.transform.position - PlayerController.transform.position);
|
||||
Instance.PlayerController.DougBody.transform.rotation = door.gameObject.transform.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,17 +200,17 @@ private IEnumerator EnterSceneDoorCoroutine(string scene, int doorId)
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
|
||||
Instance.blackScreen.color = blackScreenColor;
|
||||
Instance.BlackScreen.color = blackScreenColor;
|
||||
fadeTime += Time.deltaTime;
|
||||
|
||||
Instance.playerController.WalkInDirection((door.WalkDirection.position - playerController.transform.position).normalized);
|
||||
Instance.PlayerController.WalkInDirection((door.WalkDirection.position - PlayerController.transform.position).normalized);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// move character a little more
|
||||
while (fadeTime < moveDuration)
|
||||
{
|
||||
Instance.playerController.WalkInDirection((door.WalkDirection.position - playerController.transform.position).normalized);
|
||||
Instance.PlayerController.WalkInDirection((door.WalkDirection.position - PlayerController.transform.position).normalized);
|
||||
fadeTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
@@ -214,7 +218,7 @@ private IEnumerator EnterSceneDoorCoroutine(string scene, int doorId)
|
||||
fadeTime = 0;
|
||||
|
||||
Instance.isTransitioningScenes = false;
|
||||
Instance.playerController.SetCharacterControl(true);
|
||||
Instance.PlayerController.SetCharacterControl(true);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -226,22 +230,6 @@ public bool InSceneTransition()
|
||||
return Instance.isTransitioningScenes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the blackscreen object reference
|
||||
/// </summary>
|
||||
public Image GetBlackScreen()
|
||||
{
|
||||
return Instance.blackScreen;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the playerContoller object reference
|
||||
/// </summary>
|
||||
public PlayerController GetPlayerController()
|
||||
{
|
||||
return Instance.playerController;
|
||||
}
|
||||
|
||||
#region Storybool Save/Load
|
||||
public void SaveStoryBools(ref StoryboolSaveData data)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: -1
|
||||
executionOrder: -500
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
|
||||
@@ -15,8 +15,8 @@ public class PlayerController : MonoBehaviour
|
||||
public float gravity = 10;
|
||||
|
||||
Interactable nearestInteractable;
|
||||
public CameraController cameraController;
|
||||
private CharacterController characterController;
|
||||
private CameraController cameraController;
|
||||
private Vector3 moveDir;
|
||||
private bool hasControl = true; // set this to false if we want to stop reading player inputs
|
||||
private bool canUseTools = true; // set this to false if we want to stop reading player tool use
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
// Handles scene-persistent player data
|
||||
public class PlayerManager : MonoBehaviour
|
||||
@@ -9,11 +10,21 @@ public class PlayerManager : MonoBehaviour
|
||||
public void SavePlayerData(ref PlayerSaveData data)
|
||||
{
|
||||
data.activeToolIndex = GameManager.Instance.PlayerManager.CurrentToolIndex;
|
||||
data.camRotation = GameManager.Instance.PlayerController.cameraController.camTargetRotation;
|
||||
data.dougPosition = GameManager.Instance.PlayerController.transform.position;
|
||||
data.currentScene = SceneManager.GetActiveScene().name;
|
||||
}
|
||||
|
||||
public void LoadPlayerData(PlayerSaveData data)
|
||||
{
|
||||
/*if (data.currentScene != null && data.currentScene != SceneManager.GetActiveScene().name)
|
||||
{
|
||||
SceneManager.LoadScene(data.currentScene);
|
||||
}*/
|
||||
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = data.activeToolIndex;
|
||||
GameManager.Instance.PlayerController.cameraController.SnapToRotation(data.camRotation);
|
||||
GameManager.Instance.PlayerController.transform.position = data.dougPosition;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -22,4 +33,7 @@ public void LoadPlayerData(PlayerSaveData data)
|
||||
public struct PlayerSaveData
|
||||
{
|
||||
public int activeToolIndex;
|
||||
public float camRotation;
|
||||
public Vector3 dougPosition;
|
||||
public string currentScene;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ IEnumerator EnterRoomCoroutine(RoomDoor door)
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
|
||||
GameManager.Instance.GetBlackScreen().color = blackScreenColor;
|
||||
GameManager.Instance.BlackScreen.color = blackScreenColor;
|
||||
fadeTime += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
@@ -53,7 +53,7 @@ IEnumerator EnterRoomCoroutine(RoomDoor door)
|
||||
while (fadeTime < fadeDuration)
|
||||
{
|
||||
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
|
||||
GameManager.Instance.GetBlackScreen().color = blackScreenColor;
|
||||
GameManager.Instance.BlackScreen.color = blackScreenColor;
|
||||
playerController.WalkInDirection((door.linkedDoor.WalkDirection.position - playerController.transform.position).normalized);
|
||||
|
||||
fadeTime += Time.deltaTime;
|
||||
|
||||
Reference in New Issue
Block a user