2024-01-04 15:27:48 +01:00

547 lines
21 KiB
C#

using Assets.Scripts.Classes;
using Assets.Scripts.Player;
using Assets.Scripts.Races;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Localization.Settings;
using UnityEngine.UI;
using Assets.Scripts.InteractableObjects;
namespace Assets.Scripts
{
public class UIHandler : MonoBehaviour
{
public GameObject compass;
public GameObject information;
public GameObject fight;
public GameObject message;
public GameObject deathscreen;
public GameObject options;
public GameObject pauseMenu;
public GameObject playerHUD;
public GameObject questlog;
public GameObject introduction;
public GameObject tutorial;
public GameObject inventory;
public GameObject waterLayer;
public UIState state;
// Start is called before the first frame update
void Start()
{
FileHandler.loadOptions(true);
}
// Update is called once per frame
void Update()
{
if (state == UIState.GAME || state == UIState.FIGHT)
{
if (GameObject.Find("Player").GetComponent<PlayerGameObject>().getPlayerStat("Killcount").getAmount() == -1)
{
GameObject.Find("Player").GetComponent<PlayerGameObject>().getPlayerStat("Killcount").changeAmount(1);
}
updatePlayerHUD();
switchWaterLayer();
updateCoordinates();
if (EventSystem.current.currentSelectedGameObject != null)
{
EventSystem.current.SetSelectedGameObject(null);
}
}
}
private void updateCoordinates()
{
GameObject coordinates = GameObject.Find("txtCoordinates");
Vector3 position = GameObject.Find("Player").transform.position;
string tiletype = GameObject.Find("WorldGenerator").GetComponent<WorldGenerator>().getCurrentTile().GetComponent<Tile>().getTileType().ToString();
if (tiletype != null)
{
tiletype = tiletype.Replace("Tile", "");
}
coordinates.GetComponent<Text>().text = TextHandler.getText(tiletype.ToLower()) + "(" + (int)position.x + "/" + (int)position.y + "/" + (int)position.z + ")";
}
private void switchWaterLayer()
{
if (GameObject.Find("Player").transform.position.y < -1)
{
waterLayer.transform.localScale = new Vector3(1, 1, 1);
}
else
{
waterLayer.transform.localScale = new Vector3(0, 0, 0);
}
}
private void updatePlayerHUD()
{
updateHUD(GameObject.Find("Player").GetComponent<PlayerGameObject>());
}
public bool canPlayerMove()
{
if (state == UIState.GAME || state == UIState.CHARACTER || state == UIState.QUEST || state == UIState.INVENTORY)
{
return true;
}
return false;
}
public bool canPlayerRotate()
{
if (state == UIState.GAME)
{
return true;
}
return false;
}
public bool isPlayerInFight()
{
return state == UIState.FIGHT;
}
public void startGame()
{
introduction.transform.localScale = new Vector3(0, 0, 0);
tutorial.transform.localScale = new Vector3(1, 1, 1);
showHUD();
state = UIState.TUTORIAL;
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnCloseTutorial"));
}
public void openOptions()
{
FileHandler.loadOptionDisplay();
hideOtherElements(options);
state = UIState.PAUSEOPTIONS;
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnAudio"));
showOptionView("audio");
}
public void closeOptions()
{
state = UIState.PAUSE;
openPauseMenu();
}
public void switchInventory()
{
if (state == UIState.INVENTORY)
{
closeInventory();
}
else
{
openInventory();
}
}
public void openInventory()
{
hideOtherElements(inventory);
state = UIState.INVENTORY;
EventSystem.current.SetSelectedGameObject(GameObject.Find("bagOne"));
}
public void closeInventory()
{
inventory.transform.localScale = new Vector3(0, 0, 0);
GameObject.Find("pnlInventoryActions").transform.localScale = new Vector3(0, 0, 0);
showHUD();
state = UIState.GAME;
}
public void openFight()
{
GameObject.Find("txtRounds").GetComponent<Text>().text = "-1";
hideOtherElements(fight);
playerHUD.transform.localScale = new Vector3(1, 1, 1);
state = UIState.FIGHT;
}
public void closeFight()
{
fight.transform.localScale = new Vector3(0, 0, 0);
showHUD();
state = UIState.GAME;
}
public void showHUD()
{
playerHUD.transform.localScale = new Vector3(1, 1, 1);
compass.transform.localScale = new Vector3(1, 1, 1);
}
public void openMainMenu()
{
GameObject.Find("SceneHandlerLoaded").GetComponent<SceneHandler>().openMenuScene();
}
public void switchPauseMenu()
{
if (state == UIState.GAME || state == UIState.CHARACTER || state == UIState.PAUSE || state == UIState.QUEST || state == UIState.INVENTORY || state == UIState.INTRODUCTION)
{
if (state == UIState.PAUSE)
{
closePauseMenu();
}
else
{
if (state == UIState.GAME)
{
openPauseMenu();
}
else
{
hideOtherElements(null);
showHUD();
state = UIState.GAME;
}
}
}
}
public void switchQuestLog()
{
if (state == UIState.QUEST)
{
closeQuestLog();
}
else
{
openQuestLog();
}
}
public void openQuestLog()
{
questlog.GetComponent<QuestLog>().showQuests();
state = UIState.QUEST;
hideOtherElements(questlog);
EventSystem.current.SetSelectedGameObject(GameObject.Find("scrollQuestlog"));
}
public void closeQuestLog()
{
questlog.transform.localScale = new Vector3(0, 0, 0);
state = UIState.GAME;
showHUD();
}
public string saveVideoSettings()
{
GameObject resolution = GameObject.Find("dropResolution");
GameObject mode = GameObject.Find("dropMode");
string result = "";
switch (resolution.GetComponent<Dropdown>().value)
{
case 0:
Screen.SetResolution(800, 600, Screen.fullScreenMode);
break;
case 1:
Screen.SetResolution(1280, 800, Screen.fullScreenMode);
break;
case 2:
Screen.SetResolution(1920, 1080, Screen.fullScreenMode);
break;
}
switch (mode.GetComponent<Dropdown>().value)
{
case 0:
if (Screen.fullScreenMode != FullScreenMode.Windowed)
{
Screen.fullScreenMode = FullScreenMode.Windowed;
}
break;
case 1:
if (Screen.fullScreenMode != FullScreenMode.ExclusiveFullScreen)
{
Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
}
break;
case 2:
if (Screen.fullScreenMode != FullScreenMode.FullScreenWindow)
{
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
}
break;
}
result = result + "Resolution:" + resolution.GetComponent<Dropdown>().value + "\r\n";
result = result + "Mode:" + mode.GetComponent<Dropdown>().value;
return result;
}
public string saveLanguage()
{
GameObject language = GameObject.Find("dropLanguage");
string result = "";
switch (language.GetComponent<Dropdown>().value)
{
case 0:
result = "de";
break;
case 1:
result = "en";
break;
}
result = "Language:" + result;
return result;
}
public void openPauseMenu()
{
hideOtherElements(pauseMenu);
state = UIState.PAUSE;
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnContinue"));
}
public void closePauseMenu()
{
pauseMenu.transform.localScale = new Vector3(0, 0, 0);
showHUD();
state = UIState.GAME;
}
public void showDeathScreen()
{
state = UIState.DEATH;
PlayerObject player = GameObject.Find("Player").GetComponent<PlayerGameObject>().getPlayer();
GameObject statText = GameObject.Find("txtDeathStats");
string text = statText.GetComponent<Text>().text;
text = text.Replace("NAME", player.getPlayerName());
text = text.Replace("RACE", player.getRace().racename);
text = text.Replace("CLASS", player.getClass().classname);
text = text.Replace("LEVEL", player.getStat("Level").getAmount().ToString());
text = text.Replace("KILLS", player.getStat("Killcount").getAmount().ToString());
text = text.Replace("HEALTH", player.getStat("MaxHealth").getAmount().ToString());
text = text.Replace("SECONDARY", player.getStat("MaxSecondary").getAmount().ToString());
text = text.Replace("INT", player.getStat("Intelligence").getAmount().ToString());
text = text.Replace("STR", player.getStat("Strength").getAmount().ToString());
text = text.Replace("DEX", player.getStat("Dexterity").getAmount().ToString());
text = text.Replace("TREES", player.getStat("TreeCount").getAmount().ToString());
text = text.Replace("ORES", player.getStat("OreCount").getAmount().ToString());
text = text.Replace("DIFFICULTY", player.getDifficulty() == 0 ? "Easy" : player.getDifficulty() == 1 ? "Normal" : "Hard");
statText.GetComponent<Text>().text = text;
hideOtherElements(deathscreen);
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnMenu"));
}
public void hideOtherElements(GameObject obj)
{
for (int i = 0; i < GameObject.Find("Canvas").transform.childCount; i++)
{
if (!GameObject.Find("Canvas").transform.GetChild(i).gameObject.Equals(obj))
{
GameObject.Find("Canvas").transform.GetChild(i).localScale = new Vector3(0, 0, 0);
}
}
if (obj != null)
{
obj.transform.localScale = new Vector3(1, 1, 1);
}
if (obj != fight)
{
showHUD();
}
}
public void displayInformation(string information)
{
GameObject.Find("txtObjectName").GetComponent<Text>().text = information;
this.information.transform.localScale = new Vector3(1, 1, 1);
}
public void hideInformation()
{
information.transform.localScale = new Vector3(0, 0, 0);
}
public void showMessage(string message)
{
this.message.GetComponent<LogWriter>().addMessage(message);
}
public void updateFightInterface(GameObject enemy, GameObject player)
{
updateFightInterfaceEnemy(enemy);
updateFightInterfaceActions(player.GetComponent<PlayerGameObject>());
GameObject.Find("txtRounds").GetComponent<Text>().text = (int.Parse(GameObject.Find("txtRounds").GetComponent<Text>().text) + 1).ToString();
}
private void updateFightInterfaceActions(PlayerGameObject player)
{
GameObject skillOne = GameObject.Find("btnActionTwo");
GameObject skillTwo = GameObject.Find("btnActionThree");
GameObject skillThree = GameObject.Find("btnActionFour");
player.displayAction(0, skillOne.transform.Find("imgAction").gameObject, skillOne.transform.Find("descAction").gameObject);
player.displayAction(1, skillTwo.transform.Find("imgAction").gameObject, skillTwo.transform.Find("descAction").gameObject);
player.displayAction(2, skillThree.transform.Find("imgAction").gameObject, skillThree.transform.Find("descAction").gameObject);
}
private void updateFightInterfaceEnemy(GameObject enemy)
{
// { health, maxHealth, secondary, maxSecondary, strength, dexterity, intelligence };
int[] enemyStats = enemy.GetComponent<Enemy>().getStats();
GameObject foreground = GameObject.Find("healthForegroundEnemy");
GameObject background = GameObject.Find("healthBackgroundEnemy");
GameObject text = GameObject.Find("healthTextEnemy");
updateBar(foreground, background, text, enemyStats[1], enemyStats[0]);
foreground = GameObject.Find("secondaryForegroundEnemy");
background = GameObject.Find("secondaryBackgroundEnemy");
text = GameObject.Find("secondaryTextEnemy");
updateBar(foreground, background, text, enemyStats[3], enemyStats[2]);
}
public void adjustInformation(PlayerGameObject player)
{
Dictionary<string, int> equipment = inventory.GetComponent<Inventory>().getEquipmentBonus();
GameObject.Find("txtStrength").GetComponent<Text>().text = "STR: " + player.getPlayerStat("Strength").getAmount() + " (+" + equipment["STR"] + ")";
GameObject.Find("txtDexterity").GetComponent<Text>().text = "DEX: " + player.getPlayerStat("Dexterity").getAmount() + " (+" + equipment["DEX"] + ")";
GameObject.Find("txtIntelligence").GetComponent<Text>().text = "INT: " + player.getPlayerStat("Intelligence").getAmount() + " (+" + equipment["INT"] + ")";
GameObject.Find("txtHealth").GetComponent<Text>().text = TextHandler.getText("health") + " " + player.getPlayerStat("MaxHealth").getAmount() + " (+" + equipment["HP"] + ")";
GameObject.Find("txtSecondary").GetComponent<Text>().text = "Mana: " + player.getPlayerStat("MaxSecondary").getAmount() + " (+" + equipment["MP"] + ")";
player.updateName(GameObject.Find("txtName").GetComponent<Text>());
updatePoints(player.getPlayerStat("Points").getAmount());
}
private void updatePoints(int points)
{
GameObject strength = GameObject.Find("btnStrengthIncrease");
GameObject dexterity = GameObject.Find("btnDexterityIncrease");
GameObject intelligence = GameObject.Find("btnIntelligenceIncrease");
GameObject health = GameObject.Find("btnHealthIncrease");
GameObject secondary = GameObject.Find("btnSecondaryIncrease");
GameObject txtPoints = GameObject.Find("txtPoints");
txtPoints.GetComponent<Text>().text = TextHandler.getText("points") + " " + points;
if (points > 0)
{
strength.transform.localScale = new Vector3(1, 1, 1);
dexterity.transform.localScale = new Vector3(1, 1, 1);
intelligence.transform.localScale = new Vector3(1, 1, 1);
health.transform.localScale = new Vector3(1, 1, 1);
secondary.transform.localScale = new Vector3(1, 1, 1);
}
else
{
strength.transform.localScale = new Vector3(0, 0, 0);
dexterity.transform.localScale = new Vector3(0, 0, 0);
intelligence.transform.localScale = new Vector3(0, 0, 0);
health.transform.localScale = new Vector3(0, 0, 0);
secondary.transform.localScale = new Vector3(0, 0, 0);
}
}
public void updateHUD(PlayerGameObject player)
{
Dictionary<string, int> equipment = inventory.GetComponent<Inventory>().getEquipmentBonus();
GameObject information = GameObject.Find("txtPlayerInformationHUD");
player.updateNameHUD(information.GetComponent<Text>());
GameObject foreground = GameObject.Find("healthForegroundPlayer");
GameObject background = GameObject.Find("healthBackgroundPlayer");
GameObject text = GameObject.Find("healthTextPlayer");
updateBar(foreground, background, text, player.getPlayerStat("MaxHealth").getAmount() + equipment["HP"], player.getPlayerStat("Health").getAmount());
foreground = GameObject.Find("secondaryForegroundPlayer");
background = GameObject.Find("secondaryBackgroundPlayer");
text = GameObject.Find("secondaryTextPlayer");
updateBar(foreground, background, text, player.getPlayerStat("MaxSecondary").getAmount() + equipment["MP"], player.getPlayerStat("Secondary").getAmount());
}
public void updateBar(GameObject bar, GameObject barBackground, GameObject textField, int maxValue, int minValue)
{
string text = minValue + "/" + maxValue;
double percentage = 0;
if (maxValue > 0)
{
percentage = (1 / (double)maxValue) * minValue;
}
float change = (float)(barBackground.GetComponent<RectTransform>().rect.width - (barBackground.GetComponent<RectTransform>().rect.width * percentage));
if (textField != null)
{
textField.GetComponent<Text>().text = text;
}
bar.GetComponent<RectTransform>().offsetMax = new Vector2(-change, bar.GetComponent<RectTransform>().offsetMax.y);
}
public void openIntroduction()
{
GameObject.Find("AudioHandler").GetComponent<AudioHandler>().Start();
if (PlayerPrefs.GetInt("isLoad") == 0)
{
GameObject.Find("WorldGenerator").GetComponent<WorldGenerator>().resetGame(PlayerPrefs.GetInt("cityAmount"));
GameObject.Find("Player").GetComponent<PlayerGameObject>().generatePlayer();
hideOtherElements(introduction);
state = UIState.INTRODUCTION;
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnClose"));
}
else
{
FileHandler.loadGame(GameObject.Find("Player").GetComponent<PlayerGameObject>(), GameObject.Find("WorldGenerator").GetComponent<WorldGenerator>(), GameObject.Find("Inventory").GetComponent<Inventory>(), GameObject.Find("QuestLog").GetComponent<QuestLog>());
hideOtherElements(introduction);
introduction.transform.localScale = new Vector3(0, 0, 0);
tutorial.transform.localScale = new Vector3(0, 0, 0);
showHUD();
state = UIState.GAME;
}
updatePlayerHUD();
}
public void closeTutorial()
{
tutorial.transform.localScale = new Vector3(0, 0, 0);
state = UIState.GAME;
}
public void showOptionView(string key)
{
GameObject optionContent = GameObject.Find("pnlContent");
for (int i = 0; i < optionContent.transform.childCount; i++)
{
if (optionContent.transform.GetChild(i).name.ToLower().Contains(key))
{
optionContent.transform.GetChild(i).transform.localScale = new Vector3(1, 1, 1);
}
else
{
optionContent.transform.GetChild(i).transform.localScale = new Vector3(0, 0, 0);
}
}
}
public void switchLanguage()
{
GameObject language = GameObject.Find("dropLanguage");
switch (language.GetComponent<Dropdown>().value)
{
case 0:
LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.GetLocale("de");
break;
case 1:
LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.GetLocale("en");
break;
}
}
}
}