3DSurvivalGame / Assets /Scripts /PlayerState.cs
doc2txt's picture
1
3497d64
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerState : MonoBehaviour
{
public static PlayerState Instance { get; set; }
public float currentHealth;
public float maxHealth;
public float currentCalories;
public float maxCalories;
public float currentHydration;
public float maxHydration;
float distanceTraveled = 0;
Vector3 lastPosition;
public GameObject playerBody;
public bool isHydrationActive = true;
private void Awake()
{
if (Instance != null && Instance != this)
{
DestroyObject(gameObject);
}
else
{
Instance = this;
}
}
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
currentCalories = maxCalories;
currentHydration = maxHydration;
StartCoroutine(decreaseHydration());
}
IEnumerator decreaseHydration()
{
while (isHydrationActive)
{
currentHydration--;
yield return new WaitForSeconds(20);
}
}
// Update is called once per frame
void Update()
{
distanceTraveled += Vector3.Distance(playerBody.transform.position, lastPosition);
lastPosition = playerBody.transform.position;
if (distanceTraveled >= 5)
{
distanceTraveled = 0;
currentCalories--;
}
}
public void setCalories(float newCalories)
{
currentCalories = newCalories;
}
internal void setHydration(float newHydration)
{
currentHydration = newHydration;
}
internal void setHealth(float newHealth)
{
currentHealth = newHealth;
}
}