Make lives work
This commit is contained in:
@@ -10,12 +10,13 @@ public class Damageable : MonoBehaviour
|
||||
public float force = 50f;
|
||||
public float damage = 0f;
|
||||
public float maxDamage = 1000f;
|
||||
private GameManager gameManager;
|
||||
public int lives = 3;
|
||||
//private GameManager gameManager;
|
||||
private Animator animator;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
gameManager = GameManager.Instance;
|
||||
//gameManager = GameManager.Instance;
|
||||
animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
@@ -57,6 +58,15 @@ public class Damageable : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public void Damage(float damage)
|
||||
{
|
||||
this.damage += damage;
|
||||
if (damage >= maxDamage)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void SuccessfulParry(GameObject damageSource, float force)
|
||||
{
|
||||
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * force, ForceMode2D.Force);
|
||||
@@ -70,8 +80,8 @@ public class Damageable : MonoBehaviour
|
||||
|
||||
private void Die()
|
||||
{
|
||||
Debug.Log($"{name}: MAKE THIS WORK.");
|
||||
if (gameManager != null)
|
||||
//Debug.Log($"{name}: MAKE THIS WORK.");
|
||||
if (GameManager.Instance != null)
|
||||
{
|
||||
animator.SetTrigger("Die");
|
||||
StartCoroutine(HandleDeath());
|
||||
@@ -80,8 +90,9 @@ public class Damageable : MonoBehaviour
|
||||
|
||||
private IEnumerator HandleDeath()
|
||||
{
|
||||
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
|
||||
gameManager.PlayerDied(gameObject);
|
||||
//yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
GameManager.Instance.PlayerDied(this);
|
||||
}
|
||||
|
||||
public void Respawn()
|
||||
|
||||
@@ -69,18 +69,18 @@ public class GameManager : MonoBehaviour
|
||||
public static string map = "Platformer With Headroom"; // loads a default map as a safety net
|
||||
public Vector2 spawnPosition;
|
||||
|
||||
public void PlayerDied(GameObject player)
|
||||
public void PlayerDied(Damageable player)
|
||||
{
|
||||
if (gameMode == GameMode.freeForAll)
|
||||
{
|
||||
currentLives--;
|
||||
if (currentLives <= 0)
|
||||
player.lives--;
|
||||
if (player.lives <= 0)
|
||||
{
|
||||
GameOver(player);
|
||||
GameOver(player.gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
RespawnPlayer(player);
|
||||
RespawnPlayer(player.gameObject);
|
||||
}
|
||||
}
|
||||
if (gameMode == GameMode.keepAway)
|
||||
@@ -100,6 +100,7 @@ public class GameManager : MonoBehaviour
|
||||
{
|
||||
player.transform.position = respawnScript.spawnPoint;
|
||||
player.GetComponent<Damageable>().ResetDamage();
|
||||
player.GetComponent<Damageable>().Respawn();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +108,22 @@ public class GameManager : MonoBehaviour
|
||||
{
|
||||
// Add game over screen
|
||||
player.SetActive(false);
|
||||
EndGameEvent?.Invoke();
|
||||
if (AlivePlayers().Count <= 1)
|
||||
{
|
||||
EndGameEvent?.Invoke();
|
||||
print(AlivePlayers()[0].name + " is the winner");
|
||||
}
|
||||
}
|
||||
|
||||
private List<GameObject> AlivePlayers()
|
||||
{
|
||||
List<GameObject> alivePlayers = new();
|
||||
|
||||
foreach (GameObject player in players)
|
||||
{
|
||||
if (player.activeInHierarchy) alivePlayers.Add(player);
|
||||
}
|
||||
|
||||
return alivePlayers;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,10 @@ public class HealthBarManager : MonoBehaviour
|
||||
{
|
||||
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;
|
||||
//Vector3 screenPosition = Camera.main.WorldToScreenPoint(player.transform.position);
|
||||
//screenPosition.y += 15;
|
||||
//Vector3 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
|
||||
healthBar.transform.SetPositionAndRotation(new Vector3(player.transform.position.x, player.transform.position.y + 1.5f, player.transform.position.z), Quaternion.identity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +38,7 @@ public class HealthBarManager : MonoBehaviour
|
||||
if (!playerHealthBars.ContainsKey(player))
|
||||
{
|
||||
GameObject healthBar = Instantiate(healthBarPrefab);
|
||||
healthBar.transform.localScale *= 1.5f;
|
||||
healthBar.GetComponent<TerribleHealthBarScript>().SetPlayer(player);
|
||||
playerHealthBars[player] = healthBar;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// This won scene thing is just duct taped on for the presentation.
|
||||
|
||||
public class PlayerCameraMovement : MonoBehaviour
|
||||
{
|
||||
private Vector3 start;
|
||||
private Vector3 target;
|
||||
public float weight;
|
||||
public float speed;
|
||||
private GameObject playerThatWon;
|
||||
|
||||
public bool winScene = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -15,6 +20,15 @@ public class PlayerCameraMovement : MonoBehaviour
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (winScene)
|
||||
{
|
||||
if (playerThatWon == null) playerThatWon = GameManager.players[0];
|
||||
target = playerThatWon.transform.position;
|
||||
transform.position = Vector3.Lerp(transform.position, new Vector3(target.x, target.y, target.z - 10), speed * Time.deltaTime);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
List<GameObject> players = GameManager.players;
|
||||
|
||||
if (players.Count == 0) return;
|
||||
@@ -30,4 +44,10 @@ public class PlayerCameraMovement : MonoBehaviour
|
||||
transform.position = Vector3.Lerp(transform.position, new Vector3(target.x, target.y, transform.position.z), speed * Time.deltaTime);
|
||||
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
|
||||
}
|
||||
|
||||
public void WinScene(GameObject player)
|
||||
{
|
||||
winScene = true;
|
||||
playerThatWon = player;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,11 @@ public class RespawnOnTriggerEnter : MonoBehaviour
|
||||
{
|
||||
if (other.CompareTag(respawnTag))
|
||||
{
|
||||
GetComponent<Damageable>().Respawn();
|
||||
//GetComponent<Damageable>().Respawn();
|
||||
if (TryGetComponent(out Damageable damageable))
|
||||
{
|
||||
damageable.Damage(9999f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user