Advertisement
DugganSC

InteractableZone.cs

Dec 22nd, 2023
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.51 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Game.Scripts.UI;
  6.  
  7.  
  8. namespace Game.Scripts.LiveObjects
  9. {
  10.     public class InteractableZone : MonoBehaviour
  11.     {
  12.         private enum ZoneType
  13.         {
  14.             Collectable,
  15.             Action,
  16.             HoldAction
  17.         }
  18.  
  19.         private enum KeyState
  20.         {
  21.             Press,
  22.             PressHold
  23.         }
  24.  
  25.         [SerializeField]
  26.         private ZoneType _zoneType;
  27.         [SerializeField]
  28.         private int _zoneID;
  29.         [SerializeField]
  30.         private int _requiredID;
  31.         [SerializeField]
  32.         [Tooltip("Press the (---) Key to .....")]
  33.         private string _displayMessage;
  34.         [SerializeField]
  35.         private GameObject[] _zoneItems;
  36.         private bool _inZone = false;
  37.         private bool _itemsCollected = false;
  38.         private bool _actionPerformed = false;
  39.         [SerializeField]
  40.         private Sprite _inventoryIcon;
  41.         [SerializeField]
  42.         private KeyCode _zoneKeyInput;
  43.         [SerializeField]
  44.         private KeyState _keyState;
  45.         [SerializeField]
  46.         private GameObject _marker;
  47.  
  48.         private PlayerInputs _playerInput;
  49.  
  50.         private bool _inHoldState = false;
  51.  
  52.         private static int _currentZoneID = 0;
  53.         public static int CurrentZoneID
  54.         {
  55.             get
  56.             {
  57.                return _currentZoneID;
  58.             }
  59.             set
  60.             {
  61.                 _currentZoneID = value;
  62.                          
  63.             }
  64.         }
  65.  
  66.  
  67.         public static event Action<InteractableZone> onZoneInteractionComplete;
  68.         public static event Action<int> onHoldStarted;
  69.         public static event Action<int> onHoldEnded;
  70.  
  71.         private void OnEnable()
  72.         {
  73.             InteractableZone.onZoneInteractionComplete += SetMarker;
  74.             _playerInput = new PlayerInputs();
  75.             _playerInput.Player.Enable();
  76.             _playerInput.Player.Interact.performed += Interact_performed;
  77.             _playerInput.Player.Interact.canceled += Interact_canceled;
  78.         }
  79.  
  80.         private void Interact_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  81.         {
  82.             if (_inZone == true)
  83.             {
  84.                 if (_keyState == KeyState.PressHold)
  85.                 {
  86.                     _inHoldState = false;
  87.                     onHoldEnded?.Invoke(_zoneID);
  88.                 }
  89.             }
  90.         }
  91.  
  92.         private void Interact_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  93.         {
  94.             if (_inZone == true)
  95.             {
  96.                 if (_keyState != KeyState.PressHold)
  97.                 {
  98.                     //press
  99.                     switch (_zoneType)
  100.                     {
  101.                         case ZoneType.Collectable:
  102.                             if (_itemsCollected == false)
  103.                             {
  104.                                 CollectItems();
  105.                                 _itemsCollected = true;
  106.                                 UIManager.Instance.DisplayInteractableZoneMessage(false);
  107.                             }
  108.                             break;
  109.  
  110.                         case ZoneType.Action:
  111.                             if (_actionPerformed == false)
  112.                             {
  113.                                 PerformAction();
  114.                                 _actionPerformed = true;
  115.                                 UIManager.Instance.DisplayInteractableZoneMessage(false);
  116.                             }
  117.                             break;
  118.                     }
  119.                 }
  120.  
  121.                 else if (_keyState == KeyState.PressHold && _inHoldState == false)
  122.                 {
  123.                     _inHoldState = true;
  124.  
  125.                     switch (_zoneType)
  126.                     {
  127.                         case ZoneType.HoldAction:
  128.                             PerformHoldAction();
  129.                             break;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.  
  135.         private void OnTriggerEnter(Collider other)
  136.         {
  137.             string _interactKeyName = _playerInput.Player.Interact.bindings[0].ToDisplayString();
  138.             if (other.CompareTag("Player") && _currentZoneID > _requiredID)
  139.             {
  140.                 switch (_zoneType)
  141.                 {
  142.                     case ZoneType.Collectable:
  143.                         if (_itemsCollected == false)
  144.                         {
  145.                             _inZone = true;
  146.                             if (_displayMessage != null)
  147.                             {
  148.                                 string message = $"Press the {_interactKeyName} key to {_displayMessage}.";
  149.                                 UIManager.Instance.DisplayInteractableZoneMessage(true, message);
  150.                             }
  151.                             else
  152.                                 UIManager.Instance.DisplayInteractableZoneMessage(true, $"Press the {_interactKeyName} key to collect");
  153.                         }
  154.                         break;
  155.  
  156.                     case ZoneType.Action:
  157.                         if (_actionPerformed == false)
  158.                         {
  159.                             _inZone = true;
  160.                             if (_displayMessage != null)
  161.                             {
  162.                                 string message = $"Press the {_interactKeyName} key to {_displayMessage}.";
  163.                                 UIManager.Instance.DisplayInteractableZoneMessage(true, message);
  164.                             }
  165.                             else
  166.                                 UIManager.Instance.DisplayInteractableZoneMessage(true, $"Press the {_interactKeyName} key to perform action");
  167.                         }
  168.                         break;
  169.  
  170.                     case ZoneType.HoldAction:
  171.                         _inZone = true;
  172.                         if (_displayMessage != null)
  173.                         {
  174.                             string message = $"Press the {_interactKeyName} key to {_displayMessage}.";
  175.                             UIManager.Instance.DisplayInteractableZoneMessage(true, message);
  176.                         }
  177.                         else
  178.                             UIManager.Instance.DisplayInteractableZoneMessage(true, $"Hold the {_interactKeyName} key to perform action");
  179.                         break;
  180.                 }
  181.             }
  182.         }
  183.  
  184.         private void Update()
  185.         {
  186.         }
  187.        
  188.         private void CollectItems()
  189.         {
  190.             foreach (var item in _zoneItems)
  191.             {
  192.                 item.SetActive(false);
  193.             }
  194.  
  195.             UIManager.Instance.UpdateInventoryDisplay(_inventoryIcon);
  196.  
  197.             CompleteTask(_zoneID);
  198.  
  199.             onZoneInteractionComplete?.Invoke(this);
  200.  
  201.         }
  202.  
  203.         private void PerformAction()
  204.         {
  205.             foreach (var item in _zoneItems)
  206.             {
  207.                 item.SetActive(true);
  208.             }
  209.  
  210.             if (_inventoryIcon != null)
  211.                 UIManager.Instance.UpdateInventoryDisplay(_inventoryIcon);
  212.  
  213.             onZoneInteractionComplete?.Invoke(this);
  214.         }
  215.  
  216.         private void PerformHoldAction()
  217.         {
  218.             UIManager.Instance.DisplayInteractableZoneMessage(false);
  219.             onHoldStarted?.Invoke(_zoneID);
  220.         }
  221.  
  222.         public GameObject[] GetItems()
  223.         {
  224.             return _zoneItems;
  225.         }
  226.  
  227.         public int GetZoneID()
  228.         {
  229.             return _zoneID;
  230.         }
  231.  
  232.         public void CompleteTask(int zoneID)
  233.         {
  234.             if (zoneID == _zoneID)
  235.             {
  236.                 _currentZoneID++;
  237.                 onZoneInteractionComplete?.Invoke(this);
  238.             }
  239.         }
  240.  
  241.         public void ResetAction(int zoneID)
  242.         {
  243.             if (zoneID == _zoneID)
  244.                 _actionPerformed = false;
  245.         }
  246.  
  247.         public void SetMarker(InteractableZone zone)
  248.         {
  249.             if (_zoneID == _currentZoneID)
  250.                 _marker.SetActive(true);
  251.             else
  252.                 _marker.SetActive(false);
  253.         }
  254.  
  255.         private void OnTriggerExit(Collider other)
  256.         {
  257.             if (other.CompareTag("Player"))
  258.             {
  259.                 _inZone = false;
  260.                 UIManager.Instance.DisplayInteractableZoneMessage(false);
  261.             }
  262.         }
  263.  
  264.         private void OnDisable()
  265.         {
  266.             InteractableZone.onZoneInteractionComplete -= SetMarker;
  267.         }      
  268.        
  269.     }
  270. }
  271.  
  272.  
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement