Guest User

Custom AC Hotspot detection and interaction

a guest
Jul 19th, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using AC;
  5. using Rewired;
  6.  
  7. public class MyHotspotDetectionAndInteraction : MonoBehaviour
  8. {
  9.     // The corresponding IDs in the AC Cursor Manager
  10.     private const int DEFAULT_CURSOR = 0;
  11.     private const int MOUSE_HOTSPOT = 1;
  12.     private const int BUTTON_X = 3;
  13.     private const int BUTTON_SQUARE = 7;
  14.  
  15.     private Menu hotspotMenu; // Our own local Menu
  16.     private MenuGraphic hotspotMenuGraphic;
  17.     private MenuLabel hotspotMenuLabel;
  18.  
  19.     private Rewired.Player rPlayer;
  20.  
  21.     bool inBetweenScenes;
  22.  
  23.     private void Awake()
  24.     {
  25.         rPlayer = ReInput.players.GetPlayer(0);
  26.     }
  27.  
  28.     private void OnEnable()
  29.     {
  30.         // Internal state machine event
  31.         GameStateManager.OnGameStateChanged += OnGameStateChanged;
  32.         EventManager.OnBeforeChangeScene += OnBeforeChangeScene;
  33.         EventManager.OnAfterChangeScene += OnAfterChangeScene;
  34.     }
  35.  
  36.     private void OnDisable()
  37.     {
  38.         GameStateManager.OnGameStateChanged -= OnGameStateChanged;
  39.         EventManager.OnBeforeChangeScene -= OnBeforeChangeScene;
  40.         EventManager.OnAfterChangeScene -= OnAfterChangeScene;
  41.     }
  42.  
  43.     private void OnBeforeChangeScene(string nextSceneName)
  44.     {
  45.         inBetweenScenes = true;
  46.     }
  47.  
  48.     private void OnAfterChangeScene(LoadingGame loadingGame)
  49.     {
  50.         inBetweenScenes = false;
  51.     }
  52.  
  53.     private void OnGameStateChanged(GameStates _oldState, GameStates _newState)
  54.     {
  55.         if (_newState != GameStates.PLAYING)
  56.         {
  57.             Hide();
  58.         }
  59.     }
  60.  
  61.     void Start()
  62.     {
  63.         hotspotMenu = PlayerMenus.GetMenuWithName("Hotspot");
  64.         hotspotMenuGraphic = hotspotMenu.GetElementWithName("Graphic") as MenuGraphic;
  65.         hotspotMenuLabel = hotspotMenu.GetElementWithName("HotspotLabel") as MenuLabel;
  66.     }
  67.  
  68.     bool IsDirectControlEnabled()
  69.     {
  70.         return GlobalVariables.GetBooleanValue(0);
  71.     }
  72.  
  73.     // Update is called once per frame
  74.     void Update()
  75.     {
  76.         // Attempt (failed) to force the cursor not to switch icon in-between scenes
  77.         // if currently in "point and click" mode
  78.         if (inBetweenScenes)
  79.         {
  80.             if (!IsDirectControlEnabled())
  81.             {
  82.                 KickStarter.playerCursor.SetCursorFromID(DEFAULT_CURSOR);
  83.             }
  84.         }
  85.  
  86.         if (!IsDirectControlEnabled())
  87.         {
  88.             if (KickStarter.playerInteraction.IsMouseOverHotspot())
  89.             {
  90.                 Hotspot hotspot = KickStarter.playerInteraction.GetActiveHotspot();
  91.  
  92.                 if (hotspot != null)
  93.                 {
  94.                     Show(hotspot);
  95.  
  96.                     if (KickStarter.stateHandler.IsInGameplay() && hotspotMenu != null && hotspotMenu.IsOn())
  97.                     {
  98.                         Interact(hotspot);
  99.                     }
  100.                 }
  101.             }
  102.             else
  103.             {
  104.                 Hide();
  105.             }
  106.         }
  107.         else
  108.         {
  109.             Hotspot[] detectedHotspots = KickStarter.player.hotspotDetector.GetAllDetectedHotspots();
  110.             if (detectedHotspots.Length > 0)
  111.             {
  112.                 Hotspot hotspot = detectedHotspots[0];
  113.                 Show(hotspot);
  114.  
  115.                 if (KickStarter.stateHandler.IsInGameplay() && hotspotMenu != null && hotspotMenu.IsOn())
  116.                 {
  117.                     Interact(hotspot);
  118.                 }
  119.             }
  120.             else
  121.             {
  122.                 Hide();
  123.             }
  124.         }
  125.  
  126.         if (KickStarter.stateHandler.gameState == GameState.Cutscene)
  127.         {
  128.             Hide();
  129.         }
  130.     }
  131.  
  132.     private void Show(Hotspot hotspot)
  133.     {
  134.         if (!hotspot.IsOn())
  135.         {
  136.             return;
  137.         }
  138.  
  139.         // Internal state machine
  140.         if (GameStateManager.instance.State != GameStates.PLAYING)
  141.         {
  142.             return;
  143.         }
  144.  
  145.         hotspotMenu.SetHotspot(hotspot, null);
  146.         hotspotMenuLabel.label = hotspot.GetName(Options.GetLanguage());
  147.  
  148.         // If in "Point and Click" mode, make sure that the cursor icon is
  149.         // the default one and update the hotspot menu graphic icon
  150.         if (!IsDirectControlEnabled())
  151.         {
  152.             if (hotspot != null && hotspot.GetFirstUseButton() != null)
  153.             {
  154.                 hotspot.GetFirstUseButton().iconID = DEFAULT_CURSOR;
  155.             }
  156.            
  157.             hotspotMenuGraphic.graphic = KickStarter.cursorManager.GetCursorIconFromID(MOUSE_HOTSPOT);
  158.         }
  159.         else
  160.         {
  161.             // If in "Direct control" mode, check whether you're using a Playstation controller
  162.             // or a different one, and update the hotspot menu graphic icon accordingly
  163.             if (ControllerGlyphs.instance.CurrentController(ControllerTypes.PS3) ||
  164.                 ControllerGlyphs.instance.CurrentController(ControllerTypes.PS4))
  165.             {
  166.                 hotspotMenuGraphic.graphic = KickStarter.cursorManager.GetCursorIconFromID(BUTTON_SQUARE);
  167.             }
  168.             else
  169.             {
  170.                 hotspotMenuGraphic.graphic = KickStarter.cursorManager.GetCursorIconFromID(BUTTON_X);
  171.  
  172.                 if (hotspot != null && hotspot.GetFirstUseButton() != null)
  173.                 {
  174.                     hotspot.GetFirstUseButton().iconID = BUTTON_X;
  175.                 }
  176.             }
  177.         }
  178.  
  179.         // Turn the menu on if the menu is not visible
  180.         if (!hotspotMenu.IsVisible())
  181.         {
  182.             hotspotMenu.TurnOn();
  183.         }
  184.     }
  185.  
  186.     private void Hide()
  187.     {
  188.         if (hotspotMenu != null)
  189.         {
  190.             hotspotMenu.TurnOff(false);
  191.         }
  192.     }
  193.  
  194.     private void Interact(Hotspot hotspot)
  195.     {
  196.         bool didPressButton;
  197.  
  198.         // Both keypress checks are directly linked to Rewired
  199.         if (IsDirectControlEnabled())
  200.         {
  201.             didPressButton = rPlayer.GetButtonUp("InteractionX");
  202.         }
  203.         else
  204.         {
  205.             didPressButton = rPlayer.controllers.Mouse.GetButtonUp(0);
  206.         }
  207.  
  208.         if (didPressButton)
  209.         {
  210.             KickStarter.sceneSettings.PlayDefaultSound(GameSFXManager.instance.UIHotspotClickSound, false);
  211.  
  212.             if (hotspotMenu.TargetHotspot == hotspot)
  213.             {
  214.                 hotspot.RunUseInteraction();
  215.             }
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment