Health stuff
This commit is contained in:
8
3D blobici/Assets/Scripts/HP.meta
Normal file
8
3D blobici/Assets/Scripts/HP.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 987db2b9503502740955897893550fde
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
60
3D blobici/Assets/Scripts/HP/Health Manager.cs
Normal file
60
3D blobici/Assets/Scripts/HP/Health Manager.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
public class HealthManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float _maxHealth = 100f;
|
||||
[SerializeField] private float _currentHealth = 100f;
|
||||
[SerializeField] private TMP_Text _healthText;
|
||||
|
||||
public float CurrentHealth { get => _currentHealth;}
|
||||
public float MaxHealth { get => _maxHealth; }
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if(_healthText != null)
|
||||
_healthText.text = _currentHealth.ToString() + "/" + _maxHealth.ToString();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_healthText != null)
|
||||
_healthText.text = _currentHealth.ToString() + "/" + _maxHealth.ToString();
|
||||
|
||||
|
||||
// These are only for debugging!!!!
|
||||
if(Input.GetKeyDown(KeyCode.K))
|
||||
if(transform.tag == "Player")
|
||||
ModifyHealth(-10);
|
||||
|
||||
if(Input.GetKeyDown(KeyCode.L))
|
||||
ModifyHealth(10);
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.M))
|
||||
if(transform.tag == "Enemy")
|
||||
ModifyHealth(-50);
|
||||
}
|
||||
|
||||
public void ModifyHealth(float amount)
|
||||
{
|
||||
if (_currentHealth + amount > _maxHealth)
|
||||
{
|
||||
_currentHealth = _maxHealth;
|
||||
return;
|
||||
}
|
||||
if (_currentHealth + amount <= 0)
|
||||
{
|
||||
_currentHealth = 0;
|
||||
if (transform.tag == "Player")
|
||||
Debug.Log("Game over bastarde");
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
Debug.Log("Character named " + gameObject.name + " died");
|
||||
}
|
||||
return;
|
||||
}
|
||||
_currentHealth += amount;
|
||||
}
|
||||
|
||||
}
|
||||
2
3D blobici/Assets/Scripts/HP/Health Manager.cs.meta
Normal file
2
3D blobici/Assets/Scripts/HP/Health Manager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1685f5c21ab4b2e418e35e5cf3e60864
|
||||
@@ -12,24 +12,15 @@ public class PlayerMovement : MonoBehaviour
|
||||
private CharacterController controller;
|
||||
private Vector3 velocity;
|
||||
private PlayerSkillTree PlayerSkills;
|
||||
private int health;
|
||||
private int maxHealth;
|
||||
[Header ("Health")]
|
||||
[SerializeField] private TMP_Text healthBar;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
PlayerSkills = PlayerSkillTree.Instance;
|
||||
maxHealth = PlayerSkills.MaxHealth;
|
||||
health = PlayerSkills.GetHealth();
|
||||
|
||||
Debug.Assert(maxHealth > 0);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
controller = GetComponent<CharacterController>();
|
||||
healthBar.text = GetHealthText();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -50,17 +41,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
Debug.Log("Potion not available!");
|
||||
}
|
||||
}
|
||||
|
||||
// TEMPORARY!!! LATER USED FOR COLISIONS
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.F))
|
||||
{
|
||||
PlayerSkills.SetHealth(-20);
|
||||
health = PlayerSkills.GetHealth();
|
||||
|
||||
healthBar.text = GetHealthText();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
@@ -116,9 +96,4 @@ public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
return PlayerSkills.GetPlayerSkills();
|
||||
}
|
||||
|
||||
private string GetHealthText()
|
||||
{
|
||||
return (health.ToString() + " / " + maxHealth.ToString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user