TalesOfNovariel/Assets/Scripts/Player/PlayerGameObject.cs
2023-12-13 12:49:38 +01:00

377 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using Assets.Scripts.Classes;
using Assets.Scripts.Races;
using Newtonsoft.Json.Linq;
using UnityEngine.InputSystem;
namespace Assets.Scripts.Player
{
public class PlayerGameObject : MonoBehaviour
{
UIHandler uihandler;
AudioHandler audioHandler;
WorldGenerator worldGenerator;
Inventory inventory;
int difficulty = 0;
bool finishedGame = false;
PlayerObject player;
public float speed = 5f;
DateTime jumpTimer;
DateTime now;
int bobbingDirection = -1;
private void OnEnable()
{
#if UNITY_EDITOR
SceneHandler.switchGameToMenu();
#endif
}
void Start()
{
uihandler = GameObject.Find("UIHandler").GetComponent<UIHandler>();
audioHandler = GameObject.Find("AudioHandler").GetComponent<AudioHandler>();
if (GameObject.Find("Inventory") != null)
{
inventory = GameObject.Find("Inventory").GetComponent<Inventory>();
}
if (GameObject.Find("WorldGenerator") != null)
{
worldGenerator = GameObject.Find("WorldGenerator").GetComponent<WorldGenerator>();
}
generatePlayer();
}
// Update is called once per frame
void Update()
{
if (player.getStat("Killcount").getAmount() != -1)
{
getRotation();
regeneratePlayer();
uihandler.adjustInformation(this);
if (!finishedGame)
{
gameFinished();
}
}
}
private void regeneratePlayer()
{
if (uihandler.state != UIState.FIGHT)
{
if (now == null)
{
now = DateTime.Now;
}
else
{
if (now.AddSeconds(10).CompareTo(DateTime.Now) <= 0)
{
now = DateTime.Now;
player.regainSecondary(inventory.getEquipmentBonus()["MPR"],inventory.getEquipmentBonus()["MP"]);
player.healPlayer(inventory.getEquipmentBonus()["HPR"],inventory.getEquipmentBonus()["HP"]);
}
}
}
else
{
now = DateTime.Now;
}
}
public void generatePlayer()
{
BasicRace race = new BasicRace();
BasicClass role = new BasicClass();
switch (PlayerPrefs.GetInt("class"))
{
case 0:
role = new WarriorClass();
break;
case 1:
role = new MageClass();
break;
case 2:
role = new ThiefClass();
break;
}
switch (PlayerPrefs.GetInt("race"))
{
case 0:
race = new HumanRace();
break;
case 1:
race = new ElvenRace();
break;
case 2:
race = new DwarvenRace();
break;
case 3:
race = new GoblinRace();
break;
case 4:
race = new GiantRace();
break;
}
string playername = PlayerPrefs.GetString("playername");
difficulty = PlayerPrefs.GetInt("difficulty");
player = new PlayerObject(playername, race, role, difficulty);
}
public void generatePlayer(BasicRace playerRace, BasicClass playerClass, string name, int difficulty){
player = new PlayerObject(name, playerRace, playerClass, difficulty);
}
public void move(Vector3 input)
{
GameObject camera = GameObject.Find("Main Camera");
if (input.y != 0)
{
if (jumpTimer != null)
{
if (jumpTimer.AddSeconds(1).CompareTo(DateTime.Now) <= 0)
{
jumpTimer = DateTime.Now;
gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 5, 0);
audioHandler.playJump();
}
}
else
{
jumpTimer = DateTime.Now;
gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 5, 0);
audioHandler.playJump();
}
}
Vector3 movement = new Vector3(input.x, 0, input.z);
movement = camera.transform.TransformDirection(movement);
movement.y = 0;
gameObject.transform.Translate(movement * speed * Time.deltaTime);
if (input.z != 0)
{
if (camera.transform.localPosition.y >= 0.5)
{
bobbingDirection = -1;
}
else if (camera.transform.localPosition.y <= 0.3)
{
bobbingDirection = 1;
}
camera.transform.Translate(new Vector3(0, bobbingDirection, 0) * Time.deltaTime / 2);
}
GameObject.Find("QuestLog").GetComponent<QuestLog>().updateQuests("explore", gameObject, 1);
}
public void getRotation()
{
GameObject needle = GameObject.Find("imgNeedle");
float rotation = GameObject.Find("Main Camera").transform.rotation.eulerAngles.y;
if (rotation < 0)
{
rotation += 360;
}
if (rotation >= 360)
{
rotation -= 360;
}
needle.transform.eulerAngles = new Vector3(0, 0, rotation);
}
public void gameFinished()
{
if (worldGenerator.gameWon())
{
finishedGame = true;
uihandler.showMessage("SUCCESS;" + TextHandler.getText("gameWon"));
switch (difficulty)
{
case 0:
SteamWorksHandler.getStandardAchievement("EasyAchievement");
break;
case 1:
SteamWorksHandler.getStandardAchievement("NormalAchievement");
break;
case 2:
SteamWorksHandler.getStandardAchievement("HardAchievement");
break;
}
}
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name.Contains("_"))
{
worldGenerator.changeCurrentTile(col.gameObject);
}
}
void OnTriggerStay(Collider col)
{
if (col.gameObject.name.Contains("border"))
{
Vector3 newTile = new Vector3();
if (col.gameObject.name.ToLower() == "bordernorth")
{
newTile = newTile + new Vector3(0, 0, 1);
}
if (col.gameObject.name.ToLower() == "bordersouth")
{
newTile = newTile + new Vector3(0, 0, -1);
}
if (col.gameObject.name.ToLower() == "bordereast")
{
newTile = newTile + new Vector3(1, 0, 0);
}
if (col.gameObject.name.ToLower() == "borderwest")
{
newTile = newTile + new Vector3(-1, 0, 0);
}
worldGenerator.createTile(newTile);
}
}
public void displayAction(int index, GameObject image, GameObject desc)
{
player.getSkill(index).display(image, desc, player.getStat("Secondary").getAmount());
}
public void enemyKilled()
{
uihandler.showMessage("SUCCESS;" + TextHandler.getText("enemyKilled"));
player.getStat("Killcount").changeAmount(1);
SteamWorksHandler.getSlimeAchievement(player.getStat("Killcount").getAmount());
gameFinished();
}
public void gainExperience(int amount)
{
if (player.gainExperience(amount))
{
uihandler.showMessage("SUCCESS;" + TextHandler.getText("levelUp"));
audioHandler.playLevelUp();
}
}
public void updateName(Text nameUI)
{
nameUI.text = player.getPlayerName() + " (" + TextHandler.getText(player.getRace().racename) + "/" + TextHandler.getText(player.getClass().classname) + ", Lvl. " + player.getStat("Level").getAmount() + ")";
}
public void updateNameHUD(Text nameUI)
{
nameUI.text = player.getPlayerName() + "\n\r" + TextHandler.getText(player.getRace().racename) + "/" + TextHandler.getText(player.getClass().classname) + "\r\n Lvl. " + player.getStat("Level").getAmount();
}
public void upgradeStrength()
{
if (!player.upgradeStat("Strength", 1))
{
uihandler.showMessage("ERROR;" + TextHandler.getText("noPoints"));
}
}
public void upgradeDexterity()
{
if (!player.upgradeStat("Dexterity", 1))
{
uihandler.showMessage("ERROR;" + TextHandler.getText("noPoints"));
}
}
public void upgradeIntelligence()
{
if (!player.upgradeStat("Intelligence", 1))
{
uihandler.showMessage("ERROR;" + TextHandler.getText("noPoints"));
}
}
public void upgradeHealth()
{
if (!player.upgradeStat("MaxHealth", 5))
{
uihandler.showMessage("ERROR;" + TextHandler.getText("noPoints"));
}
}
public void upgradeSecondary()
{
if (!player.upgradeStat("MaxSecondary", 5))
{
uihandler.showMessage("ERROR;" + TextHandler.getText("noPoints"));
}
}
public void displaySkills(GameObject pnlSkills)
{
for (int i = 1; i <= 3; i++)
{
GameObject skill = pnlSkills.transform.Find("skill" + i).gameObject;
player.getSkill(i - 1).displaySkill(skill.transform.Find("imgAction").gameObject, skill.transform.Find("descAction").gameObject);
}
}
public PlayerStat getPlayerStat(string identifier){
return player.getStat(identifier);
}
public void loadPlayer(JToken json){
player = new PlayerObject();
player.loadPlayer(json);
}
public PlayerObject getPlayer(){
return player;
}
public string saveGame(){
return player.saveGame();
}
public BasicClass getClass(){
return player.getClass();
}
public BasicRace getRace(){
return player.getRace();
}
public int calculateDamage(){
return player.calculateDamage(inventory.getEquipmentBonus()["STR"], inventory.getEquipmentBonus()["DEX"]);
}
public bool takeDamage(int amount){
if(player != null){
return player.takeDamage(amount,inventory.getEquipmentBonus()["DEX"], inventory.getEquipmentBonus()["INT"]);
}
return false;
}
public int castSkill(int skillnumber){
return player.castSkill(skillnumber,inventory.getEquipmentBonus()["INT"], inventory.getEquipmentBonus()["STR"], inventory.getEquipmentBonus()["DEX"]);
}
public void reduceCooldown(int skillnumber){
player.reduceCooldown(skillnumber);
}
public void regainSecondary(){
player.regainSecondary(inventory.getEquipmentBonus()["MPR"], inventory.getEquipmentBonus()["MP"]);
}
}
}