85 lines
2.6 KiB
C#
85 lines
2.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using Spaceshooter.Combat;
|
|
using Spaceshooter.Managers;
|
|
|
|
namespace Spaceshooter.Player
|
|
{
|
|
public class PlayerInput3D : MonoBehaviour
|
|
{
|
|
private Camera mainCamera;
|
|
|
|
private void Start()
|
|
{
|
|
mainCamera = Camera.main;
|
|
if (mainCamera == null)
|
|
{
|
|
mainCamera = FindObjectOfType<Camera>();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// Handle Pause Toggle
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
if (GameManager.Instance != null)
|
|
{
|
|
GameManager.Instance.TogglePause();
|
|
}
|
|
}
|
|
|
|
// Only process game input while in Playing state
|
|
if (GameManager.Instance != null && GameManager.Instance.CurrentState != GameState.Playing)
|
|
{
|
|
return;
|
|
}
|
|
|
|
HandleWeaponSlotHotkeys();
|
|
HandleMouseClick();
|
|
}
|
|
|
|
private void HandleWeaponSlotHotkeys()
|
|
{
|
|
if (PlayerShip3D.Instance == null) return;
|
|
|
|
if (Input.GetKeyDown(KeyCode.Alpha1)) PlayerShip3D.Instance.SetActiveSlot(0);
|
|
if (Input.GetKeyDown(KeyCode.Alpha2)) PlayerShip3D.Instance.SetActiveSlot(1);
|
|
if (Input.GetKeyDown(KeyCode.Alpha3)) PlayerShip3D.Instance.SetActiveSlot(2);
|
|
}
|
|
|
|
private void HandleMouseClick()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
// Don't fire through UI elements
|
|
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (mainCamera == null)
|
|
{
|
|
mainCamera = Camera.main;
|
|
if (mainCamera == null) return;
|
|
}
|
|
|
|
// 2D Mouse World Raycast
|
|
Vector3 screenPos = Input.mousePosition;
|
|
Vector3 worldPos3 = mainCamera.ScreenToWorldPoint(screenPos);
|
|
Vector2 mouseWorldPos = new Vector2(worldPos3.x, worldPos3.y);
|
|
|
|
RaycastHit2D hit = Physics2D.Raycast(mouseWorldPos, Vector2.zero);
|
|
if (hit.collider != null)
|
|
{
|
|
IClickable3D clickable = hit.collider.GetComponent<IClickable3D>();
|
|
if (clickable != null)
|
|
{
|
|
clickable.OnClickedByPlayer(hit.point);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|