Health bar and player management refined
This commit is contained in:
@@ -24,7 +24,6 @@ public class AnimationPlayer : MonoBehaviour
|
||||
{
|
||||
animator.SetInteger("state", (int)state);
|
||||
transform.localScale = new Vector3(Mathf.Sign(backwards ? -1 : 1) * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
|
||||
|
||||
animator.SetBool("block", block);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
@@ -8,19 +10,13 @@ public class Damageable : MonoBehaviour
|
||||
public float force = 50f;
|
||||
public float damage = 0f;
|
||||
public float maxDamage = 1000f;
|
||||
public HealthBar healthBar;
|
||||
|
||||
private GameManager gameManager;
|
||||
private Animator animator;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
gameManager = GameManager.Instance;
|
||||
|
||||
if (healthBar != null)
|
||||
{
|
||||
healthBar.SetMaxHealth(maxDamage);
|
||||
healthBar.SetHealth(maxDamage - damage);
|
||||
}
|
||||
animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
@@ -55,10 +51,6 @@ public class Damageable : MonoBehaviour
|
||||
}
|
||||
damage += actualForce;
|
||||
damage = Mathf.Clamp(damage, 0f, maxDamage);
|
||||
if (healthBar != null)
|
||||
{
|
||||
healthBar.SetHealth(maxDamage - damage);
|
||||
}
|
||||
if (damage >= maxDamage)
|
||||
{
|
||||
Die();
|
||||
@@ -70,10 +62,6 @@ public class Damageable : MonoBehaviour
|
||||
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * force, ForceMode2D.Force);
|
||||
damage += force;
|
||||
damage = Mathf.Clamp(damage, 0f, maxDamage);
|
||||
if (healthBar != null)
|
||||
{
|
||||
healthBar.SetHealth(maxDamage - damage);
|
||||
}
|
||||
if (damage >= maxDamage)
|
||||
{
|
||||
Die();
|
||||
@@ -82,12 +70,18 @@ public class Damageable : MonoBehaviour
|
||||
|
||||
private void Die()
|
||||
{
|
||||
Debug.Log($"{name}: MAKE THIS WORK.");
|
||||
if (gameManager != null)
|
||||
{
|
||||
gameManager.PlayerDied(gameObject); //add death animation trigger
|
||||
animator.SetTrigger("Die");
|
||||
StartCoroutine(HandleDeath());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private IEnumerator HandleDeath()
|
||||
{
|
||||
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
|
||||
gameManager.PlayerDied(gameObject);
|
||||
}
|
||||
|
||||
public void Respawn()
|
||||
@@ -96,6 +90,7 @@ public class Damageable : MonoBehaviour
|
||||
if (TryGetComponent<Rigidbody2D>(out var rb))
|
||||
{
|
||||
rb.linearVelocity = Vector2.zero;
|
||||
rb.angularVelocity = 0f;
|
||||
}
|
||||
if (TryGetComponent<Damageable>(out var damageable))
|
||||
{
|
||||
@@ -106,10 +101,5 @@ public class Damageable : MonoBehaviour
|
||||
public void ResetDamage()
|
||||
{
|
||||
damage = 0f;
|
||||
if (healthBar != null)
|
||||
{
|
||||
healthBar.SetHealth(maxDamage);
|
||||
}
|
||||
//transform.localScale = Vector3.one;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@ public class GameManager : MonoBehaviour
|
||||
public static GameManager Instance { get; private set; }
|
||||
public int maxLives = 3;
|
||||
public int currentLives;
|
||||
public delegate void GameEvent();
|
||||
public event GameEvent StartGameEvent;
|
||||
public event GameEvent EndGameEvent;
|
||||
public static List<GameObject> players = new List<GameObject>();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -26,21 +30,31 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
StartGameEvent?.Invoke();
|
||||
print("Starting game with mode: " + gameMode + " and map: " + map);
|
||||
if (gameMode == GameMode.freeForAll)
|
||||
{
|
||||
currentLives = maxLives;
|
||||
StartFreeForAll();
|
||||
foreach (GameObject player in players)
|
||||
{
|
||||
player.transform.position = spawnPosition;
|
||||
}
|
||||
}
|
||||
if (gameMode == GameMode.keepAway)
|
||||
{
|
||||
currentLives = 1;
|
||||
StartKeepAway();
|
||||
foreach (GameObject player in players)
|
||||
{
|
||||
player.transform.position = spawnPosition;
|
||||
}
|
||||
}
|
||||
if (gameMode == GameMode.obstacleCourse)
|
||||
{
|
||||
currentLives = 1;
|
||||
StartObstacleCourse();
|
||||
foreach (GameObject player in players)
|
||||
{
|
||||
player.transform.position = spawnPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,38 +65,10 @@ public class GameManager : MonoBehaviour
|
||||
obstacleCourse
|
||||
}
|
||||
|
||||
public static GameMode gameMode = GameMode.freeForAll;
|
||||
|
||||
public static string map = "Platformer With Headroom"; //called for in PlayerManager and should be changed to load from here instead
|
||||
|
||||
public static List<GameObject> players = new List<GameObject>();
|
||||
|
||||
public static GameMode gameMode = GameMode.freeForAll; // loads a default gamemode as a safety net
|
||||
public static string map = "Platformer With Headroom"; // loads a default map as a safety net
|
||||
public Vector2 spawnPosition;
|
||||
|
||||
private void StartFreeForAll()
|
||||
{
|
||||
foreach (GameObject player in players)
|
||||
{
|
||||
player.transform.position = spawnPosition;
|
||||
}
|
||||
}
|
||||
|
||||
private void StartKeepAway()
|
||||
{
|
||||
foreach (GameObject player in players)
|
||||
{
|
||||
player.transform.position = spawnPosition;
|
||||
}
|
||||
}
|
||||
|
||||
private void StartObstacleCourse()
|
||||
{
|
||||
foreach (GameObject player in players)
|
||||
{
|
||||
player.transform.position = spawnPosition;
|
||||
}
|
||||
}
|
||||
|
||||
public void PlayerDied(GameObject player)
|
||||
{
|
||||
if (gameMode == GameMode.freeForAll)
|
||||
@@ -119,7 +105,8 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
private void GameOver(GameObject player)
|
||||
{
|
||||
// Disable player controls and show game over screen
|
||||
// Add game over screen
|
||||
player.SetActive(false);
|
||||
EndGameEvent?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class HealthBar : MonoBehaviour
|
||||
{
|
||||
public Slider slider;
|
||||
|
||||
public void SetMaxHealth(float health)
|
||||
{
|
||||
if (slider != null)
|
||||
{
|
||||
slider.maxValue = health;
|
||||
slider.value = health;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetHealth(float health)
|
||||
{
|
||||
if (slider != null)
|
||||
{
|
||||
slider.value = health;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 15c35eadf0afb0e42a41e4d6f5350554
|
||||
56
Assets/Scripts/HealthBarManager.cs
Normal file
56
Assets/Scripts/HealthBarManager.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class HealthBarManager : MonoBehaviour
|
||||
{
|
||||
public GameObject healthBarPrefab;
|
||||
private Dictionary<GameObject, GameObject> playerHealthBars = new Dictionary<GameObject, GameObject>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
GameManager.Instance.StartGameEvent += OnGameStart;
|
||||
GameManager.Instance.EndGameEvent += OnGameEnd;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
GameManager.Instance.StartGameEvent -= OnGameStart;
|
||||
GameManager.Instance.EndGameEvent -= OnGameEnd;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
foreach (var kvp in playerHealthBars)
|
||||
{
|
||||
GameObject player = kvp.Key;
|
||||
GameObject healthBar = kvp.Value;
|
||||
Vector3 screenPosition = Camera.main.WorldToScreenPoint(player.transform.position);
|
||||
screenPosition.y += 15;
|
||||
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
|
||||
healthBar.transform.position = worldPosition;
|
||||
healthBar.transform.rotation = Quaternion.identity;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGameStart()
|
||||
{
|
||||
foreach (GameObject player in GameManager.players)
|
||||
{
|
||||
if (!playerHealthBars.ContainsKey(player))
|
||||
{
|
||||
GameObject healthBar = Instantiate(healthBarPrefab);
|
||||
healthBar.GetComponent<TerribleHealthBarScript>().SetPlayer(player);
|
||||
playerHealthBars[player] = healthBar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGameEnd()
|
||||
{
|
||||
foreach (var kvp in playerHealthBars)
|
||||
{
|
||||
Destroy(kvp.Value);
|
||||
}
|
||||
playerHealthBars.Clear();
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/HealthBarManager.cs.meta
Normal file
2
Assets/Scripts/HealthBarManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a80b536f823e50142b142b4e0b64ea97
|
||||
@@ -5,15 +5,12 @@ using UnityEngine.InputSystem;
|
||||
public class PlayerManager : MonoBehaviour
|
||||
{
|
||||
public static PlayerManager Instance;
|
||||
|
||||
public List<PlayerJoinCard> cards;
|
||||
[SerializeField] private InputActionAsset playerActions;
|
||||
|
||||
public List<Color> playerColors;
|
||||
|
||||
public GameObject playerSelect;
|
||||
|
||||
private bool gameStarted = false;
|
||||
private bool isStartPressed = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -34,17 +31,12 @@ public class PlayerManager : MonoBehaviour
|
||||
Destroy(playerInput.gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("Player joined");
|
||||
|
||||
DontDestroyOnLoad(playerInput.gameObject);
|
||||
|
||||
PlayerJoinCard card = PlayerCardCreator.Instance.CreateCard();
|
||||
card.playerNumber = GameManager.players.Count + 1;
|
||||
cards.Add(card);
|
||||
|
||||
GameManager.players.Add(playerInput.gameObject);
|
||||
|
||||
Colorize(GameManager.players.Count - 1);
|
||||
}
|
||||
|
||||
@@ -63,26 +55,33 @@ public class PlayerManager : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("A PlayerManager already exists.");
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
gameStarted = true;
|
||||
Debug.Log("Game started");
|
||||
HubManager.Instance.LoadScene(GameManager.map);
|
||||
if (GameManager.players.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (isStartPressed)
|
||||
{
|
||||
gameStarted = true;
|
||||
HubManager.Instance.LoadScene(GameManager.map);
|
||||
}
|
||||
else
|
||||
{
|
||||
isStartPressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void Colorize(int index)
|
||||
{
|
||||
GameObject player = GameManager.players[index];
|
||||
|
||||
Color color = playerColors[(GameManager.players.Count - 1) % playerColors.Count];
|
||||
float tint = Mathf.Floor((GameManager.players.Count - 1) / playerColors.Count);
|
||||
color = (color + color + Color.white * tint) / (tint + 2);
|
||||
|
||||
ApplyColor(player, color);
|
||||
ApplyColor(cards[GameManager.players.IndexOf(player)].playerPreview, color);
|
||||
}
|
||||
@@ -101,4 +100,4 @@ public class PlayerManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,4 +21,4 @@ public class RespawnOnTriggerEnter : MonoBehaviour
|
||||
GetComponent<Damageable>().Respawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,45 +21,64 @@ public class TerribleHealthBarScript : MonoBehaviour
|
||||
private Vector3 targetPosition;
|
||||
private Color targetActualColor;
|
||||
|
||||
private Damageable player;
|
||||
public GameObject player;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
healthScript = player.GetComponent<Damageable>();
|
||||
initialScale = healthVisual.transform.localScale;
|
||||
initialPosition = healthVisual.transform.position;
|
||||
targetScale = initialScale;
|
||||
targetPosition = initialPosition;
|
||||
targetActualColor = actualHealthVisual.GetComponent<SpriteRenderer>().color;
|
||||
if (healthScript == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Initialize();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (healthScript.gameObject.transform.localScale.x <= 0f)
|
||||
if (player == null || healthScript == null)
|
||||
{
|
||||
transform.localScale = new Vector3(-1 * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.localScale = new Vector3(1 * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
|
||||
}//
|
||||
|
||||
float healthRatio = (healthScript.maxDamage - healthScript.damage) / healthScript.maxDamage;
|
||||
|
||||
targetActualColor = Color.Lerp(fullDeathColor, fullHealthColor, healthRatio);
|
||||
targetScale = new Vector3(Mathf.Lerp(0, 1, healthRatio) * initialScale.x, healthVisual.transform.localScale.y, healthVisual.transform.localScale.z);
|
||||
targetPosition = new Vector3(Mathf.Lerp(-0.5f, 0, healthRatio), healthVisual.transform.localPosition.y, healthVisual.transform.localPosition.z);
|
||||
text.text = (healthScript.maxDamage - healthScript.damage).ToString() + "/" + healthScript.maxDamage.ToString();
|
||||
|
||||
actualHealthVisual.transform.localScale = targetScale;
|
||||
actualHealthVisual.transform.localPosition = targetPosition;
|
||||
|
||||
healthVisual.transform.localScale = Vector3.Lerp(healthVisual.transform.localScale, targetScale, smoothSpeed);
|
||||
healthVisual.transform.localPosition = Vector3.Lerp(healthVisual.transform.localPosition, targetPosition, smoothSpeed);
|
||||
|
||||
actualHealthVisual.GetComponent<SpriteRenderer>().color = Color.Lerp(actualHealthVisual.GetComponent<SpriteRenderer>().color, targetActualColor, smoothSpeed);
|
||||
deathVisual.GetComponent<SpriteRenderer>().color = Color.Lerp(deathVisual.GetComponent<SpriteRenderer>().color, targetActualColor * 0.5f, smoothSpeed);
|
||||
healthVisual.GetComponent<SpriteRenderer>().color = subtractionColor;
|
||||
}
|
||||
|
||||
public void SetPlayer(GameObject player)
|
||||
{
|
||||
this.player = player;
|
||||
if (this.player == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
healthScript = player.GetComponent<Damageable>();
|
||||
if (healthScript == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Initialize();
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
initialScale = healthVisual.transform.localScale;
|
||||
initialPosition = healthVisual.transform.position;
|
||||
targetScale = initialScale;
|
||||
targetPosition = initialPosition;
|
||||
targetActualColor = actualHealthVisual.GetComponent<SpriteRenderer>().color;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user