Advertisement
dronkowitz

HUD.cs (Learning Unity)

Nov 4th, 2022 (edited)
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.UI;
  7.  
  8. public class HUD : MonoBehaviour
  9. {
  10.     public static HUD Instance { get; private set; }
  11.     public Image cursorImage;
  12.     public Sprite standard, active, notActive;
  13.     public GameObject conversationScreen;
  14.     public TMP_Text conversationText;
  15.     public Item heldItem;
  16.     public bool paused;
  17.     public GameObject saveScreen, pauseScreen, gameScreen;
  18.     public enum CursorTypes
  19.     {
  20.         standard,
  21.         active,
  22.         notActive,
  23.     }
  24.  
  25.     // Start is called before the first frame update
  26.     void Start()
  27.     {
  28.         Instance = this;
  29.         conversationScreen.SetActive(false);
  30.         SetCursorType(CursorTypes.standard);
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.         if (conversationScreen.activeSelf)
  37.         {
  38.             return;
  39.         }
  40.         cursorImage.transform.parent.position = Input.mousePosition;
  41.         if (Input.GetKeyDown(KeyCode.Escape))
  42.         {
  43.             if(!pauseScreen.activeSelf)
  44.             {
  45.                 OpenPauseMenu();
  46.             }
  47.             else
  48.             {
  49.                 ClosePauseMenu();
  50.             }
  51.         }
  52.     }
  53.  
  54.     public void OpenPauseMenu()
  55.     {
  56.         paused = true;
  57.         Time.timeScale = 0;
  58.         if (saveScreen.activeSelf)
  59.         {
  60.             saveScreen.SetActive(false);
  61.         }
  62.         pauseScreen.SetActive(true);
  63.         gameScreen.SetActive(false);
  64.     }
  65.  
  66.     public void ClosePauseMenu()
  67.     {
  68.         paused = false;
  69.         Time.timeScale = 1;
  70.         pauseScreen.SetActive(false);
  71.         gameScreen.SetActive(true);
  72.         saveScreen.SetActive(false);
  73.     }
  74.  
  75.     public void OpenSaveScreen()
  76.     {
  77.         paused = true;
  78.         Time.timeScale = 0;
  79.         pauseScreen.SetActive(false);
  80.         gameScreen.SetActive(false);
  81.         saveScreen.SetActive(true);
  82.     }
  83.  
  84.     public void CloseSaveScreen()
  85.     {
  86.         paused = false;
  87.         Time.timeScale = 1;
  88.         pauseScreen.SetActive(false);
  89.         gameScreen.SetActive(true);
  90.         saveScreen.SetActive(false);
  91.     }
  92.  
  93.     public void SetCursorType(CursorTypes type)
  94.     {
  95.         switch (type)
  96.         {
  97.             case CursorTypes.standard:
  98.                 cursorImage.sprite = standard;
  99.                 return;
  100.             case CursorTypes.active:
  101.                 cursorImage.sprite = active;
  102.                 return;
  103.             case CursorTypes.notActive:
  104.                 cursorImage.sprite = notActive;
  105.                 return;
  106.         }
  107.     }
  108.  
  109.     public void StartConverstation(ConversationPiece[] newConversations)
  110.     {
  111.         StartCoroutine(ConversationProcess(newConversations));
  112.     }
  113.    
  114.  
  115.     private IEnumerator ConversationProcess(ConversationPiece[] newConversations)
  116.     {
  117.         conversationScreen.SetActive(true);
  118.         foreach(ConversationPiece c in newConversations)
  119.         {
  120.             conversationText.text = c.convo;
  121.             while (!Input.GetKey(KeyCode.Return)) yield return null;
  122.             while (Input.GetKey(KeyCode.Return)) yield return null;
  123.             c.sentenceEvent?.Invoke();
  124.         }
  125.         conversationScreen.SetActive(false);
  126.     }
  127.    
  128.     public void LoadGame(int saveSlot)
  129.     {
  130.         GameManager.Instance.LoadData(saveSlot);
  131.     }
  132.  
  133.     public void SaveGame(int saveSlot)
  134.     {
  135.         GameManager.Instance.SaveData(saveSlot);
  136.     }
  137. }
  138.  
  139. [System.Serializable]
  140. public struct ConversationPiece
  141. {
  142.     public string convo;
  143.     public Transform target, lookTarget;
  144.     public UnityEvent sentenceEvent;
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement