save tool between scenes and in save file
This commit is contained in:
@@ -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