game(ui,scripts): donut interactive prompt demo

This commit is contained in:
sippy-thinks 2024-08-11 15:02:17 +08:00
parent 43399cf933
commit 663b9114ba
7 changed files with 119 additions and 13 deletions

View file

@ -29,7 +29,7 @@ public class CommonMenu : MonoBehaviour
/// <summary> /// <summary>
/// the visual element object for the menu /// the visual element object for the menu
/// </summary> /// </summary>
public VisualElement UI; protected VisualElement UI;
/// <summary> /// <summary>
/// checks if The Menu (2022) was set up correctly /// checks if The Menu (2022) was set up correctly

View file

@ -1,5 +1,5 @@
/* /*
* author: mark joshwel * author: mark joshwel, sai puay
* date: 11/8/2024 * date: 11/8/2024
* description: game manager singleton for a single source of truth state management * description: game manager singleton for a single source of truth state management
*/ */
@ -7,6 +7,8 @@
using System; using System;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using Cursor = UnityEngine.Cursor;
/// <summary> /// <summary>
/// singleton class for managing the game state as a single source of truth /// singleton class for managing the game state as a single source of truth
@ -33,21 +35,45 @@ public enum DisplayState
public static GameManager Instance; public static GameManager Instance;
/// <summary> /// <summary>
/// property to store the heads-up display game object /// game object for the interaction prompt
/// </summary> /// </summary>
[SerializeField] private GameObject headsUpDisplay; [SerializeField] private GameObject guiInteractionPromptObject;
/// <summary>
/// game object for the heads-up display
/// </summary>
[SerializeField] private GameObject guiHudObject;
/// <summary> /// <summary>
/// the current state of the game /// the current state of the game
/// </summary> /// </summary>
private DisplayState _state = DisplayState.UnassociatedState; private DisplayState _state = DisplayState.UnassociatedState;
/// <summary>
/// the visual element object for game ui (hud/prompts/tooltips)
/// </summary>
private VisualElement _ui;
/// <summary>
/// hud ui label for an interaction prompt/tooltip
/// </summary>
private Label _uiLabelInteractionPrompt;
/// <summary>
/// hud ui label for the player's score out of a thousand
/// </summary>
private Label _uiLabelScore;
/// <summary>
/// hud ui label for the speed-run stopwatch
/// </summary>
private Label _uiLabelStopwatch;
/// <summary> /// <summary>
/// property to check if the game is paused based on the current <c>DisplayState</c> /// property to check if the game is paused based on the current <c>DisplayState</c>
/// </summary> /// </summary>
public bool Paused => _state != DisplayState.Game; public bool Paused => _state != DisplayState.Game;
/// <summary> /// <summary>
/// function to set doesn't destroy on load and checks for multiple instances /// function to set doesn't destroy on load and checks for multiple instances
/// </summary> /// </summary>
@ -73,9 +99,11 @@ private void Awake()
Destroy(gameObject); Destroy(gameObject);
} }
// check if the heads-up display is set if (guiInteractionPromptObject == null)
if (headsUpDisplay == null) throw new NullReferenceException("GameManager: guiInteractionPromptObject not set");
throw new NullReferenceException("GameManager: heads-up display is not set in game manager properties");
if (guiHudObject == null)
throw new NullReferenceException("GameManager: guiHudObject not set");
} }
/// <summary> /// <summary>
@ -90,6 +118,28 @@ private void Start()
PauseGameHelper(DisplayState.ScreenMainMenu); PauseGameHelper(DisplayState.ScreenMainMenu);
} }
/// <summary>
/// game run speed run stopwatch logic
/// </summary>
// TODO: implement this (speed-run stopwatch)
private void Update()
{
}
/// <summary>
/// initialise ui elements used by the game[ manager]
/// </summary>
/// >
private void OnEnable()
{
_ui = guiInteractionPromptObject.GetComponent<UIDocument>()?.rootVisualElement;
_uiLabelInteractionPrompt = _ui.Q<Label>("InteractionPromptLabel");
_ui = guiHudObject.GetComponent<UIDocument>()?.rootVisualElement;
_uiLabelStopwatch = _ui.Q<Label>("CurrentStopwatchLabel");
_uiLabelScore = _ui.Q<Label>("CurrentScoreLabel");
}
/// <summary> /// <summary>
/// helper function to hide any menu that is currently showing /// helper function to hide any menu that is currently showing
/// </summary> /// </summary>
@ -99,6 +149,9 @@ private void HideMenuHelper()
foreach (var menu in GameObject.FindGameObjectsWithTag("Interfaces")) foreach (var menu in GameObject.FindGameObjectsWithTag("Interfaces"))
foreach (Transform menuChild in menu.transform) foreach (Transform menuChild in menu.transform)
{ {
// skip if it is 'GameInterface' object
if (menuChild.gameObject.CompareTag("GameInterface")) continue;
// disable the menu if it's currently active // disable the menu if it's currently active
if (!menuChild.gameObject.activeSelf) continue; if (!menuChild.gameObject.activeSelf) continue;
@ -122,7 +175,6 @@ private void PauseGameHelper(DisplayState incomingState)
Time.timeScale = 0f; Time.timeScale = 0f;
Cursor.lockState = CursorLockMode.None; Cursor.lockState = CursorLockMode.None;
Cursor.visible = true; Cursor.visible = true;
} }
// or if the incoming state is the main menu, we should probably free the cursor // or if the incoming state is the main menu, we should probably free the cursor
else if (incomingState == DisplayState.ScreenMainMenu) else if (incomingState == DisplayState.ScreenMainMenu)
@ -309,6 +361,24 @@ public void NewGame()
// set to game state // set to game state
SetDisplayState(DisplayState.Game); SetDisplayState(DisplayState.Game);
// TODO // TODO: reset game state
}
/// <summary>
/// function to set an interaction prompt/tooltip for the player
/// </summary>
/// <param name="prompt">string to show to the player</param>
public void SetInteractionPrompt(string prompt)
{
_uiLabelInteractionPrompt.text = prompt;
_uiLabelInteractionPrompt.visible = true;
}
/// <summary>
/// function to stop showing an interaction prompt/tooltip for the player
/// </summary>
public void ClearInteractionPrompt()
{
_uiLabelInteractionPrompt.visible = false;
} }
} }

