93 lines
2.4 KiB
C#
93 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace Watermelon
|
|
{
|
|
public static class Control
|
|
{
|
|
public static InputType InputType { get; private set; }
|
|
|
|
public static IControlBehavior CurrentControl { get; private set; }
|
|
|
|
public static GamepadData GamepadData { get; private set; }
|
|
|
|
public delegate void OnInputChangedCallback(InputType input);
|
|
public static event OnInputChangedCallback OnInputChanged;
|
|
|
|
private static GameObject ownerObject;
|
|
|
|
public static bool IsInitialized { get; private set; } = false;
|
|
|
|
public static void Init(InputType inputType, GamepadData gamepadData, GameObject owner)
|
|
{
|
|
ownerObject = owner;
|
|
|
|
InputType = inputType;
|
|
GamepadData = gamepadData;
|
|
|
|
if (GamepadData != null) GamepadData.Init();
|
|
|
|
IsInitialized = true;
|
|
}
|
|
|
|
public static void ChangeInputType(InputType inputType)
|
|
{
|
|
InputType = inputType;
|
|
|
|
Object.Destroy(CurrentControl as MonoBehaviour);
|
|
|
|
switch (inputType)
|
|
{
|
|
case InputType.Gamepad:
|
|
|
|
GamepadControl gamepadControl = ownerObject.AddComponent<GamepadControl>();
|
|
gamepadControl.Init();
|
|
|
|
CurrentControl = gamepadControl;
|
|
|
|
break;
|
|
|
|
case InputType.Keyboard:
|
|
KeyboardControl keyboardControl = ownerObject.AddComponent<KeyboardControl>();
|
|
keyboardControl.Init();
|
|
|
|
CurrentControl = keyboardControl;
|
|
break;
|
|
}
|
|
|
|
OnInputChanged?.Invoke(inputType);
|
|
}
|
|
|
|
public static void SetControl(IControlBehavior controlBehavior)
|
|
{
|
|
CurrentControl = controlBehavior;
|
|
}
|
|
|
|
public static void EnableMovementControl()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if(CurrentControl == null)
|
|
{
|
|
Debug.LogError("[Control]: Control behavior isn't set!");
|
|
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
CurrentControl.EnableMovementControl();
|
|
}
|
|
|
|
public static void DisableMovementControl()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (CurrentControl == null)
|
|
{
|
|
Debug.LogError("[Control]: Control behavior isn't set!");
|
|
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
CurrentControl.DisableMovementControl();
|
|
}
|
|
}
|
|
} |