added inventory and stamina system

This commit is contained in:
2026-02-17 15:38:46 +00:00
parent 8b975163a0
commit 27f2931ce1
43 changed files with 2980 additions and 250 deletions

View File

@@ -4,33 +4,31 @@ using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
[Header("Movement Settings")]
public float walkSpeed = 50f; // Boosted from 8f to overcome collision issues
public float runSpeed = 80f; // Boosted from 14f
public float walkSpeed = 50f;
public float runSpeed = 80f;
public float jumpHeight = 2.5f;
public float gravity = -20f;
public float gravity = -20f;
[Header("Mouse Look Settings")]
public float mouseSensitivity = 3f;
public float maxLookAngle = 90f;
public float maxLookAngle = 90f;
[Header("References")]
public Camera playerCamera;
// Private variables
private CharacterController controller;
private Vector3 velocity;
private bool isGrounded;
private float xRotation = 0f;
private Inventory inventory;
private Player player;
private Vector3 velocity;
private bool isGrounded;
private float xRotation = 0f;
void Start()
{
Debug.Log("Starting game");
// FORCE NORMAL TIME (in case something external changed it)
Time.timeScale = 1f;
controller = GetComponent<CharacterController>();
if (controller == null)
{
Debug.LogError("FirstPersonController: No CharacterController found!");
@@ -40,85 +38,58 @@ public class FirstPersonController : MonoBehaviour
if (playerCamera == null)
playerCamera = GetComponentInChildren<Camera>();
inventory = GetComponent<Inventory>();
player = GetComponent<Player>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Cursor.visible = false;
}
void Update()
{
if( controller == null )
return;
if (controller == null) return;
isGrounded = controller.isGrounded;
if (isGrounded && velocity.y < 0)
velocity.y = -2f;
// ─── Movement via direct KeyCode ───
float moveX = 0f;
float moveZ = 0f;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) moveZ += 1f;
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) moveZ -= 1f;
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) moveX -= 1f;
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) moveX += 1f;
// DEBUG: Log once when W is first pressed
if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log($"[FPC DEBUG] W pressed | controller.enabled={controller.enabled} | position={transform.position} | isGrounded={isGrounded}");
Debug.Log($"[FPC DEBUG] Time.timeScale={Time.timeScale} | Time.deltaTime={Time.deltaTime} | walkSpeed={walkSpeed}");
}
float moveX = 0f, moveZ = 0f;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) moveZ += 1f;
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) moveZ -= 1f;
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) moveX -= 1f;
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) moveX += 1f;
Vector3 move = transform.right * moveX + transform.forward * moveZ;
if (move.magnitude > 1f) move.Normalize();
if (move.magnitude > 1f)
move.Normalize();
bool wantSprint = Input.GetKey(KeyCode.LeftShift) && move.magnitude > 0f;
bool isSprinting = wantSprint && (player == null || player.CanSprint());
if (player != null) player.isSprinting = isSprinting;
float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
Vector3 posBefore = transform.position;
float currentSpeed = isSprinting ? runSpeed : walkSpeed;
controller.Move(move * currentSpeed * Time.deltaTime);
// DEBUG: Log if we tried to move but didn't
if (move.magnitude > 0f && Input.GetKeyDown(KeyCode.W))
{
Vector3 posAfter = transform.position;
//Debug.Log($"[FPC DEBUG] Move attempt: delta={move * currentSpeed * Time.deltaTime} | actualDelta={(posAfter - posBefore)} | controller.height={controller.height} | controller.radius={controller.radius}");
// Check what we're colliding with
Collider[] nearbyColliders = Physics.OverlapSphere(transform.position, controller.radius + 0.5f);
//Debug.Log($"[FPC DEBUG] Found {nearbyColliders.Length} colliders near player");
foreach (Collider col in nearbyColliders)
{
if (col != controller && !(col is CharacterController))
Debug.LogWarning($"[FPC DEBUG] Nearby collider: {col.gameObject.name} on layer {LayerMask.LayerToName(col.gameObject.layer)}");
}
}
// Jumping
if (Input.GetKey(KeyCode.Space) && isGrounded)
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
// Gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
controller.Move(new Vector3(0f, velocity.y, 0f) * Time.deltaTime);
// Mouse look
HandleMouseLook();
bool inventoryOpen = inventory != null && inventory.IsOpen;
if (!inventoryOpen)
HandleMouseLook();
// Escape to unlock cursor
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
Cursor.visible = true;
}
// Click to re-lock cursor
if (Input.GetMouseButtonDown(0) && Cursor.lockState == CursorLockMode.None)
if (Input.GetMouseButtonDown(0) && Cursor.lockState == CursorLockMode.None && !inventoryOpen)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Cursor.visible = false;
}
}
@@ -128,9 +99,8 @@ public class FirstPersonController : MonoBehaviour
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -maxLookAngle, maxLookAngle);
xRotation = Mathf.Clamp(xRotation, -maxLookAngle, maxLookAngle);
playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
}