Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AC;
- using UnityEngine;
- using Rewired;
- public enum ControlState
- {
- DIRECT,
- POINT_CLICK
- }
- public class ControlSwitcher : MonoBehaviour
- {
- Rewired.Player player;
- [SerializeField] private ControlState _state;
- public ControlState State { get => _state; set => _state = value; }
- public BoxCollider2D[] SceneColliders;
- public Animator Animator;
- public bool Direct = false;
- public float dirNormalized = 0f;
- public delegate void Delegate_OnSwitchControlType();
- public static event Delegate_OnSwitchControlType OnSwitchToDirectControl;
- public static event Delegate_OnSwitchControlType OnSwitchToPointAndClick;
- private void Awake()
- {
- player = ReInput.players.GetPlayer(0);
- }
- private void OnEnable()
- {
- EventManager.OnSetPlayer += UpdateSceneColliders;
- EventManager.OnInitialiseScene += UpdateSceneColliders;
- EventManager.OnInitialiseScene += EventManager_OnInitialiseScene;
- EventManager.OnAfterChangeScene += EventManager_OnAfterChangeScene;
- EventManager.OnEnterGameState += EventManager_OnEnterGameState;
- player.controllers.AddLastActiveControllerChangedDelegate(OnControllerChange);
- }
- private void OnDisable()
- {
- EventManager.OnSetPlayer -= UpdateSceneColliders;
- EventManager.OnInitialiseScene -= UpdateSceneColliders;
- EventManager.OnInitialiseScene -= EventManager_OnInitialiseScene;
- EventManager.OnAfterChangeScene -= EventManager_OnAfterChangeScene;
- EventManager.OnEnterGameState -= EventManager_OnEnterGameState;
- player.controllers.RemoveLastActiveControllerChangedDelegate(OnControllerChange);
- }
- private void EventManager_OnEnterGameState(GameState gameState)
- {
- if (gameState == GameState.Cutscene)
- {
- DisableColliders();
- }
- else if (gameState == GameState.Normal)
- {
- if (Direct)
- {
- EnableColliders();
- }
- else
- {
- DisableColliders();
- }
- }
- }
- private void EventManager_OnInitialiseScene()
- {
- InputMethodSwitcher();
- }
- private void EventManager_OnAfterChangeScene(LoadingGame loadingGame)
- {
- InputMethodSwitcher();
- }
- private void InputMethodSwitcher()
- {
- Rewired.Controller controller = GameInputManager.instance.LastActiveController();
- if (controller != null)
- {
- if (controller is Joystick)
- {
- SwitchToDirect();
- }
- else if (controller is Mouse || controller is Keyboard)
- {
- SwitchToPointAndClick();
- }
- }
- }
- void OnControllerChange(Rewired.Player player, Controller controller)
- {
- switch (controller.type)
- {
- case ControllerType.Joystick:
- SwitchToDirect();
- break;
- case ControllerType.Mouse:
- case ControllerType.Keyboard:
- SwitchToPointAndClick();
- break;
- }
- }
- private void UpdateSceneColliders()
- {
- SceneColliders = GameObject.Find("_CollisionCubes").GetComponentsInChildren<BoxCollider2D>();
- }
- private void UpdateSceneColliders(AC.Player p)
- {
- SceneColliders = GameObject.Find("_CollisionCubes").GetComponentsInChildren<BoxCollider2D>();
- }
- // If playing in "Point and Click" mode disable all the wall colliders
- // used when playing in "Direct" mode
- private void DisableColliders()
- {
- for (int i = 0; i < SceneColliders.Length; i++)
- {
- SceneColliders[i].isTrigger = true;
- }
- }
- // If playing in "Direct" mode enable all the wall colliders
- private void EnableColliders()
- {
- for (int i = 0; i < SceneColliders.Length; i++)
- {
- SceneColliders[i].isTrigger = false;
- }
- }
- private void SwitchToPointAndClick()
- {
- GlobalVariables.SetBooleanValue(0, false);
- Animator.SetBool("DirectControl", false);
- DisableColliders();
- KickStarter.settingsManager.movementMethod = MovementMethod.PointAndClick;
- KickStarter.settingsManager.hotspotDetection = HotspotDetection.MouseOver;
- KickStarter.settingsManager.inputMethod = InputMethod.MouseAndKeyboard;
- Direct = false;
- KickStarter.menuManager.keyboardControlWhenPaused = false;
- KickStarter.playerCursor.ForceOffCursor = false;
- _state = ControlState.POINT_CLICK;
- OnSwitchToPointAndClick?.Invoke();
- }
- private void SwitchToDirect()
- {
- if (KickStarter.stateHandler.IsInGameplay()) KickStarter.player.Halt();
- GlobalVariables.SetBooleanValue(0, true);
- Animator.SetBool("DirectControl", true);
- if (KickStarter.stateHandler.gameState != GameState.Cutscene) EnableColliders();
- KickStarter.settingsManager.movementMethod = MovementMethod.Direct;
- KickStarter.settingsManager.hotspotDetection = HotspotDetection.PlayerVicinity;
- KickStarter.settingsManager.inputMethod = InputMethod.KeyboardOrController;
- Direct = true;
- KickStarter.menuManager.keyboardControlWhenPaused = true;
- KickStarter.playerCursor.ForceOffCursor = true;
- _state = ControlState.DIRECT;
- OnSwitchToDirectControl?.Invoke();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment