GameManager - robblofield/TomeboundDocs GitHub Wiki
GameManager
Author: Youngju Yun
Last Updated: 01/01/2024
Overview
This script holds data and several methods of generating debug messages, management of input devices, and general settings of the game.
Dependencies
- Player.cs
- GridBasedMovement1.cs
- Action_EnterGate.cs
- Action_Grab.cs
- Gate.cs
- Tome.cs
- CollisionChecker.cs
Contents
- Breakdown of code
- Variables
- Awake()
- Update()
- SwapPlayer()
- SetErrorMessage()
- autoDisable()
- CheckForControllers()
- Full Code Reference
Breakdown of Code
Variables
public GameObject wizX;
public GameObject wizY;
public GameObject controllingObject;
public GameObject wizX, wizY: They are the reference to the wizards which are controlled by a player wizX refers to the Green Wizard and the wizY is for the Blue.
public GameObject controllingObject: This variable holds either of the wizards that are currently in control of the player. It is used in the method SwapPlayer().
//Debug Mode Texts
public GameObject debugMessages;
int currentDebugPage = 0;
public TextMeshProUGUI errorMessage;
public TextMeshProUGUI wizYStatus;
public TextMeshProUGUI wizXStatus;
public GameObject debugMessages: This is a reference to the parent that holds all the debug messages displayed on the left-top side of the screen.
int currentDebugPage: This integer represents the current page of the debug messages displayed.
TextMeshProUGUI errorMessage: This is a reference to the Text Property of the red error message which is shown at the bottom of the display.
TextMeshProUGUI wizYStatus, wizXStatus: This is a reference to the Text Property about the status of wizY and wizX.
//Gate Settings
public bool invertTeleportation;
public bool isTeleporting;
private GameObject book;
bool invertTeleportation: The boolean that inverts the position of wizards and the Tome when they entered the next floor via Gates.
bool isTeleporting: A boolean that is set true at the beginning of the Update() function in Gate.cs to override the player input and other automated functions during the Teleportation in between Gates. It's not used in the current build.
//controller connected
private bool connected = false;
public TextMeshProUGUI controllerName;
public GameObject controlsKeyboard;
public GameObject controlsGamepad;
bool connected: A boolean which is set to true when any type of Gamepad is connected.
TextMeshProUGUI controllerName This Text Property holds the name of input devices currently in use, either keyboard or gamepad.
public GameObject controlsKeyboard, controlsGamepad: They refer to the Button Mappings for Keyboard and Gamepad, which is a part of Debug Messages.
//Player Movement Indicator UI
public GameObject indicatorLR;
public GameObject indicatorLRTome;
public GameObject indicatorUD;
public GameObject indicatorUDTome;
public GameObject indicatorOMNI;
public GameObject indicatorOMNIX;
public GameObject indicatorOMNIY;
GameObject indicatorLR: They are references to the GameObject that indicates that the player can only move the character horizontally.
GameObject indicatorUD: They are references to the GameObject which indicates that the player can only move the character vertically.
GameObject indicatorOMNI: They are references to the GameObject which indicates that the player can move the character in any direction.
Awake()
From the first frame of the game, it starts Coroutine CheckForControllers() to look for any gamepads connected.
StartCoroutine(CheckForControllers());
Start()
void Start()
{
book = GameObject.FindWithTag("Book");
// wizYMovement = wizY.GetComponent<gridBasedMovement>();
// wizXMovement = wizX.GetComponent<gridBasedMovement>();
controllingObject = wizX;
//controllingPlayerText.text = "wizX";
//set all indicators to inactive
indicatorLR.SetActive(true);
indicatorUD.SetActive(false);
indicatorOMNI.SetActive(false);
SetErrorMessage("");
}
The variable book is set as the GameObject with the tag "Book".
Then, it sets wizX the first wizard to be controlled by the player.
Afterwards, Every indicator is deactivated, except for the indicators for wizX.
Finally, the error message is blanked out.
Update()
if(Input.GetKeyDown(KeyCode.F1)){
debugMessages.transform.GetChild(currentDebugPage).gameObject.SetActive(false);
currentDebugPage++;
if(currentDebugPage > 3){
currentDebugPage = 0;
}
debugMessages.transform.GetChild(currentDebugPage).gameObject.SetActive(true);
}
if (wizY.GetComponent<Action_Grab>().isGrabbingObject)
{
wizYStatus.text = "Wizard Blue:Holding";
if (wizY.GetComponent<GridBasedMovement1>().isMoving)
{
wizYStatus.text = "Wizard Blue:Moving and Holding";
}
}
else
{
if (wizY.GetComponent<GridBasedMovement1>().isMoving)
{
wizYStatus.text = "Wizard Blue:Moving";
}
else
{
wizYStatus.text = "Wizard Blue:Idle";
}
}
if (wizX.GetComponent<Action_Grab>().isGrabbingObject)
{
wizXStatus.text = "Wizard Green:Holding";
if (wizX.GetComponent<GridBasedMovement1>().isMoving)
{
wizXStatus.text = "Wizard Green:Moving and Holding";
}
}
else
{
if (wizX.GetComponent<GridBasedMovement1>().isMoving)
{
wizXStatus.text = "Wizard Green:Moving";
}
else
{
wizXStatus.text = "Wizard Green:Idle";
}
}
// Set indicator to OMNI when both are grabbing the book
if (wizX.GetComponent<Action_Grab>().isGrabbingObject && wizY.GetComponent<Action_Grab>().isGrabbingObject)
{
indicatorOMNI.SetActive(true);
indicatorLR.SetActive(false);
indicatorUD.SetActive(false);
indicatorLRTome.SetActive(false);
indicatorUDTome.SetActive(false);
if (controllingObject == wizX)
{
indicatorOMNIX.SetActive(true);
}
else
{
indicatorOMNIY.SetActive(true);
}
}
else
{
indicatorOMNI.SetActive(false);
if(wizX.GetComponent<Action_Grab>().isGrabbingObject && controllingObject == wizX){
indicatorLRTome.SetActive(true);
}else if(wizY.GetComponent<Action_Grab>().isGrabbingObject && controllingObject == wizY){
indicatorUDTome.SetActive(true);
}else{
indicatorLRTome.SetActive(false);
indicatorUDTome.SetActive(false);
}
}
// Set indiactor to previous controller
if (!indicatorLR.activeInHierarchy && !indicatorUD.activeInHierarchy && !indicatorOMNI.activeInHierarchy)
{
if (controllingObject == wizX)
{
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorLR.SetActive(true);
indicatorOMNIX.SetActive(false);
}else{
indicatorOMNIX.SetActive(true);
indicatorLR.SetActive(false);
}
}
else
{
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorUD.SetActive(true);
indicatorOMNIY.SetActive(false);
}else{
indicatorOMNIY.SetActive(false);
indicatorUD.SetActive(true);
}
}
}
Update() Turn Over the Page
if(Input.GetKeyDown(KeyCode.F1)){
debugMessages.transform.GetChild(currentDebugPage).gameObject.SetActive(false);
currentDebugPage++;
if(currentDebugPage > 3){
currentDebugPage = 0;
}
debugMessages.transform.GetChild(currentDebugPage).gameObject.SetActive(true);
}
The Debug Message information changes over the page by pressing F1.
The page count increments by 1, and it goes back to 0 if the page count is more than 3.
It activates the child of debugMessages by indexing the current page count and deactivating the rest of the children.
Update() Player Status Debug Message
if (wizY.GetComponent<Action_Grab>().isGrabbingObject)
{
wizYStatus.text = "Wizard Blue:Holding";
if (wizY.GetComponent<GridBasedMovement1>().isMoving)
{
wizYStatus.text = "Wizard Blue:Moving and Holding";
}
}
else
{
if (wizY.GetComponent<GridBasedMovement1>().isMoving)
{
wizYStatus.text = "Wizard Blue:Moving";
}
else
{
wizYStatus.text = "Wizard Blue:Idle";
}
}
if (wizX.GetComponent<Action_Grab>().isGrabbingObject)
{
wizXStatus.text = "Wizard Green:Holding";
if (wizX.GetComponent<GridBasedMovement1>().isMoving)
{
wizXStatus.text = "Wizard Green:Moving and Holding";
}
}
else
{
if (wizX.GetComponent<GridBasedMovement1>().isMoving)
{
wizXStatus.text = "Wizard Green:Moving";
}
else
{
wizXStatus.text = "Wizard Green:Idle";
}
}
This part of the Update() updates the status text of wizX and wizY according to data from a component of GridBasedMovement1 and Action_Grab from both player characters, wizX and wizY.
The status of wizX/wizY is varied into 4 types; Idle, Moving, Holding, Moving and Holding.
Update() Set Indicator to OMNI
if (wizX.GetComponent<Action_Grab>().isGrabbingObject && wizY.GetComponent<Action_Grab>().isGrabbingObject)
{
indicatorOMNI.SetActive(true);
indicatorLR.SetActive(false);
indicatorUD.SetActive(false);
indicatorLRTome.SetActive(false);
indicatorUDTome.SetActive(false);
if (controllingObject == wizX)
{
indicatorOMNIX.SetActive(true);
}
else
{
indicatorOMNIY.SetActive(true);
}
}
else
{
indicatorOMNI.SetActive(false);
if(wizX.GetComponent<Action_Grab>().isGrabbingObject && controllingObject == wizX){
indicatorLRTome.SetActive(true);
}else if(wizY.GetComponent<Action_Grab>().isGrabbingObject && controllingObject == wizY){
indicatorUDTome.SetActive(true);
}else{
indicatorLRTome.SetActive(false);
indicatorUDTome.SetActive(false);
}
}
Whilst the Tome is grabbed by both of the wizards, the indicators of the controllingObject and the Tome change to OMNI since they can move in any direction.
It only shows the indicator of either of the wizards to clarify which one is in control now.
If the Tome is held by either of the wizards, the indicator of the Tome will change to either indicatorLR or indicatorUD depending on the wizard who is holding it.
Update() Activation of the Previous Indicator
The wizards can move freely if they are not on the floor same as the Tome. If the movement of the wizard is locked, the indicator sign is set to LR/UD, but if it's not, the indicator is set to OMNI.
activeInHierachy returns true if the object is active and also its parents.
SwapPlayer()
It switches the wizard in control, wizX or wizY.
public void SwapPlayer()
{
if (controllingObject == wizX)
{
controllingObject = wizY;
//controllingPlayerText.text = "wizY";
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorUD.SetActive(true);
indicatorOMNIY.SetActive(false);
}else{
indicatorOMNIY.SetActive(true);
indicatorUD.SetActive(false);
}
indicatorLR.SetActive(false);
indicatorOMNI.SetActive(false);
indicatorOMNIX.SetActive(false);
}
else
{
controllingObject = wizX;
//controllingPlayerText.text = "wizX";
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorLR.SetActive(true);
indicatorOMNIX.SetActive(false);
}else{
indicatorOMNIX.SetActive(true);
indicatorLR.SetActive(false);
}
indicatorUD.SetActive(false);
indicatorOMNI.SetActive(false);
indicatorOMNIY.SetActive(false);
}
Debug.Log(controllingObject);
}
When the character is swapped, the visible indicator is also swapped.
If the wizard was holding the Tome, the indicator for the Tome is also shown.
SetErrorMessage()
Generates Error Message using the string parameter sentence for 3 seconds.
public void SetErrorMessage(string sentence)
{
StopCoroutine(autoDisable(3f));
errorMessage.text = sentence;
StartCoroutine(autoDisable(3f));
}
autoDisable()
A subroutine that deactivates errorMessage gameObject automatically at a specified time equal to the float parameter duration.
IEnumerator autoDisable(float duration)
{
errorMessage.gameObject.SetActive(true);
yield return new WaitForSeconds(duration);
errorMessage.gameObject.SetActive(false);
}
CheckForControllers()
A subroutine to look for any gamepad or foreign input devices for every second.
IEnumerator CheckForControllers(){
while (true) {
var allGamepads = Gamepad.all;
if (!connected && allGamepads.Count > 0) {
connected = true;
Debug.Log("Connected");
controllerName.text = "Input Source: Gamepad";
Debug.Log(allGamepads[0]);
controlsGamepad.SetActive(true);
controlsKeyboard.SetActive(false);
} else if (connected && allGamepads.Count == 0) {
connected = false;
Debug.Log("Disconnected");
controllerName.text = "Input Source: Keyboard";
controlsGamepad.SetActive(false);
controlsKeyboard.SetActive(true);
}
yield return new WaitForSeconds(1f);
}
}
CheckForControllers() Initialization
while (true) {
var allGamepads = Gamepad.all;
It uses a while loop to look for all connected gamepads.
CheckForControllers() Connected
if (!connected && allGamepads.Count > 0) {
connected = true;
Debug.Log("Connected");
controllerName.text = "Input Source: Gamepad";
Debug.Log(allGamepads[0]);
controlsGamepad.SetActive(true);
controlsKeyboard.SetActive(false);
If it found more than 1 gamepad, the boolean connected is set to true and the debug text for the Input Source and the Button Mapping is set to "Input Source: Gamepad".
CheckForControllers() Disconnected
else if (connected && allGamepads.Count == 0) {
connected = false;
Debug.Log("Disconnected");
controllerName.text = "Input Source: Keyboard";
controlsGamepad.SetActive(false);
controlsKeyboard.SetActive(true);
}
If it is no longer able to find any connected gamepads, connected is set to false and Input Source and the Button Mapping are set back to Keyboard.
CheckForControllers() Interval
yield return new WaitForSeconds(1f);
}
To avoid infinite loops, the subroutine will take a 1-second interval at the end of the loop.
Full Code Reference
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.InputSystem;
public class GameManager : MonoBehaviour
{
public GameObject wizX;
public GameObject wizY;
public GameObject controllingObject;
//Debug Mode Texts
public GameObject debugMessages;
int currentDebugPage = 0;
public TextMeshProUGUI controllingPlayerText;
public TextMeshProUGUI errorMessage;
public TextMeshProUGUI wizYStatus;
public TextMeshProUGUI wizXStatus;
//Gate Settings
public bool invertTeleportation;
public bool isTeleporting;
private GameObject book;
//controller connected
private bool connected = false;
public TextMeshProUGUI controllerName;
public GameObject controlsKeyboard;
public GameObject controlsGamepad;
//Player Movement Indicator UI
public GameObject indicatorLR;
public GameObject indicatorLRTome;
public GameObject indicatorUD;
public GameObject indicatorUDTome;
public GameObject indicatorOMNI;
public GameObject indicatorOMNIX;
public GameObject indicatorOMNIY;
void Awake(){
StartCoroutine(CheckForControllers());
}
void Start()
{
book = GameObject.FindWithTag("Book");
// wizYMovement = wizY.GetComponent<gridBasedMovement>();
// wizXMovement = wizX.GetComponent<gridBasedMovement>();
controllingObject = wizX;
//controllingPlayerText.text = "wizX";
//set all indicators to inactive
indicatorLR.SetActive(true);
indicatorUD.SetActive(false);
indicatorOMNI.SetActive(false);
SetErrorMessage("");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.F1)){
debugMessages.transform.GetChild(currentDebugPage).gameObject.SetActive(false);
currentDebugPage++;
if(currentDebugPage > 3){
currentDebugPage = 0;
}
debugMessages.transform.GetChild(currentDebugPage).gameObject.SetActive(true);
}
if (wizY.GetComponent<Action_Grab>().isGrabbingObject)
{
wizYStatus.text = "Wizard Blue:Holding";
if (wizY.GetComponent<GridBasedMovement1>().isMoving)
{
wizYStatus.text = "Wizard Blue:Moving and Holding";
}
}
else
{
if (wizY.GetComponent<GridBasedMovement1>().isMoving)
{
wizYStatus.text = "Wizard Blue:Moving";
}
else
{
wizYStatus.text = "Wizard Blue:Idle";
}
}
if (wizX.GetComponent<Action_Grab>().isGrabbingObject)
{
wizXStatus.text = "Wizard Green:Holding";
if (wizX.GetComponent<GridBasedMovement1>().isMoving)
{
wizXStatus.text = "Wizard Green:Moving and Holding";
}
}
else
{
if (wizX.GetComponent<GridBasedMovement1>().isMoving)
{
wizXStatus.text = "Wizard Green:Moving";
}
else
{
wizXStatus.text = "Wizard Green:Idle";
}
}
// Set indicator to OMNI when both are grabbing the book
if (wizX.GetComponent<Action_Grab>().isGrabbingObject && wizY.GetComponent<Action_Grab>().isGrabbingObject)
{
indicatorOMNI.SetActive(true);
indicatorLR.SetActive(false);
indicatorUD.SetActive(false);
indicatorLRTome.SetActive(false);
indicatorUDTome.SetActive(false);
if (controllingObject == wizX)
{
indicatorOMNIX.SetActive(true);
}
else
{
indicatorOMNIY.SetActive(true);
}
}
else
{
indicatorOMNI.SetActive(false);
if(wizX.GetComponent<Action_Grab>().isGrabbingObject && controllingObject == wizX){
indicatorLRTome.SetActive(true);
}else if(wizY.GetComponent<Action_Grab>().isGrabbingObject && controllingObject == wizY){
indicatorUDTome.SetActive(true);
}else{
indicatorLRTome.SetActive(false);
indicatorUDTome.SetActive(false);
}
}
// Set indiactor to previous controller
if (!indicatorLR.activeInHierarchy && !indicatorUD.activeInHierarchy && !indicatorOMNI.activeInHierarchy)
{
if (controllingObject == wizX)
{
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorLR.SetActive(true);
indicatorOMNIX.SetActive(false);
}else{
indicatorOMNIX.SetActive(true);
indicatorLR.SetActive(false);
}
}
else
{
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorUD.SetActive(true);
indicatorOMNIY.SetActive(false);
}else{
indicatorOMNIY.SetActive(false);
indicatorUD.SetActive(true);
}
}
}
}
public void SwapPlayer()
{
if (controllingObject == wizX)
{
controllingObject = wizY;
//controllingPlayerText.text = "wizY";
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorUD.SetActive(true);
indicatorOMNIY.SetActive(false);
}else{
indicatorOMNIY.SetActive(true);
indicatorUD.SetActive(false);
}
indicatorLR.SetActive(false);
indicatorOMNI.SetActive(false);
indicatorOMNIX.SetActive(false);
}
else
{
controllingObject = wizX;
//controllingPlayerText.text = "wizX";
if(wizX.GetComponent<GridBasedMovement1>().movementLocked){
indicatorLR.SetActive(true);
indicatorOMNIX.SetActive(false);
}else{
indicatorOMNIX.SetActive(true);
indicatorLR.SetActive(false);
}
indicatorUD.SetActive(false);
indicatorOMNI.SetActive(false);
indicatorOMNIY.SetActive(false);
}
Debug.Log(controllingObject);
}
public void SetErrorMessage(string sentence)
{
StopCoroutine(autoDisable(3f));
errorMessage.text = sentence;
StartCoroutine(autoDisable(3f));
}
IEnumerator autoDisable(float duration)
{
errorMessage.gameObject.SetActive(true);
yield return new WaitForSeconds(duration);
errorMessage.gameObject.SetActive(false);
}
IEnumerator CheckForControllers(){
while (true) {
var allGamepads = Gamepad.all;
if (!connected && allGamepads.Count > 0) {
connected = true;
Debug.Log("Connected");
controllerName.text = "Input Source: Gamepad";
Debug.Log(allGamepads[0]);
controlsGamepad.SetActive(true);
controlsKeyboard.SetActive(false);
} else if (connected && allGamepads.Count == 0) {
connected = false;
Debug.Log("Disconnected");
controllerName.text = "Input Source: Keyboard";
controlsGamepad.SetActive(false);
controlsKeyboard.SetActive(true);
}
yield return new WaitForSeconds(1f);
}
}
}