Ver.0.3.0.33
This commit is contained in:
@@ -1,34 +1,41 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AibisDream.Framework;
|
||||
using AibisDream.Kit;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public class CursorManager : MonoBehaviour
|
||||
public class CursorManager : Singleton<CursorManager>
|
||||
{
|
||||
private const float DelayTime = 0.1f;
|
||||
|
||||
[Header("指针图片")] public Sprite defaultCursor;
|
||||
public Sprite nextTalk, talking, option, interactive, holding, disable, eye;
|
||||
public Sprite nextTalk, talking, option, interactive, holding, disable;
|
||||
|
||||
private Image _cursor;
|
||||
private GraphicRaycaster _raycaster;
|
||||
|
||||
private Sprite _curCursor;
|
||||
private CursorState _stateCache;
|
||||
|
||||
#region 状态相关
|
||||
|
||||
private Coroutine _switchCoroutine;
|
||||
private CursorState _curState = CursorState.Default;
|
||||
private bool _isTalking;
|
||||
[HideInInspector] public bool isInMenu;
|
||||
private bool _isInGame;
|
||||
|
||||
#endregion
|
||||
|
||||
// 卸载时注销事件
|
||||
private readonly List<IUnRegister> _unRegisters = new();
|
||||
|
||||
public void Awake()
|
||||
private void Start()
|
||||
{
|
||||
InitRefs();
|
||||
InitCursor();
|
||||
@@ -59,14 +66,54 @@ namespace AibisDream
|
||||
private void InitRefs()
|
||||
{
|
||||
_cursor = transform.Find("Cursor").GetComponent<Image>();
|
||||
_raycaster = UIManager.Instance.Canvas.GetComponent<GraphicRaycaster>();
|
||||
}
|
||||
|
||||
private void RegisterEvent()
|
||||
{
|
||||
// 此处监听所有可能对鼠标状态产生影响的事件
|
||||
RegisterGameEvent();
|
||||
RegisterDialogEvent();
|
||||
RegisterTriggerEvent();
|
||||
RegisterMenu();
|
||||
}
|
||||
|
||||
private void RegisterGameEvent()
|
||||
{
|
||||
_unRegisters.Add(EnumEventSystem.Global.Register(GameLoopEnum.GameStart, OnGameStart));
|
||||
_unRegisters.Add(EnumEventSystem.Global.Register(GameLoopEnum.GameQuit, OnGameQuit));
|
||||
_unRegisters.Add(EnumEventSystem.Global.Register(GameLoopEnum.PauseGame, OnPauseGame));
|
||||
_unRegisters.Add(EnumEventSystem.Global.Register(GameLoopEnum.UnPauseGame, OnUnPauseGame));
|
||||
}
|
||||
|
||||
private void OnGameStart()
|
||||
{
|
||||
_isInGame = true;
|
||||
}
|
||||
|
||||
private void OnUnPauseGame()
|
||||
{
|
||||
_curState = _stateCache;
|
||||
_stateCache = CursorState.Default;
|
||||
|
||||
_isInGame = true;
|
||||
}
|
||||
|
||||
private void OnPauseGame()
|
||||
{
|
||||
_stateCache = _curState;
|
||||
_curState = CursorState.Default;
|
||||
|
||||
_isInGame = false;
|
||||
}
|
||||
|
||||
private void OnGameQuit()
|
||||
{
|
||||
_curCursor = defaultCursor;
|
||||
|
||||
_curState = CursorState.Default;
|
||||
_stateCache = CursorState.Default;
|
||||
|
||||
_isInGame = false;
|
||||
}
|
||||
|
||||
private void RegisterDialogEvent()
|
||||
@@ -98,22 +145,48 @@ namespace AibisDream
|
||||
EnumEventSystem.Global.Register<TriggerEnum, IInteraction>(TriggerEnum.PointerUp, OnPointerUp));
|
||||
}
|
||||
|
||||
private void RegisterMenu()
|
||||
{
|
||||
_unRegisters.Add(EnumEventSystem.Global.Register<EventEnum, bool>(EventEnum.Menu, OnMenuChange));
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
|
||||
UpdateCursor();
|
||||
HandleDialog();
|
||||
}
|
||||
|
||||
private void UpdateCursor()
|
||||
{
|
||||
// 再判断是不是浮在可交互物上
|
||||
_cursor.sprite = !_isInGame || IsOverSelected() ? defaultCursor : _curCursor;
|
||||
}
|
||||
|
||||
private void HandleDialog()
|
||||
{
|
||||
if (!_isInGame) return;
|
||||
if (!Input.GetMouseButtonDown(0) && !Input.GetKeyDown(KeyCode.Space)) return;
|
||||
|
||||
// 控制对话
|
||||
if (_isTalking)
|
||||
{
|
||||
if (_isTalking && !isInMenu)
|
||||
{
|
||||
DialogController.Instance.JumpLine();
|
||||
}
|
||||
DialogController.Instance.JumpLine();
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsOverSelected()
|
||||
{
|
||||
if (!EventSystem.current.IsPointerOverGameObject())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var eventData = new PointerEventData(EventSystem.current)
|
||||
{
|
||||
position = Input.mousePosition
|
||||
};
|
||||
|
||||
var results = new List<RaycastResult>();
|
||||
_raycaster.Raycast(eventData, results);
|
||||
|
||||
return results.Count > 0 && results.Any(result => result.gameObject.CompareTag("Selectable"));
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
// // 将虚拟指针与鼠标位置同步
|
||||
@@ -139,7 +212,7 @@ namespace AibisDream
|
||||
_switchCoroutine = StartCoroutine(DelayAction(() =>
|
||||
{
|
||||
_isTalking = false;
|
||||
_cursor.sprite = defaultCursor;
|
||||
_curCursor = defaultCursor;
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -155,7 +228,7 @@ namespace AibisDream
|
||||
_switchCoroutine = StartCoroutine(DelayAction(() =>
|
||||
{
|
||||
_isTalking = false;
|
||||
_cursor.sprite = defaultCursor;
|
||||
_curCursor = defaultCursor;
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -212,17 +285,6 @@ namespace AibisDream
|
||||
SwitchCursor(EventSystemEx.Instance.holdingObj == target.GetGameObject() ? interactive : defaultCursor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 菜单打开
|
||||
|
||||
private void OnMenuChange(bool isMenuOpen)
|
||||
{
|
||||
isInMenu = isMenuOpen;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -235,7 +297,7 @@ namespace AibisDream
|
||||
private void SwitchCursor(Sprite sprite)
|
||||
{
|
||||
StopDelayCursor();
|
||||
_cursor.sprite = sprite;
|
||||
_curCursor = sprite;
|
||||
}
|
||||
|
||||
private void StopDelayCursor()
|
||||
@@ -259,7 +321,6 @@ namespace AibisDream
|
||||
public enum CursorState
|
||||
{
|
||||
Default,
|
||||
Interactive,
|
||||
Eye
|
||||
Interactive
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user