add unlock system for tools

This commit is contained in:
2026-06-29 22:15:59 -05:00
parent 9dfcee6159
commit fc0bcf0201
12 changed files with 513 additions and 65 deletions
+90 -18
View File
@@ -13,6 +13,7 @@ public class PlayerController : MonoBehaviour
public float walkSpeed;
public float sprintMultiplier = 1.5f;
public float gravity = 10;
public float rotationSpeed = 15;
Interactable nearestInteractable;
public CameraController cameraController;
@@ -23,14 +24,9 @@ public class PlayerController : MonoBehaviour
private bool isSprinting = false;
private bool paused = false;
// Mathematical plane used to catch the raycast from camera to get direction for
// looking at the mouse
private Plane groundPlane;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
groundPlane = new Plane(Vector3.up, -DougBody.transform.position.y);
characterController = GetComponent<CharacterController>();
cameraController = GetComponent<CameraController>();
SwitchTools(GameManager.Instance.PlayerManager.CurrentToolIndex);
@@ -40,7 +36,6 @@ void Start()
void Update()
{
moveDir = Vector3.zero;
groundPlane.distance = -DougBody.transform.position.y;
// TODO: Move input detection somewhere else
if (Input.GetKeyDown(KeyCode.Escape))
@@ -223,14 +218,67 @@ void ToolUseDetector()
/// </summary>
void RotatePlayerTowardMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
/*Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane plane = new Plane(cameraController.MainCamera.transform.forward,DougBody.transform.position);
if (groundPlane.Raycast(ray, out var distance))
if (plane.Raycast(ray, out var distance))
{
Vector3 hitPoint = ray.GetPoint(distance);
Vector3 direction = hitPoint - DougBody.transform.position;
direction.y = 0f;
DougBody.transform.LookAt(new Vector3(hitPoint.x, DougBody.transform.position.y, hitPoint.z));
///DougBody.transform.LookAt(new Vector3(hitPoint.x, DougBody.transform.position.y, hitPoint.z));
if (direction.sqrMagnitude > 0.001f)
{
Quaternion targetRotation =
Quaternion.LookRotation(direction);
DougBody.transform.rotation = Quaternion.Slerp(
DougBody.transform.rotation,
targetRotation,
rotationSpeed * Time.deltaTime
);
}
}*/
Camera cam = cameraController.MainCamera;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Vector3 targetPoint;
// 1. Try real world hit first (most stable)
if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
{
targetPoint = hit.point;
}
else
{
// 2. Fallback to plane at player height
Plane plane = new Plane(Vector3.up, DougBody.transform.position);
if (!plane.Raycast(ray, out float enter))
return;
targetPoint = ray.GetPoint(enter);
}
// 3. Flatten direction so we only rotate on Y axis
Vector3 direction = targetPoint - DougBody.transform.position;
direction.y = 0f;
if (direction.sqrMagnitude < 0.001f)
return;
// 4. Convert to rotation
Quaternion targetRotation = Quaternion.LookRotation(direction.normalized);
// 5. Smooth rotation
DougBody.transform.rotation = Quaternion.Slerp(
DougBody.transform.rotation,
targetRotation,
rotationSpeed * Time.deltaTime
);
}
/// <summary>
@@ -305,8 +353,12 @@ public Vector3 GetVelocity()
/// Switches the tools in hand by taking an index
///
/// <param name="toolIndex">Index of the tool to switch to</param>
/// <param name="defaultToUp">
/// If true, try the next tool if the tool at toolIndex is not unlocked yet. If false,
/// try the previous tool if the tool at toolIndex is not unlocked.
/// </param>
/// </summary>
private void SwitchTools(int toolIndex)
private void SwitchTools(int toolIndex, bool defaultToUp=true)
{
if (toolIndex < 0 || toolIndex >= tools.Length)
{
@@ -319,9 +371,20 @@ private void SwitchTools(int toolIndex)
tool.gameObject.SetActive(false);
}
// load new tool
GameManager.Instance.PlayerManager.CurrentToolIndex = toolIndex;
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
if (GameManager.Instance.PlayerManager.IsToolUnlocked(tools[toolIndex].ToolType))
{
// load new tool
GameManager.Instance.PlayerManager.CurrentToolIndex = toolIndex;
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
}
else
{
// try next index
if (defaultToUp)
SwitchTools(toolIndex + 1, defaultToUp);
else
SwitchTools(toolIndex - 1, defaultToUp);
}
}
/// <summary>
@@ -332,11 +395,11 @@ private void SwitchTools(int toolIndex)
private void SwitchTools(bool up)
{
// calculate index of next tool
int newIndex = up ? GameManager.Instance.PlayerManager.CurrentToolIndex - 1 : GameManager.Instance.PlayerManager.CurrentToolIndex + 1;
int newIndex = up ? GameManager.Instance.PlayerManager.CurrentToolIndex + 1 : GameManager.Instance.PlayerManager.CurrentToolIndex - 1;
if (newIndex < 0)
{
newIndex = tools.Length + newIndex;
newIndex = tools.Length - 1;
}
else if (newIndex >= tools.Length)
{
@@ -346,9 +409,18 @@ private void SwitchTools(bool up)
// disable current tool
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(false);
// load new tool
GameManager.Instance.PlayerManager.CurrentToolIndex = newIndex;
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
if (GameManager.Instance.PlayerManager.IsToolUnlocked(tools[newIndex].ToolType))
{
// load new tool
GameManager.Instance.PlayerManager.CurrentToolIndex = newIndex;
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
}
else
{
// try next tool
if (up) SwitchTools(newIndex + 1);
else SwitchTools(newIndex - 1, false);
}
}
}