using UnityEngine;
using System.Collections;
using Rewired;
using Elven;
using Elven.Helpers;
using System;
namespace DungeonCleaner.Input
{
public enum InputMode { None, Mouse, Gamepad }
public class InputGather : Singleton<InputGather>
{
public event EventHandler<InputMode> OnInputModeChanged;
public InputMode CurrentInputMode { get; private set; }
Player player;
protected override void Awake()
{
base.Awake();
player = ReInput.players.GetPlayer(0);
}
bool SimulatingControls;
bool SimulatedFrame;
public void SimulateControls(GameplayInputs gameplay, bool UntilManualDeactivate)
{
SimulatedFrame = true;
Gameplay = gameplay;
if (UntilManualDeactivate) SimulatingControls = true;
}
public void StopSimulatingControls()
{
SimulatingControls = false;
}
public enum InputMapType
{
Gameplay,
Debug,
Menu,
Dialog
}
public void SetInputMap(InputMapType mapType, bool enabled)
{
if (player == null) return;
string mapName = mapType.ToString();
player.controllers.maps.SetMapsEnabled(enabled, mapName);
}
public GameplayInputs Gameplay;
public DebugInputs Debugging;
public MenuInputs Menu;
public DialogInputs Dialog;
private bool preventGameplayInputs;
public bool PreventGameplayInputs { get => preventGameplayInputs; set => preventGameplayInputs = value; }
public struct DialogInputs
{
public bool AdvanceDialog;
}
public struct GameplayInputs
{
public float HorizontalMovement;
public float VerticalMovement;
public bool CrouchDown;
public bool CrouchUp;
public bool CrouchHeld;
public bool JumpDown;
public bool JumpUp;
public bool JumpHeld;
public bool UseItem;
public bool Interact;
public bool CancelInteract;
public bool Taunt;
public bool Dodge;
public bool ChargeAimLaunch;
public bool ShootAimLaunch;
}
public struct DebugInputs
{
public bool ToggleConsole;
public bool EnterCommand;
}
public struct MenuInputs
{
public bool Up;
public bool Down;
public bool Left;
public bool Right;
public bool Accept;
public bool Cancel;
public bool Pause;
public bool Diary;
public bool RShoulder;
public bool LShoulder;
}
public void ClearCurrentInputs()
{
Gameplay = new();
Debugging = new();
Menu = new();
SimulateControls(new(), false);
}
private void Update()
{
ListenForControllerChange();
if (SimulatedFrame)
{
if (!SimulatingControls) SimulatedFrame = false;
return;
}
Gameplay = UpdateGameplayInput();
Debugging = UpdateDebugInputs();
Menu = UpdateMenuInput();
Dialog = UpdateDialogInputs();
}
public DialogInputs UpdateDialogInputs()
{
DialogInputs dialog = new();
dialog.AdvanceDialog = player.GetButtonDown("AdvanceDialog");
return dialog;
}
public GameplayInputs UpdateGameplayInput()
{
GameplayInputs gameplay = new();
//jump
if (preventGameplayInputs)
{
Debug.LogWarning("GameplayInputs está apagado, recuerda siempre rehabilitarlo inmediatamente después de procesar" +
"lo que sea que quisieras hacer");
return gameplay;
}
gameplay.JumpDown = player.GetButtonDown("Jump");
gameplay.JumpUp = player.GetButtonUp("Jump");
gameplay.JumpHeld = player.GetButton("Jump");
gameplay.UseItem = player.GetButtonDown("UseItem");
gameplay.Interact = player.GetButtonDown("Interact");
gameplay.CancelInteract = player.GetButtonUp("Interact");
gameplay.Dodge = player.GetButtonDown("Dodge");
//Shoot
gameplay.ChargeAimLaunch = player.GetButtonDown("AimShoot");
gameplay.ShootAimLaunch = player.GetButtonUp("AimShoot");
gameplay.Taunt = player.GetButtonDown("Taunt");
//Axis
gameplay.HorizontalMovement = player.GetAxisRaw("HorizontalAxis");
gameplay.VerticalMovement = player.GetAxisRaw("VerticalAxis");
//crouch
gameplay.CrouchDown = player.GetButtonDown("Crouch");
gameplay.CrouchUp = player.GetButtonUp("Crouch");
gameplay.CrouchHeld = player.GetButton("Crouch");
return gameplay;
}
public MenuInputs UpdateMenuInput()
{
MenuInputs menuInputs = new();
menuInputs.Up = player.GetButtonDown("Up");
menuInputs.Down = player.GetButtonDown("Down");
menuInputs.Left = player.GetButtonDown("Left");
menuInputs.Right = player.GetButtonDown("Right");
menuInputs.Accept = player.GetButtonDown("Accept");
menuInputs.Cancel = player.GetButtonDown("Cancel");
menuInputs.Pause = player.GetButtonDown("Pause");
menuInputs.Diary = player.GetButtonDown("Diary");
menuInputs.RShoulder = player.GetButtonDown("PageRight");
menuInputs.LShoulder = player.GetButtonDown("PageLeft");
return menuInputs;
}
public DebugInputs UpdateDebugInputs()
{
DebugInputs debugInputs = new();
debugInputs.ToggleConsole = player.GetButtonDown("ToggleConsole");
debugInputs.EnterCommand = player.GetButtonDown("EnterCommand");
return debugInputs;
}
public Vector2 GetMouseDirectionTowardsPoint(Vector2 point)
{
var controllerType = ReInput.controllers.GetLastActiveControllerType();
switch (controllerType)
{
case ControllerType.Keyboard:
case ControllerType.Mouse:
Vector2 mousePos = GetMousePos();
return (mousePos - point).normalized;
case ControllerType.Joystick:
case ControllerType.Custom:
//TODO Controller mouse aim
return Vector2.zero;
}
return Vector2.zero;
}
public Vector2 GetMousePos()
{
return Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);
}
private void ListenForControllerChange()
{
if (ReInput.controllers.GetLastActiveControllerType() == ControllerType.Mouse
|| ReInput.controllers.GetLastActiveControllerType() == ControllerType.Keyboard)
{
SetInputMode(InputMode.Mouse);
}
else if (ReInput.controllers.GetLastActiveControllerType() == ControllerType.Joystick)
SetInputMode(InputMode.Gamepad);
}
void SetInputMode(InputMode mode)
{
if (CurrentInputMode == mode) return;
CurrentInputMode = mode;
OnInputModeChanged?.Invoke(this, mode);
}
}
}