Add project files.
This commit is contained in:
75
Assets/Scripts/Control/PlayerController.cs
Normal file
75
Assets/Scripts/Control/PlayerController.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using RPG.Movement;
|
||||
using RPG.Combat;
|
||||
using System;
|
||||
|
||||
namespace RPG.Control
|
||||
{
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
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;
|
||||
}
|
||||
private void OnEnable()
|
||||
{
|
||||
moveAction.Enable();
|
||||
interactAction.Enable();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
moveAction.Disable();
|
||||
interactAction.Disable();
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if (InteractWithCombat()) return;
|
||||
if (InteractWithMovement()) return;
|
||||
}
|
||||
|
||||
private bool InteractWithCombat()
|
||||
{
|
||||
RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
|
||||
foreach (RaycastHit hit in hits)
|
||||
{
|
||||
CombatTarget target = hit.transform.GetComponent<CombatTarget>();
|
||||
if (target == null) continue;
|
||||
if (interactAction.WasPressedThisFrame())
|
||||
{
|
||||
//MoveToCursor();
|
||||
GetComponent<Fighter>().Attack(target);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool InteractWithMovement()
|
||||
{
|
||||
RaycastHit hit;
|
||||
bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
|
||||
if (hasHit)
|
||||
{
|
||||
if (moveAction.IsPressed())
|
||||
GetComponent<Mover>().MoveTo(hit.point);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Ray GetMouseRay()
|
||||
{
|
||||
return mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user