first commit
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class PlayerHealth : MonoBehaviour
|
||||
{
|
||||
public float maxHealth = 200f;
|
||||
|
||||
public float CurrentHealth { get; private set; }
|
||||
public bool IsDead { get; private set; }
|
||||
|
||||
private HUDController _hud;
|
||||
private FPSController _controller;
|
||||
private WeaponSwitcher _weaponSwitcher;
|
||||
|
||||
public void Initialize(HUDController hud, FPSController controller, WeaponSwitcher weaponSwitcher)
|
||||
{
|
||||
_hud = hud;
|
||||
_controller = controller;
|
||||
_weaponSwitcher = weaponSwitcher;
|
||||
CurrentHealth = maxHealth;
|
||||
_hud?.SetHealth(CurrentHealth, maxHealth);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
CurrentHealth = maxHealth;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (_hud == null)
|
||||
{
|
||||
_hud = FindObjectOfType<HUDController>();
|
||||
}
|
||||
|
||||
if (_controller == null)
|
||||
{
|
||||
_controller = GetComponent<FPSController>();
|
||||
}
|
||||
|
||||
if (_weaponSwitcher == null)
|
||||
{
|
||||
_weaponSwitcher = GetComponent<WeaponSwitcher>();
|
||||
}
|
||||
|
||||
if (CurrentHealth <= 0f)
|
||||
{
|
||||
CurrentHealth = maxHealth;
|
||||
}
|
||||
|
||||
_hud?.SetHealth(CurrentHealth, maxHealth);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!IsDead)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.R))
|
||||
{
|
||||
Time.timeScale = 1f;
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public void TakeDamage(float amount)
|
||||
{
|
||||
if (IsDead)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CurrentHealth = Mathf.Clamp(CurrentHealth - amount, 0f, maxHealth);
|
||||
_hud?.SetHealth(CurrentHealth, maxHealth);
|
||||
_hud?.ShowDamageFlash();
|
||||
|
||||
if (CurrentHealth <= 0f)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Heal(int amount)
|
||||
{
|
||||
if (IsDead || CurrentHealth >= maxHealth)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CurrentHealth = Mathf.Clamp(CurrentHealth + amount, 0f, maxHealth);
|
||||
_hud?.SetHealth(CurrentHealth, maxHealth);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
IsDead = true;
|
||||
Time.timeScale = 0f;
|
||||
|
||||
if (_controller != null)
|
||||
{
|
||||
_controller.SetLookEnabled(false);
|
||||
_controller.enabled = false;
|
||||
_controller.SetCursorLocked(false);
|
||||
}
|
||||
|
||||
if (_weaponSwitcher != null)
|
||||
{
|
||||
_weaponSwitcher.enabled = false;
|
||||
}
|
||||
|
||||
_hud?.ShowDeathScreen();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user