Practice_Unity/Assets/Scripts/Managers/InputManager.cs
SEAN cbd35babca 작업
1. ExpBar를 UIBar 로 변경해서 UIButton과 같이 여러곳에서 공용으로 사용할 수 있게 변경
2. EventBus 를 만들어서 Button같이 자기 스스로 동작을 시작할 수 있는 오브젝트들 말고 수동적인 오브젝트들이 값을 받는 방식을 관리하게 설정
  - PlayerController, UIManager, UIBar, EventBus를 연결함
3. Events 아래에 Player의 이벤트에 연관된 코드를 PlayerEvents 로 만들어서 관리함
  - 경험치 전달 양식이 여기 존재
  - 체력바나 마나바 같은 양식도 여기서 만들 예정
2025-10-20 16:54:45 +09:00

125 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
public class InputManager: IManager
{
private InputActions _action;
private Dictionary<string, Action> _registeredActions = new Dictionary<string, Action>();
public Joystick JoyStick { get; set; }
// 화면 터치를 어떻게 받아올건지에 대한 액션
public Action<Define.InputEvent> InputAction = null;
// 화면의 터치하는 실시간 좌표를 사용하는 기능을 위한 액션
public Action<Vector2> PointAction = null;
// 조이스틱의 이동을 담당하는 액션
public Action<Vector2> MoveAction = null;
bool _pressed = false;
float _pressedTime = 0.0f;
public void Init()
{
_action = new InputActions();
_action.UI.Enable();
}
public void OnUpdate()
{
JoystickInput();
AnyInput();
}
void JoystickInput()
{
if (JoyStick == null)
return;
Vector2 dir = new Vector2(JoyStick.Horizontal, JoyStick.Vertical);
MoveAction?.Invoke(dir);
}
void AnyInput()
{
PointAction?.Invoke(_action.UI.Point.ReadValue<Vector2>());
if (InputAction != null)
{
// UI 위에 마우스(손가락)가 올라가 있는지 체크
if (EventSystem.current.IsPointerOverGameObject())
{
if (_pressed)
{
InputAction.Invoke(Time.time - _pressedTime < 0.2f
? Define.InputEvent.Click
: Define.InputEvent.Up);
}
_pressed = false;
_pressedTime = 0.0f;
return;
}
if (_action.UI.Touch.IsPressed())
{
// Touch를 누르고 있는 상태
if (!_pressed)
{
InputAction.Invoke(Define.InputEvent.Down);
_pressedTime = Time.time;
}
InputAction.Invoke(Define.InputEvent.Press);
_pressed = true;
}
else
{
if (_pressed)
{
if (Time.time - _pressedTime < 0.2f)
InputAction.Invoke(Define.InputEvent.Click);
else
InputAction.Invoke(Define.InputEvent.Up);
_pressed = false;
_pressedTime = 0.0f;
}
}
}
}
public void RegisterAction(string name, Action action)
{
if (!_registeredActions.ContainsKey(name)) _registeredActions[name] = null;
UnregisterAction(name, action);
_registeredActions[name] += action;
}
public void UnregisterAction(string name, Action action)
{
if (_registeredActions.ContainsKey(name)) _registeredActions[name] -= action;
}
//MARK: ACTION INVOKE
public void OnClicked(string name)
{
if(_registeredActions.TryGetValue(name, out Action action)) action?.Invoke();
}
//MARK: CLEAR
public void Clear()
{
_action.Dispose();
InputAction = null;
PointAction = null;
MoveAction = null;
JoyStick = null;
_registeredActions.Clear();
}
}