TalesOfNovariel/Assets/Scripts/Player/PlayerGameObject.cs
2025-06-07 09:05:32 +02:00

404 lines
13 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;
using Assets.Scripts.InteractableObjects;
using UnityEngine.UIElements;
using UnityEngine.Animations;
namespace Assets.Scripts.Player
{
public class PlayerGameObject : MonoBehaviour
{
public float speed = 5f;
public GameObject[] leftHands;
public GameObject[] rightHands;
UIHandler uihandler;
AudioHandler audioHandler;
WorldGenerator worldGenerator;
Inventory inventory;
int difficulty = 0;
bool finishedGame = false;
PlayerObject player;
DateTime now;
int jumpFrameCounter;
public bool isArmed;
bool canJump;
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>();
}
isArmed = true;
jumpFrameCounter = 0;
canJump = false;
}
// Update is called once per frame
void Update()
{
if (player == null)
{
return;
}
if (player.getStat("Killcount").getAmount() == -1)
{
return;
}
if (uihandler.state == UIState.DEATH)
{
return;
}
getRotation();
regeneratePlayer();
uihandler.adjustInformation(this);
if (!finishedGame)
{
gameFinished();
}
if (player.isDead())
{
SteamWorksHandler.getStandardAchievement("DeathAchievement");
uihandler.showDeathScreen();
}
}
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(4 - difficulty * 2, 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;
case 3:
role = new DruidClass();
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;
case 5:
race = new NightelfRace();
break;
}
string playername = PlayerPrefs.GetString("playername");
difficulty = PlayerPrefs.GetInt("difficulty");
player = new PlayerObject(playername, race, role, difficulty);
player.getClass().loadHandObjects();
GetComponent<Animator>().SetBool("isArmed", true);
}
//Generating player instance for stat preview during creation
public void generatePlayer(BasicRace playerRace, BasicClass playerClass, string name, int difficulty)
{
player = new PlayerObject(name, playerRace, playerClass, difficulty);
}
public void move(Vector3 input)
{
if(gameObject.GetComponent<Rigidbody>().linearVelocity.y <= 0.1f && gameObject.GetComponent<Rigidbody>().linearVelocity.y >= -0.1f){
jumpFrameCounter++;
}
if(jumpFrameCounter >= 10){
canJump = true;
jumpFrameCounter = 0;
}
if (input.y != 0)
{
if (canJump)
{
gameObject.GetComponent<Rigidbody>().linearVelocity = new Vector3(0, 5, 0);
audioHandler.playJump();
canJump = false;
}
}
Vector3 movement = new Vector3(input.x, 0, input.z);
gameObject.transform.Translate(movement * speed * Time.deltaTime);
gameObject.GetComponent<Animator>().SetFloat("velocity", (movement * speed).z);
GameObject.Find("QuestLog").GetComponent<QuestLog>().updateQuests("explore", gameObject, 1);
}
public void rotate(Vector2 input, Vector2 speed){
transform.Rotate(Vector3.up, input.x * speed.y);// * Time.deltaTime);
}
public void getRotation()
{
GameObject needle = GameObject.Find("imgNeedle");
float rotation = -gameObject.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() && player.getStat("Level").getAmount() >= difficulty * 10 + 10)
{
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.name.Contains("_"))
{
worldGenerator.changeCurrentTile(col.gameObject);
worldGenerator.createTile(new Vector3(-1, 0, 0));
worldGenerator.createTile(new Vector3(1, 0, 0));
worldGenerator.createTile(new Vector3(0, 0, 1));
worldGenerator.createTile(new Vector3(0, 0, -1));
worldGenerator.createTile(new Vector3(-1, 0, -1));
worldGenerator.createTile(new Vector3(1, 0, -1));
worldGenerator.createTile(new Vector3(-1, 0, 1));
worldGenerator.createTile(new Vector3(1, 0, 1));
}
if (col.name.Contains("House"))
{
if (!col.transform.Find("Door").GetComponent<Door>().hasInteracted)
{
transform.position = new Vector3(transform.position.x + 10, 10, transform.position.z);
}
}
}
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() + "(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 PlayerStat getPlayerStat(string identifier)
{
return player.getStat(identifier);
}
public void loadPlayer(JToken json)
{
player = new PlayerObject();
player.loadPlayer(json);
player.getClass().loadHandObjects();
GetComponent<Animator>().SetBool("isArmed", true);
}
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)
{
if (player.takeDamage(amount, inventory.getEquipmentBonus()["DEX"], inventory.getEquipmentBonus()["INT"]))
{
audioHandler.playDamage();
return true;
}
}
return false;
}
public int castSkill(int skillnumber)
{
int damage = player.castSkill(skillnumber, inventory.getEquipmentBonus()["INT"], inventory.getEquipmentBonus()["STR"], inventory.getEquipmentBonus()["DEX"]);
if (damage > 0)
{
player.getSkill(skillnumber).playSound(audioHandler);
}
return damage;
}
public void reduceCooldown(int skillnumber)
{
player.reduceCooldown(skillnumber);
}
public void regainSecondary()
{
player.regainSecondary(inventory.getEquipmentBonus()["MPR"], inventory.getEquipmentBonus()["MP"]);
}
}
}