View file

@ -0,0 +1,24 @@
/*
* author: sai puay
* date: 11/8/2024
* description: interaction prompt demo with a probuilder donut
*/
using System;
using UnityEngine;
/// <summary>
/// demo donut interactable class
/// </summary>
public class PlaygroundDonut : CommonInteractable
{
private void OnTriggerEnter(Collider other)
{
GameManager.Instance.SetInteractionPrompt("This is a Donut");
}
private void OnTriggerExit(Collider other)
{
GameManager.Instance.ClearInteractionPrompt();
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2b437e609ff10ac4d91353be0c7ff2a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -2,8 +2,8 @@
<Style src="project://database/Assets/UI%20Toolkit/Interfaces/Common.uss?fileID=7433441132597879392&amp;guid=7e22eeee55627cf478c5e125a8197039&amp;type=3#Common" /> <Style src="project://database/Assets/UI%20Toolkit/Interfaces/Common.uss?fileID=7433441132597879392&amp;guid=7e22eeee55627cf478c5e125a8197039&amp;type=3#Common" />
<ui:VisualElement name="Root" style="flex-grow: 1; justify-content: space-around; align-items: center;"> <ui:VisualElement name="Root" style="flex-grow: 1; justify-content: space-around; align-items: center;">
<ui:VisualElement name="VertHolder" style="flex-grow: 1; justify-content: flex-start; align-items: center; align-self: auto; margin-top: 2%;"> <ui:VisualElement name="VertHolder" style="flex-grow: 1; justify-content: flex-start; align-items: center; align-self: auto; margin-top: 2%;">
<ui:Label tabindex="-1" parse-escape-sequences="true" display-tooltip-when-elided="true" text="00:53.47" name="Stopwatch" style="color: rgb(255, 255, 255); -unity-text-align: upper-center; white-space: normal; -unity-font: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold.otf?fileID=12800000&amp;guid=505dfbc18d2d4604db47ff55755f9dc8&amp;type=3#Fervojo-Bold&quot;); -unity-font-definition: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold%20SDF.asset?fileID=11400000&amp;guid=1abb65c0bf74b1649863fc75b2b83ac1&amp;type=2#Fervojo-Bold SDF&quot;); font-size: 64px; margin-bottom: 0; padding-bottom: 0;" /> <ui:Label tabindex="-1" parse-escape-sequences="true" display-tooltip-when-elided="true" text="00:53.47" name="CurrentStopwatchLabel" style="color: rgb(255, 255, 255); -unity-text-align: upper-center; white-space: normal; -unity-font: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold.otf?fileID=12800000&amp;guid=505dfbc18d2d4604db47ff55755f9dc8&amp;type=3#Fervojo-Bold&quot;); -unity-font-definition: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold%20SDF.asset?fileID=11400000&amp;guid=1abb65c0bf74b1649863fc75b2b83ac1&amp;type=2#Fervojo-Bold SDF&quot;); font-size: 64px; margin-bottom: 0; padding-bottom: 0;" />
<ui:Label tabindex="-1" parse-escape-sequences="true" display-tooltip-when-elided="true" text="1,000" name="Score" style="color: rgb(255, 255, 255); -unity-text-align: upper-center; white-space: normal; -unity-font: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold.otf?fileID=12800000&amp;guid=505dfbc18d2d4604db47ff55755f9dc8&amp;type=3#Fervojo-Bold&quot;); -unity-font-definition: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold%20SDF.asset?fileID=11400000&amp;guid=1abb65c0bf74b1649863fc75b2b83ac1&amp;type=2#Fervojo-Bold SDF&quot;); font-size: 32px; margin-top: 0; padding-top: 0;" /> <ui:Label tabindex="-1" parse-escape-sequences="true" display-tooltip-when-elided="true" text="1,000" name="CurrentScoreLabel" style="color: rgb(255, 255, 255); -unity-text-align: upper-center; white-space: normal; -unity-font: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold.otf?fileID=12800000&amp;guid=505dfbc18d2d4604db47ff55755f9dc8&amp;type=3#Fervojo-Bold&quot;); -unity-font-definition: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Bold%20SDF.asset?fileID=11400000&amp;guid=1abb65c0bf74b1649863fc75b2b83ac1&amp;type=2#Fervojo-Bold SDF&quot;); font-size: 32px; margin-top: 0; padding-top: 0;" />
</ui:VisualElement> </ui:VisualElement>
</ui:VisualElement> </ui:VisualElement>
</ui:UXML> </ui:UXML>

View file

@ -1,7 +1,7 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False"> <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
<ui:VisualElement name="Root" style="flex-grow: 1; justify-content: space-around; align-items: center;"> <ui:VisualElement name="Root" style="flex-grow: 1; justify-content: space-around; align-items: center;">
<ui:VisualElement name="VertHolder" style="flex-grow: 1; justify-content: center; margin-top: 30%;"> <ui:VisualElement name="VertHolder" style="flex-grow: 1; justify-content: center; margin-top: 30%;">
<ui:Label tabindex="-1" parse-escape-sequences="true" display-tooltip-when-elided="true" text="Press E to Interact" name="PromptLabel" style="background-color: rgba(0, 0, 0, 0.33); color: rgb(255, 255, 255); -unity-text-align: upper-center; padding-right: 8px; padding-left: 8px; padding-top: 8px; padding-bottom: 8px; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; white-space: normal; -unity-font: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Medium.otf?fileID=12800000&amp;guid=9439a6fdc5392c6479cda7dd72a8f3f7&amp;type=3#Fervojo-Medium&quot;); -unity-font-definition: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Medium%20SDF.asset?fileID=11400000&amp;guid=45943718062c86e448361bd7f6506932&amp;type=2#Fervojo-Medium SDF&quot;); font-size: 28px;" /> <ui:Label tabindex="-1" parse-escape-sequences="true" display-tooltip-when-elided="true" text="Press E to Interact" name="InteractionPromptLabel" style="background-color: rgba(0, 0, 0, 0.33); color: rgb(255, 255, 255); -unity-text-align: upper-center; padding-right: 8px; padding-left: 8px; padding-top: 8px; padding-bottom: 8px; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; white-space: normal; -unity-font: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Medium.otf?fileID=12800000&amp;guid=9439a6fdc5392c6479cda7dd72a8f3f7&amp;type=3#Fervojo-Medium&quot;); -unity-font-definition: url(&quot;project://database/Assets/UI%20Toolkit/RLFonts/Fervojo/Fervojo-Medium%20SDF.asset?fileID=11400000&amp;guid=45943718062c86e448361bd7f6506932&amp;type=2#Fervojo-Medium SDF&quot;); font-size: 28px;" />
</ui:VisualElement> </ui:VisualElement>
</ui:VisualElement> </ui:VisualElement>
</ui:UXML> </ui:UXML>

View file

@ -8,6 +8,7 @@ TagManager:
- Interactable - Interactable
- AIs - AIs
- Interfaces - Interfaces
- GameInterface
layers: layers:
- Default - Default
- TransparentFX - TransparentFX