Files
ClickRPG/Assets/Scripts/ClickController.cs

85 lines
2.8 KiB
C#
Raw Normal View History

2025-10-04 12:25:29 +01:00
using System.Collections;
using System.Collections.Generic;
2025-10-06 15:32:09 +01:00
using TMPro;
2025-10-04 12:25:29 +01:00
using UnityEngine;
using UnityEngine.InputSystem;
2025-10-06 15:32:09 +01:00
public interface IInteractable
{
string GetInteractPrompt();
void OnInteract();
}
2025-10-04 12:25:29 +01:00
public class ClickController : MonoBehaviour
{
[SerializeField] private LayerMask layerMask;
2025-10-06 15:32:09 +01:00
[SerializeField] private TextMeshProUGUI promptText;
2025-10-04 12:25:29 +01:00
void Update ()
{
2025-10-06 15:32:09 +01:00
Hover();
2025-10-04 12:25:29 +01:00
if (Mouse.current.rightButton.wasPressedThisFrame)
Click();
2025-10-06 15:32:09 +01:00
2025-10-04 12:25:29 +01:00
}
void Click ()
{
Vector2 mousePos = Mouse.current.position.ReadValue();
Ray ray = Camera.main.ScreenPointToRay(mousePos);
RaycastHit hit;
// Shoot a raycast from our mouse to what ever we are pointing at.
if (Physics.Raycast(ray, out hit, 1000, layerMask))
{
2025-10-06 15:32:09 +01:00
// This single block now handles NPCs, ItemObjects, and more!
if (hit.collider.TryGetComponent(out IInteractable interactable))
2025-10-04 12:25:29 +01:00
{
2025-10-06 15:32:09 +01:00
interactable.OnInteract();
2025-10-04 12:25:29 +01:00
}
2025-10-06 15:32:09 +01:00
else if (hit.collider.TryGetComponent(out Enemy enemy))
2025-10-04 12:25:29 +01:00
{
2025-10-06 15:32:09 +01:00
// Combat targeting is a different action, so it's fine to keep it separate.
2025-10-04 12:25:29 +01:00
Debug.Log("Enemy Clicked");
Player.Current.SetTarget(enemy);
}
2025-10-06 15:32:09 +01:00
else if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Ground"))
2025-10-04 12:25:29 +01:00
{
2025-10-06 15:32:09 +01:00
Debug.Log("Ground Clicked");
Player.Current.SetTarget(null);
Player.Current.Controller.MoveToPosition(hit.point);
}
}
}
void Hover ()
{
Vector2 mousePos = Mouse.current.position.ReadValue();
Ray ray = Camera.main.ScreenPointToRay(mousePos);
RaycastHit hit;
// Shoot a raycast from our mouse to what ever we are pointing at.
if (Physics.Raycast(ray, out hit, 1000, layerMask))
{
//int hitLayer = hit.collider.gameObject.layer;
//if (hitLayer == LayerMask.NameToLayer("Interactable"))
if(hit.collider.TryGetComponent(out IInteractable interactable))
{
// Set the position of the prompt to the mouse position
promptText.rectTransform.position = mousePos + new Vector2(15, -15);
promptText.gameObject.SetActive(true);
promptText.text = interactable.GetInteractPrompt();
if (hit.collider.TryGetComponent(out Character character))
2025-10-04 12:25:29 +01:00
{
2025-10-06 15:32:09 +01:00
HealthBarUI.instance.UpdateInfoPanel(character.charName, character.level, character.attitude);
2025-10-04 12:25:29 +01:00
}
else
{
2025-10-06 15:32:09 +01:00
//Debug.Log("Something Else Hovered");
2025-10-04 12:25:29 +01:00
}
}
else
{
2025-10-06 15:32:09 +01:00
//Debug.Log("Something Else Hovered");
2025-10-04 12:25:29 +01:00
}
}
}
}