Files
blockout/BlockOutReplica/Assets/Scripts/Gameplay/BlockOutInputController.cs
T
2026-10-20 13:21:04 +08:00

67 lines
2.5 KiB
C#

using UnityEngine;
namespace BlockOutReplica.Gameplay
{
public sealed class BlockOutInputController : MonoBehaviour
{
private BlockOutGameController game;
private Camera gameCamera;
public void Initialize(BlockOutGameController controller, Camera camera)
{ game = controller; gameCamera = camera; }
private void OnEnable()
{
if(game==null)game=GetComponent<BlockOutGameController>();
if(gameCamera==null)gameCamera=Camera.main;
}
#if UNITY_EDITOR
private void OnGUI()
{
// Game View can deliver an IMGUI click without a polled Update
// mouse-down after reload/focus changes. Preserve booster targeting
// through that native editor event; commit guards prevent double spend.
var e=Event.current;
if(game==null||gameCamera==null||game.IsInputReviewRunning||game.InputLocked||
game.SelectedBooster==BoosterType.None||e.type!=EventType.MouseDown||e.button!=0)return;
game.BeginDrag(new Vector2(e.mousePosition.x,Screen.height-e.mousePosition.y),gameCamera);
}
#endif
private void Update()
{
if (game == null || gameCamera == null || game.InputLocked || game.IsInputReviewRunning) return;
if (TryGetPointerDown(out Vector2 down)) game.BeginDrag(down, gameCamera);
if (game.IsDragging && TryGetPointerPosition(out Vector2 position))
game.ContinueDrag(position, gameCamera);
if (game.IsDragging && IsPointerUp()) game.EndDrag();
}
private static bool TryGetPointerDown(out Vector2 position)
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{ position = Input.GetTouch(0).position; return true; }
if (Input.GetMouseButtonDown(0))
{ position = Input.mousePosition; return true; }
position = default; return false;
}
private static bool TryGetPointerPosition(out Vector2 position)
{
if (Input.touchCount > 0) { position = Input.GetTouch(0).position; return true; }
position = Input.mousePosition; return true;
}
private static bool IsPointerUp()
{
if (Input.touchCount > 0)
{
TouchPhase phase = Input.GetTouch(0).phase;
return phase == TouchPhase.Ended || phase == TouchPhase.Canceled;
}
return Input.GetMouseButtonUp(0);
}
}
}