Files
SimUL/Assets/Scripts/PlayerManager.cs
T
2022-11-17 17:53:30 +02:00

139 lines
4.4 KiB
C#

using Assets.Scripts.Actions;
using Assets.Scripts.Actions.Interfaces;
using UnityEngine;
using UnityEngine.AI;
public class PlayerManager : MonoBehaviour
{
private enum States { Idle, Walking, Sleeping };
public Stat money = new Stat("Money", 100.0f);
public Stat rentAccount = new Stat("Rent Account", 0);
public Stat food = new Stat("Food Energy", 100);
public Stat energy = new Stat("Energy", 12);
// bank
public Stat bankAccount = new Stat("Bank Account", 0);
// jobs
public Stat job = new Stat("Unemployed", 0);
// Knowledge for University Jobs
public Stat literatureKnowledge = new Stat("LiteratureKnowledge", 0);
public Stat mathematicsKnowledge = new Stat("MathematicsKnowledge", 0);
public Stat computerScienceKnowledge = new Stat("ComputerScienceKnowledge", 0);
// Knowledge for Factory Jobs
public Stat electronicsKnowledge = new Stat("ElectronicsKnowledge", 0);
public Stat roboticsKnowledge = new Stat("RoboticsKnowledge", 0);
public Stat industrialDesignKnowledge = new Stat("IndustrialDesignKnowledge", 0);
public Stat careerPoints = new Stat("careerPoints", 0);
// Achievements - to WIN the game
public Stat whealthAchievement = new Stat("whealthAchievement", 999999);
public Stat educationAchievement = new Stat("educationAchievement", 999999);
public Stat careerAchievement = new Stat("careerAchievement", 999999);
public Stat happinessAchievement = new Stat("happinessAchievement", 999999);
// Inventory items
public Stat freezerItem = new Stat("Freezer Item", 400, 1);
public Stat clothesItem = new Stat("Clothes Item", 200, 3);
public Stat booksItem = new Stat("Books Item", 150, 2);
public Stat tvItem = new Stat("Tv Item", 1500, 1);
public Stat laptopItem = new Stat("Laptop Item", 3000, 1);
public NavMeshAgent player;
public Animator playerAnimator;
public Camera playerCamera;
public ParticleSystem targetDest;
public bool allowMovement = true;
private States _state;
private void OnEnable()
{
TimeManager.OnMinuteChanged += DecreaseEnergy;
}
private void OnDisable()
{
TimeManager.OnMinuteChanged -= DecreaseEnergy;
}
// Start is called before the first frame update
void Start()
{
allowMovement = true;
}
// Update is called once per frame
void Update()
{
if (allowMovement)
{
if (Input.GetMouseButton(0))
{
Ray myRay = playerCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myRay, out RaycastHit hit))
{
targetDest.transform.position = hit.point;
targetDest.Play();
player.SetDestination(hit.point);
}
}
}
else
{
player.SetDestination(player.transform.position);
player.velocity = Vector3.zero;
}
if (player.velocity != Vector3.zero)
{
playerAnimator.SetBool("IsWalking", true);
}
else if (player.velocity == Vector3.zero)
{
playerAnimator.SetBool("IsWalking", false);
}
}
public void DecreaseEnergy()
{
food.deduct(0.034f); // 48 hours it's 100, 100/2880=~0.034 per minute
switch (_state)
{
case States.Idle:
case States.Walking:
energy.deduct(0.1f); // 24 hours it's 100, 100/1440=~0.096 per minute
break;
case States.Sleeping:
energy.increase(1f);
break;
}
if (energy.Value <= 10 && _state != States.Sleeping)
{
_state = States.Sleeping;
allowMovement = false;
playerAnimator.SetBool("IsSleeping", true);
}
if (energy.Value >= 100 && _state == States.Sleeping)
{
_state = States.Idle;
allowMovement = false;
playerAnimator.SetBool("IsSleeping", false);
}
}
public bool TryBuyAction(BaseAction action)
{
if (action is ISellableItem)
{
if (!money.deduct(((ISellableItem)action).Price))
return false;
}
action.ApplyAction(this);
return true;
}
}