Files
GoblinRaid/Assets/Scripts/Control/PlayerController.cs

85 lines
2.3 KiB
C#
Raw Permalink Normal View History

2025-10-27 17:05:16 +00:00
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using RPG.Movement;
using RPG.Combat;
2025-10-30 16:50:35 +00:00
using RPG.Core;
2025-10-27 17:05:16 +00:00
using System;
namespace RPG.Control
{
public class PlayerController : MonoBehaviour
{
2025-10-30 16:50:35 +00:00
Health health;
2025-10-27 17:05:16 +00:00
private PlayerControls playerControls;
private InputAction moveAction;
private InputAction interactAction;
private Camera mainCamera;
private void Awake()
{
playerControls = new PlayerControls();
moveAction = playerControls.Player.Move;
interactAction = playerControls.Player.Action;
mainCamera = Camera.main;
}
2025-10-30 16:50:35 +00:00
void Start()
{
health = GetComponent<Health>();
}
2025-10-27 17:05:16 +00:00
private void OnEnable()
{
moveAction.Enable();
interactAction.Enable();
}
private void OnDisable()
{
moveAction.Disable();
interactAction.Disable();
}
private void Update()
{
2025-10-30 16:50:35 +00:00
if (health.IsDead()) return;
2025-10-27 17:05:16 +00:00
if (InteractWithCombat()) return;
2025-10-31 20:33:41 +00:00
if (InteractWithMovement()) return;
2025-10-27 17:05:16 +00:00
}
private bool InteractWithCombat()
{
RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
foreach (RaycastHit hit in hits)
{
CombatTarget target = hit.transform.GetComponent<CombatTarget>();
2025-10-31 20:33:41 +00:00
if (target == null) continue;
2025-10-28 17:26:07 +00:00
if (!GetComponent<Fighter>().CanAttack(target.gameObject))
continue;
2025-10-31 20:33:41 +00:00
if (interactAction.IsPressed())
2025-10-27 17:05:16 +00:00
{
//MoveToCursor();
2025-10-28 17:26:07 +00:00
GetComponent<Fighter>().Attack(target.gameObject);
2025-10-27 17:05:16 +00:00
}
return true;
}
return false;
}
private bool InteractWithMovement()
{
RaycastHit hit;
bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
if (hasHit)
{
if (moveAction.IsPressed())
2025-10-27 17:26:17 +00:00
GetComponent<Mover>().StartMoveAction(hit.point);
2025-10-27 17:05:16 +00:00
return true;
}
return false;
}
private Ray GetMouseRay()
{
return mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
}
}
}