tool system with switching

This commit is contained in:
2026-03-25 17:58:18 -05:00
parent 17ed6ba3cb
commit 40af32939e
8 changed files with 727 additions and 44 deletions
+67 -16
View File
@@ -5,15 +5,16 @@
public class PlayerController : MonoBehaviour
{
public GameObject DougBody;
public Shovel Shovel;
public Tool[] tools;
int activeToolIndex;
float toolUseTimestamp = 0;
public float walkSpeed;
public float sprintMultiplier = 1.5f;
public float gravity = 10;
float digTime = 0.9f;
float digTimestamp = 0;
bool isDigging = false;
Interactable nearestInteractable;
private CharacterController characterController;
private CameraController cameraController;
@@ -24,6 +25,7 @@ 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>();
}
@@ -35,13 +37,13 @@ void Update()
if (hasControl)
{
if (!isDigging)
if (!tools[activeToolIndex].inUse)
{
RotatePlayerTowardMouse();
TryInteract();
}
DigDetector();
ToolUseDetector();
ApplyWalk();
// TODO: This is just for testing, remove or clean up if used
@@ -57,6 +59,7 @@ void Update()
}
}
CheckWeaponChange();
CheckSprint();
ApplyGravity();
DoMovement();
@@ -77,6 +80,21 @@ private void CheckSprint()
}
}
/// <summary>
/// Checks if weapon is being switched by scroll wheel
/// </summary>
private void CheckWeaponChange()
{
if (Input.mouseScrollDelta.y > 0)
{
SwitchTools(true);
}
else if (Input.mouseScrollDelta.y < 0)
{
SwitchTools(false);
}
}
/// <summary>
/// Apply walking inputs to moveDir
/// </summary>
@@ -85,7 +103,7 @@ void ApplyWalk()
float verticalMovement = 0;
float horizontalMovement = 0;
if (!isDigging)
if (!tools[activeToolIndex].inUse)
{
verticalMovement = Input.GetAxisRaw("Vertical");
horizontalMovement = Input.GetAxisRaw("Horizontal");
@@ -151,24 +169,30 @@ public void WalkInDirection(Vector3 direction)
}
/// <summary>
/// Detect input for digging (will eventually be tool agnostic)
/// Detect input for tool use
/// </summary>
void DigDetector()
void ToolUseDetector()
{
if (!isDigging)
if (!tools[activeToolIndex].inUse)
{
if (Input.GetMouseButtonDown(0))
{
Shovel.Dig();
isDigging = true;
digTimestamp = Time.time + digTime; // fuck coroutines fr fr
tools[activeToolIndex].Use();
tools[activeToolIndex].inUse = true;
toolUseTimestamp = Time.time + tools[activeToolIndex].useTimeSec; // fuck coroutines fr fr
}
else if (Input.GetMouseButtonDown(1))
{
tools[activeToolIndex].UseAlt();
tools[activeToolIndex].inUse = true;
toolUseTimestamp = Time.time + tools[activeToolIndex].altUseTimeSec;
}
}
else
{
if (Time.time > digTimestamp)
if (Time.time > toolUseTimestamp)
{
isDigging = false;
tools[activeToolIndex].inUse = false;
}
}
}
@@ -244,4 +268,31 @@ public bool GetCharacterHasControl()
{
return hasControl;
}
/// <summary>
/// Switches the tools in hand.
///
/// <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;
if (newIndex < 0)
{
newIndex = tools.Length + newIndex;
}
else if (newIndex >= tools.Length)
{
newIndex = newIndex % tools.Length;
}
// disable current tool
tools[activeToolIndex].gameObject.SetActive(false);
// load new tool
activeToolIndex = newIndex;
tools[activeToolIndex].gameObject.SetActive(true);
}
}
+7 -2
View File
@@ -1,14 +1,19 @@
using UnityEngine;
public class Shovel : MonoBehaviour
public class Shovel : Tool
{
public Animator shovelAnimator;
public void Dig()
public override void Use()
{
shovelAnimator.SetTrigger("Dig");
}
public override void UseAlt()
{
// no tool
}
// this only works if this script is sitting on the object with the collider!!
private void OnTriggerEnter(Collider other)
{
+14
View File
@@ -0,0 +1,14 @@
using UnityEngine;
public abstract class Tool : MonoBehaviour
{
public float useTimeSec;
public float altUseTimeSec;
public bool inUse;
// primary (left click)
public abstract void Use();
// secondary (right click)
public abstract void UseAlt();
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 72d72769aaaeff0479ea6b811b94d1df