Skip to content

Instantly share code, notes, and snippets.

@LostTrainDude
Created June 28, 2023 13:24
Show Gist options
  • Save LostTrainDude/95ef9f51bceb1b01bd144799375a82b1 to your computer and use it in GitHub Desktop.
Save LostTrainDude/95ef9f51bceb1b01bd144799375a82b1 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC;
using Rewired;
public class NMHotspotDetectionAndInteraction : 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()
{
GameStateManager.OnGameStateChanged += OnGameStateChanged;
EventManager.OnBeforeChangeScene += OnBeforeChangeScene;
EventManager.OnAfterChangeScene += OnAfterChangeScene;
}
private void OnDisable()
{
GameStateManager.OnGameStateChanged -= OnGameStateChanged;
EventManager.OnBeforeChangeScene -= OnBeforeChangeScene;
EventManager.OnAfterChangeScene -= OnAfterChangeScene;
}
// Start is called before the first frame update
void Start()
{
hotspotMenu = PlayerMenus.GetMenuWithName("Hotspot");
hotspotMenuGraphic = hotspotMenu.GetElementWithName("Graphic") as MenuGraphic;
hotspotMenuLabel = hotspotMenu.GetElementWithName("HotspotLabel") as MenuLabel;
}
`
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();
}
bool IsDirectControlEnabled()
=> 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)
{
KickStarter.playerCursor.SetCursorFromID(DEFAULT_CURSOR);
if (!hotspot.IsOn())
return;
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;
if (IsDirectControlEnabled())
didPressButton = rPlayer.GetButtonUp("InteractionX"); // Directly linked to Rewired
else
didPressButton = rPlayer.controllers.Mouse.GetButtonUp(0); // Directly linked to Rewired
if (didPressButton)
{
KickStarter.sceneSettings.PlayDefaultSound(GameSFXManager.instance.UIHotspotClickSound, false);
if (hotspotMenu.TargetHotspot == hotspot)
hotspot.RunUseInteraction();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment