Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using AC;
- using Rewired;
- public class MyHotspotDetectionAndInteraction : MonoBehaviour
- {
- // The corresponding IDs in the AC Cursor Manager
- private const int DEFAULT_CURSOR = 0;
- private const int MOUSE_HOTSPOT = 1;
- private const int BUTTON_X = 3;
- private const int BUTTON_SQUARE = 7;
- private Menu hotspotMenu; // Our own local Menu
- private MenuGraphic hotspotMenuGraphic;
- private MenuLabel hotspotMenuLabel;
- private Rewired.Player rPlayer;
- bool inBetweenScenes;
- private void Awake()
- {
- rPlayer = ReInput.players.GetPlayer(0);
- }
- private void OnEnable()
- {
- // Internal state machine event
- GameStateManager.OnGameStateChanged += OnGameStateChanged;
- EventManager.OnBeforeChangeScene += OnBeforeChangeScene;
- EventManager.OnAfterChangeScene += OnAfterChangeScene;
- }
- private void OnDisable()
- {
- GameStateManager.OnGameStateChanged -= OnGameStateChanged;
- EventManager.OnBeforeChangeScene -= OnBeforeChangeScene;
- EventManager.OnAfterChangeScene -= OnAfterChangeScene;
- }
- private void OnBeforeChangeScene(string nextSceneName)
- {
- inBetweenScenes = true;
- }
- private void OnAfterChangeScene(LoadingGame loadingGame)
- {
- inBetweenScenes = false;
- }
- private void OnGameStateChanged(GameStates _oldState, GameStates _newState)
- {
- if (_newState != GameStates.PLAYING)
- {
- Hide();
- }
- }
- void Start()
- {
- hotspotMenu = PlayerMenus.GetMenuWithName("Hotspot");
- hotspotMenuGraphic = hotspotMenu.GetElementWithName("Graphic") as MenuGraphic;
- hotspotMenuLabel = hotspotMenu.GetElementWithName("HotspotLabel") as MenuLabel;
- }
- bool IsDirectControlEnabled()
- {
- return GlobalVariables.GetBooleanValue(0);
- }
- // Update is called once per frame
- void Update()
- {
- // Attempt (failed) to force the cursor not to switch icon in-between scenes
- // if currently in "point and click" mode
- if (inBetweenScenes)
- {
- if (!IsDirectControlEnabled())
- {
- KickStarter.playerCursor.SetCursorFromID(DEFAULT_CURSOR);
- }
- }
- if (!IsDirectControlEnabled())
- {
- if (KickStarter.playerInteraction.IsMouseOverHotspot())
- {
- Hotspot hotspot = KickStarter.playerInteraction.GetActiveHotspot();
- if (hotspot != null)
- {
- Show(hotspot);
- if (KickStarter.stateHandler.IsInGameplay() && hotspotMenu != null && hotspotMenu.IsOn())
- {
- Interact(hotspot);
- }
- }
- }
- else
- {
- Hide();
- }
- }
- else
- {
- Hotspot[] detectedHotspots = KickStarter.player.hotspotDetector.GetAllDetectedHotspots();
- if (detectedHotspots.Length > 0)
- {
- Hotspot hotspot = detectedHotspots[0];
- Show(hotspot);
- if (KickStarter.stateHandler.IsInGameplay() && hotspotMenu != null && hotspotMenu.IsOn())
- {
- Interact(hotspot);
- }
- }
- else
- {
- Hide();
- }
- }
- if (KickStarter.stateHandler.gameState == GameState.Cutscene)
- {
- Hide();
- }
- }
- private void Show(Hotspot hotspot)
- {
- if (!hotspot.IsOn())
- {
- return;
- }
- // Internal state machine
- if (GameStateManager.instance.State != GameStates.PLAYING)
- {
- return;
- }
- hotspotMenu.SetHotspot(hotspot, null);
- hotspotMenuLabel.label = hotspot.GetName(Options.GetLanguage());
- // If in "Point and Click" mode, make sure that the cursor icon is
- // the default one and update the hotspot menu graphic icon
- if (!IsDirectControlEnabled())
- {
- if (hotspot != null && hotspot.GetFirstUseButton() != null)
- {
- hotspot.GetFirstUseButton().iconID = DEFAULT_CURSOR;
- }
- hotspotMenuGraphic.graphic = KickStarter.cursorManager.GetCursorIconFromID(MOUSE_HOTSPOT);
- }
- else
- {
- // If in "Direct control" mode, check whether you're using a Playstation controller
- // or a different one, and update the hotspot menu graphic icon accordingly
- if (ControllerGlyphs.instance.CurrentController(ControllerTypes.PS3) ||
- ControllerGlyphs.instance.CurrentController(ControllerTypes.PS4))
- {
- hotspotMenuGraphic.graphic = KickStarter.cursorManager.GetCursorIconFromID(BUTTON_SQUARE);
- }
- else
- {
- hotspotMenuGraphic.graphic = KickStarter.cursorManager.GetCursorIconFromID(BUTTON_X);
- if (hotspot != null && hotspot.GetFirstUseButton() != null)
- {
- hotspot.GetFirstUseButton().iconID = BUTTON_X;
- }
- }
- }
- // Turn the menu on if the menu is not visible
- if (!hotspotMenu.IsVisible())
- {
- hotspotMenu.TurnOn();
- }
- }
- private void Hide()
- {
- if (hotspotMenu != null)
- {
- hotspotMenu.TurnOff(false);
- }
- }
- private void Interact(Hotspot hotspot)
- {
- bool didPressButton;
- // Both keypress checks are directly linked to Rewired
- if (IsDirectControlEnabled())
- {
- didPressButton = rPlayer.GetButtonUp("InteractionX");
- }
- else
- {
- didPressButton = rPlayer.controllers.Mouse.GetButtonUp(0);
- }
- if (didPressButton)
- {
- KickStarter.sceneSettings.PlayDefaultSound(GameSFXManager.instance.UIHotspotClickSound, false);
- if (hotspotMenu.TargetHotspot == hotspot)
- {
- hotspot.RunUseInteraction();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment