Make lives work

This commit is contained in:
RochesterX
2025-03-07 10:03:16 -05:00
parent 0e4e6d1b8b
commit 6528d1050f
9 changed files with 108 additions and 35 deletions

10
Assets/EventSystemizer.cs Normal file
View File

@@ -0,0 +1,10 @@
using UnityEngine;
using UnityEngine.EventSystems;
public class EventSystemizer : MonoBehaviour
{
private void Update()
{
//foreach (EventSystem system in FindObjectsByType<EventSystem>(FindObjectsSortMode.None))
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 305a0eb0ddc9f88438e978efa8dd6f69

View File

@@ -26,8 +26,8 @@ Transform:
m_GameObject: {fileID: 210773656378718008}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0.566, z: 0}
m_LocalScale: {x: 1, y: 0.2075, z: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.95357, y: 0.16300578, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 210773657359985456}
@@ -113,8 +113,8 @@ Transform:
m_GameObject: {fileID: 210773656934484425}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0.566, z: 0}
m_LocalScale: {x: 1, y: 0.2075, z: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.95357, y: 0.16300578, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 210773657359985456}
@@ -191,13 +191,14 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &210773657359985456
RectTransform:
--- !u!4 &210773657359985456
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 210773657359985459}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
@@ -210,11 +211,6 @@ RectTransform:
- {fileID: 210773657761048651}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &7422290698914274434
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -233,7 +229,7 @@ MonoBehaviour:
healthVisual: {fileID: 210773656934484425}
actualHealthVisual: {fileID: 904379579236604940}
deathVisual: {fileID: 210773656378718008}
smoothSpeed: 0.1
smoothSpeed: 0.05
text: {fileID: 210773657480500664}
player: {fileID: 0}
--- !u!1 &210773657480500666
@@ -253,7 +249,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &210773657480500667
RectTransform:
m_ObjectHideFlags: 0
@@ -398,7 +394,7 @@ Transform:
m_GameObject: {fileID: 210773657645469774}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0.566, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1.0231425, y: 0.22951573, z: 1.1061}
m_ConstrainProportionsScale: 0
m_Children: []
@@ -477,7 +473,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &210773657761048651
RectTransform:
m_ObjectHideFlags: 0
@@ -587,8 +583,8 @@ Transform:
m_GameObject: {fileID: 904379579236604940}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0.566, z: 0}
m_LocalScale: {x: 1, y: 0.2075, z: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.95357, y: 0.16300578, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 210773657359985456}

View File

@@ -650,6 +650,7 @@ GameObject:
- component: {fileID: 241118428}
- component: {fileID: 241118427}
- component: {fileID: 241118426}
- component: {fileID: 241118429}
m_Layer: 0
m_Name: EventSystem
m_TagString: Untagged
@@ -718,6 +719,18 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &241118429
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 241118425}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 305a0eb0ddc9f88438e978efa8dd6f69, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &330396139
GameObject:
m_ObjectHideFlags: 0

View File

@@ -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()

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}
}
}