Files
3DBlobici-WorkingTitle/3D blobici/Assets/Scripts/Player/PlayerMovement.cs
2025-08-06 00:55:06 +02:00

124 lines
3.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float sprintSpeed = 10f;
public float gravity = -9.81f;
public float rotationSpeed = 10f;
private CharacterController controller;
private Vector3 velocity;
private PlayerSkillTree PlayerSkills;
private int health;
private int maxHealth;
[Header ("Health")]
[SerializeField] private TMP_Text healthBar;
private void Awake()
{
PlayerSkills = PlayerSkillTree.Instance;
maxHealth = PlayerSkills.MaxHealth;
health = PlayerSkills.GetHealth();
Debug.Assert(maxHealth > 0);
}
void Start()
{
controller = GetComponent<CharacterController>();
healthBar.text = GetHealthText();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
HandleDash();
}
if (Input.GetKeyDown(KeyCode.L))
{
if(PlayerSkills.TryUsePotion(PotionHandler.PotionType.HealthBig))
{
Debug.Log("Potion used!");
}
else
{
Debug.Log("Potion not available!");
}
}
// TEMPORARY!!! LATER USED FOR COLISIONS
if (Input.GetKeyDown(KeyCode.F))
{
PlayerSkills.SetHealth(-20);
health = PlayerSkills.GetHealth();
healthBar.text = GetHealthText();
}
}
void FixedUpdate()
{
HandleMovement();
ApplyGravity();
}
void HandleMovement()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical");
Vector3 move = new Vector3(moveX, 0, moveZ).normalized;
if (move.magnitude >= 0.1f)
{
float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : moveSpeed;
Quaternion targetRotation = Quaternion.LookRotation(move);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
controller.Move(move * currentSpeed * Time.fixedDeltaTime);
}
}
void HandleDash()
{
if (PlayerSkills.IsSkillUnlocked(PlayerSkillTree.Skills.Dash))
{
// Implement dash logic
Debug.Log("Dashing!");
}
else
{
Debug.Log("Dash skill is not unlocked.");
}
}
void ApplyGravity()
{
if (IsGrounded() && velocity.y < 0) velocity.y = -2f;
velocity.y += gravity * Time.fixedDeltaTime;
controller.Move(velocity * Time.fixedDeltaTime);
}
bool IsGrounded()
{
float groundDistance = 0.2f;
return Physics.Raycast(transform.position, Vector3.down, groundDistance);
}
public List<PlayerSkillTree.Skills> GetPlayerSkills()
{
return PlayerSkills.GetPlayerSkills();
}
private string GetHealthText()
{
return (health.ToString() + " / " + maxHealth.ToString());
}
}