save tool between scenes and in save file
This commit is contained in:
@@ -11,6 +11,7 @@ public class GameManager : MonoBehaviour
|
||||
public static GameManager Instance { get; private set; }
|
||||
public DialogueManager DialogueManager { get; private set; }
|
||||
public TimeManager TimeManager { get; private set; }
|
||||
public PlayerManager PlayerManager { get; private set; }
|
||||
public Inventory Inventory { get; private set; }
|
||||
public Storybools Storybools { get; private set; }
|
||||
|
||||
@@ -26,6 +27,7 @@ private void Awake()
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -52,6 +54,7 @@ private void ReloadReferences()
|
||||
|
||||
Instance.Inventory = GetComponent<Inventory>();
|
||||
Instance.TimeManager = GetComponent<TimeManager>();
|
||||
Instance.PlayerManager = GetComponent<PlayerManager>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
// Handles scene-persistent player data
|
||||
public class PlayerManager : MonoBehaviour
|
||||
{
|
||||
public int CurrentToolIndex;
|
||||
|
||||
#region Player Data Save/Load
|
||||
public void SavePlayerData(ref PlayerSaveData data)
|
||||
{
|
||||
data.activeToolIndex = GameManager.Instance.PlayerManager.CurrentToolIndex;
|
||||
}
|
||||
|
||||
public void LoadPlayerData(PlayerSaveData data)
|
||||
{
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = data.activeToolIndex;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct PlayerSaveData
|
||||
{
|
||||
public int activeToolIndex;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9c9dde58f68b5e4d9fc0036b990c481
|
||||
@@ -13,6 +13,7 @@ public struct SaveData
|
||||
public StoryboolSaveData StoryboolData;
|
||||
public InventorySaveData InventorySaveData;
|
||||
public TimeSaveData TimeSaveData;
|
||||
public PlayerSaveData PlayerSaveData;
|
||||
}
|
||||
|
||||
public static string SaveFileName()
|
||||
@@ -45,6 +46,7 @@ private static void HandleSaveData()
|
||||
GameManager.Instance.SaveStoryBools(ref _saveData.StoryboolData);
|
||||
GameManager.Instance.Inventory.SaveInventory(ref _saveData.InventorySaveData);
|
||||
GameManager.Instance.TimeManager.SaveTime(ref _saveData.TimeSaveData);
|
||||
GameManager.Instance.PlayerManager.SavePlayerData(ref _saveData.PlayerSaveData);
|
||||
QuestRegistry.Instance.SaveQuestData(ref _saveData.QuestData);
|
||||
}
|
||||
|
||||
@@ -84,6 +86,7 @@ public static void HandleLoadData()
|
||||
GameManager.Instance.LoadStoryBools(_saveData.StoryboolData);
|
||||
GameManager.Instance.Inventory.LoadInventory(_saveData.InventorySaveData);
|
||||
GameManager.Instance.TimeManager.LoadTime(_saveData.TimeSaveData);
|
||||
GameManager.Instance.PlayerManager.LoadPlayerData(_saveData.PlayerSaveData);
|
||||
QuestRegistry.Instance.LoadQuestData(_saveData.QuestData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ public class PlayerController : MonoBehaviour
|
||||
public GameObject DougBody;
|
||||
|
||||
public Tool[] tools;
|
||||
|
||||
int activeToolIndex;
|
||||
float toolUseTimestamp = 0;
|
||||
|
||||
public float walkSpeed;
|
||||
@@ -25,9 +23,9 @@ public class PlayerController : MonoBehaviour
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
activeToolIndex = 0;
|
||||
characterController = GetComponent<CharacterController>();
|
||||
cameraController = GetComponent<CameraController>();
|
||||
SwitchTools(GameManager.Instance.PlayerManager.CurrentToolIndex);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@@ -37,7 +35,7 @@ void Update()
|
||||
|
||||
if (hasControl)
|
||||
{
|
||||
if (!tools[activeToolIndex].inUse)
|
||||
if (!tools[GameManager.Instance.PlayerManager.CurrentToolIndex].inUse)
|
||||
{
|
||||
RotatePlayerTowardMouse();
|
||||
TryInteract();
|
||||
@@ -103,7 +101,7 @@ void ApplyWalk()
|
||||
float verticalMovement = 0;
|
||||
float horizontalMovement = 0;
|
||||
|
||||
if (!tools[activeToolIndex].inUse)
|
||||
if (!tools[GameManager.Instance.PlayerManager.CurrentToolIndex].inUse)
|
||||
{
|
||||
verticalMovement = Input.GetAxisRaw("Vertical");
|
||||
horizontalMovement = Input.GetAxisRaw("Horizontal");
|
||||
@@ -173,26 +171,26 @@ public void WalkInDirection(Vector3 direction)
|
||||
/// </summary>
|
||||
void ToolUseDetector()
|
||||
{
|
||||
if (!tools[activeToolIndex].inUse)
|
||||
if (!tools[GameManager.Instance.PlayerManager.CurrentToolIndex].inUse)
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
tools[activeToolIndex].Use();
|
||||
tools[activeToolIndex].inUse = true;
|
||||
toolUseTimestamp = Time.time + tools[activeToolIndex].useTimeSec; // fuck coroutines fr fr
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].Use();
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].inUse = true;
|
||||
toolUseTimestamp = Time.time + tools[GameManager.Instance.PlayerManager.CurrentToolIndex].useTimeSec; // fuck coroutines fr fr
|
||||
}
|
||||
else if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
tools[activeToolIndex].UseAlt();
|
||||
tools[activeToolIndex].inUse = true;
|
||||
toolUseTimestamp = Time.time + tools[activeToolIndex].altUseTimeSec;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].UseAlt();
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].inUse = true;
|
||||
toolUseTimestamp = Time.time + tools[GameManager.Instance.PlayerManager.CurrentToolIndex].altUseTimeSec;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Time.time > toolUseTimestamp)
|
||||
{
|
||||
tools[activeToolIndex].inUse = false;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].inUse = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -270,14 +268,37 @@ public bool GetCharacterHasControl()
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switches the tools in hand.
|
||||
/// Switches the tools in hand by taking an index
|
||||
///
|
||||
/// <param name="toolIndex">Index of the tool to switch to</param>
|
||||
/// </summary>
|
||||
private void SwitchTools(int toolIndex)
|
||||
{
|
||||
if (toolIndex < 0 || toolIndex >= tools.Length)
|
||||
{
|
||||
toolIndex = 0;
|
||||
}
|
||||
|
||||
// disable all tools
|
||||
foreach (Tool tool in tools)
|
||||
{
|
||||
tool.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
// load new tool
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = toolIndex;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switches the tools in hand by moving up or down the list
|
||||
///
|
||||
/// <param name="up">Are we moving up (-1) the list?</param>
|
||||
/// </summary>
|
||||
private void SwitchTools(bool up)
|
||||
{
|
||||
// calculate index of next tool
|
||||
int newIndex = up ? activeToolIndex - 1 : activeToolIndex + 1;
|
||||
int newIndex = up ? GameManager.Instance.PlayerManager.CurrentToolIndex - 1 : GameManager.Instance.PlayerManager.CurrentToolIndex + 1;
|
||||
|
||||
if (newIndex < 0)
|
||||
{
|
||||
@@ -289,10 +310,11 @@ private void SwitchTools(bool up)
|
||||
}
|
||||
|
||||
// disable current tool
|
||||
tools[activeToolIndex].gameObject.SetActive(false);
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(false);
|
||||
|
||||
// load new tool
|
||||
activeToolIndex = newIndex;
|
||||
tools[activeToolIndex].gameObject.SetActive(true);
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = newIndex;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user