From f1d9006c94c70132c91d90bbc29cfecb599aae37 Mon Sep 17 00:00:00 2001 From: djkellerman Date: Fri, 28 Feb 2025 13:49:02 -0500 Subject: [PATCH] Script reorganization and UseItem created --- Assets/Prefabs/Bones.prefab | 15 +----- Assets/Scenes/Player Select.unity | 23 +++++++++ Assets/Scripts/Damageable.cs | 9 ++-- Assets/Scripts/GameManager.cs | 69 +++++++++++++++---------- Assets/Scripts/PlayerLives.cs | 56 -------------------- Assets/Scripts/PlayerLives.cs.meta | 2 - Assets/Scripts/PlayerManager.cs | 20 +++++-- Assets/Scripts/Punch.cs | 2 + Assets/Scripts/TerminalVelocity.cs | 21 -------- Assets/Scripts/TerminalVelocity.cs.meta | 2 - Assets/Scripts/UseItem.cs | 56 +++++++++++++++++--- 11 files changed, 138 insertions(+), 137 deletions(-) delete mode 100644 Assets/Scripts/PlayerLives.cs delete mode 100644 Assets/Scripts/PlayerLives.cs.meta delete mode 100644 Assets/Scripts/TerminalVelocity.cs delete mode 100644 Assets/Scripts/TerminalVelocity.cs.meta diff --git a/Assets/Prefabs/Bones.prefab b/Assets/Prefabs/Bones.prefab index 7dab44c..dc95dbd 100644 --- a/Assets/Prefabs/Bones.prefab +++ b/Assets/Prefabs/Bones.prefab @@ -18,7 +18,6 @@ GameObject: - component: {fileID: 1169583671018505929} - component: {fileID: 2344147968324054325} - component: {fileID: 8121950008105218343} - - component: {fileID: 3303109471384786876} - component: {fileID: 2813634395235005482} m_Layer: 0 m_Name: Bones @@ -137,6 +136,7 @@ MonoBehaviour: force: 200 damage: 0 maxDamage: 1000 + healthBar: {fileID: 0} --- !u!114 &5200627265685207284 MonoBehaviour: m_ObjectHideFlags: 0 @@ -262,19 +262,6 @@ BoxCollider2D: m_AutoTiling: 0 m_Size: {x: 0.4505486, y: 1.9437933} m_EdgeRadius: 0 ---- !u!114 &3303109471384786876 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1375850422501237868} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 36267595aa66046ac9e2e140190bbc16, type: 3} - m_Name: - m_EditorClassIdentifier: - terminalVelocity: -25 --- !u!114 &2813634395235005482 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/Player Select.unity b/Assets/Scenes/Player Select.unity index fc7c2dd..98c3e87 100644 --- a/Assets/Scenes/Player Select.unity +++ b/Assets/Scenes/Player Select.unity @@ -127,6 +127,18 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 4162490554469064066, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3} + propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_Target + value: + objectReference: {fileID: 1678402538523344736} + - target: {fileID: 4162490554469064066, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3} + propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_TargetAssemblyTypeName + value: PlayerManager, Assembly-CSharp + objectReference: {fileID: 0} + - target: {fileID: 5507202815382103773, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3} + propertyPath: m_AnchoredPosition.x + value: 300.80005 + objectReference: {fileID: 0} - target: {fileID: 7581121400164956810, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3} propertyPath: m_Name value: Player Select @@ -176,6 +188,17 @@ PrefabInstance: m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3} +--- !u!114 &1678402538523344736 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 3284257257135167210, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3} + m_PrefabInstance: {fileID: 1678402538523344735} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 33e0fad1e452e0140bc99e780d4dda4f, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Damageable.cs b/Assets/Scripts/Damageable.cs index 449f2af..1576464 100644 --- a/Assets/Scripts/Damageable.cs +++ b/Assets/Scripts/Damageable.cs @@ -10,8 +10,12 @@ public class Damageable : MonoBehaviour public float maxDamage = 1000f; public HealthBar healthBar; + private GameManager gameManager; + private void Start() { + gameManager = GameManager.Instance; + if (healthBar != null) { healthBar.SetMaxHealth(maxDamage); @@ -78,10 +82,9 @@ public class Damageable : MonoBehaviour private void Die() { - PlayerLives playerLives = GetComponent(); //add death animation trigger - if (playerLives != null) + if (gameManager != null) { - playerLives.PlayerDied(); + gameManager.PlayerDied(gameObject); //add death animation trigger } } diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 341fabf..02b97fc 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -3,22 +3,7 @@ using UnityEngine; public class GameManager : MonoBehaviour { - public void StartGame() - { - if (gameMode == GameMode.freeForAll) - { - StartFreeForAll(); - } - if (gameMode == GameMode.keepAway) - { - StartKeepAway(); - } - if (gameMode == GameMode.obstacleCourse) - { - StartObstacleCourse(); - } - } - + public static GameManager Instance { get; private set; } public enum GameMode { freeForAll, @@ -27,33 +12,63 @@ public class GameManager : MonoBehaviour } public static GameMode gameMode = GameMode.freeForAll; - - public static string map = "Platformer With Headroom"; //is called for in playermanager but should probably be removed. - + public static string map = "Platformer With Headroom"; public static List players = new List(); - public Vector2 spawnPosition; - private void StartFreeForAll() + private Dictionary playerLives = new Dictionary(); + public int maxLives = 3; + + private void Start() + { + StartGame(); + } + + public void StartGame() { foreach (GameObject player in players) { + if (gameMode == GameMode.freeForAll) + { + playerLives[player] = maxLives; + } + else + { + playerLives[player] = 1; + } player.transform.position = spawnPosition; } } - private void StartKeepAway() + public void PlayerDied(GameObject player) { - foreach (GameObject player in players) + if (gameMode == GameMode.freeForAll) { - player.transform.position = spawnPosition; + playerLives[player]--; + if (playerLives[player] <= 0) + { + GameOver(player); + } + else + { + RespawnPlayer(player); + } } } - private void StartObstacleCourse() + + private void RespawnPlayer(GameObject player) { - foreach (GameObject player in players) + RespawnOnTriggerEnter respawnScript = player.GetComponent(); + if (respawnScript != null) { - player.transform.position = spawnPosition; + player.transform.position = respawnScript.spawnPoint; + player.GetComponent().ResetDamage(); } } + + private void GameOver(GameObject player) + { + // Disable player controls and show game over screen + player.SetActive(false); + } } diff --git a/Assets/Scripts/PlayerLives.cs b/Assets/Scripts/PlayerLives.cs deleted file mode 100644 index dde2cd0..0000000 --- a/Assets/Scripts/PlayerLives.cs +++ /dev/null @@ -1,56 +0,0 @@ -using Unity.VisualScripting; -using UnityEngine; - -public class PlayerLives : MonoBehaviour -{ - public int maxLives = 3; - public int currentLives; - - private void Start() - { - if (GameManager.gameMode == GameManager.GameMode.freeForAll) - { - currentLives = maxLives; - } - if (GameManager.gameMode == GameManager.GameMode.keepAway) - { - currentLives = 1; - } - if (GameManager.gameMode == GameManager.GameMode.obstacleCourse) - { - currentLives = 1; - } - //add more gamemodes and their lives here - } - public void PlayerDied() - { - if (GameManager.gameMode == GameManager.GameMode.freeForAll) - { - currentLives--; - if (currentLives <= 0) - { - //add Game over sequence; - } - else - { - RespawnPlayer(); - } - } - if (GameManager.gameMode == GameManager.GameMode.keepAway) - { - - } - if (GameManager.gameMode == GameManager.GameMode.obstacleCourse) - { - - } - } - private void RespawnPlayer() - { - RespawnOnTriggerEnter respawnScript = GetComponent(); - if (respawnScript != null) - { - transform.position = respawnScript.spawnPoint; - } - } -} diff --git a/Assets/Scripts/PlayerLives.cs.meta b/Assets/Scripts/PlayerLives.cs.meta deleted file mode 100644 index d13c985..0000000 --- a/Assets/Scripts/PlayerLives.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 207bf152528fc954cb2c73fd9d62c9b0 \ No newline at end of file diff --git a/Assets/Scripts/PlayerManager.cs b/Assets/Scripts/PlayerManager.cs index 545c9eb..6907d86 100644 --- a/Assets/Scripts/PlayerManager.cs +++ b/Assets/Scripts/PlayerManager.cs @@ -13,6 +13,8 @@ public class PlayerManager : MonoBehaviour public GameObject playerSelect; + private bool gameStarted = false; + private void Awake() { Init(); @@ -26,9 +28,15 @@ public class PlayerManager : MonoBehaviour private void OnPlayerJoined(PlayerInput playerInput) { - print("Player joined"); - //playerInput.transform.SetParent(transform); + if (gameStarted) + { + Destroy(playerInput.gameObject); + return; + } + + Debug.Log("Player joined"); + DontDestroyOnLoad(playerInput.gameObject); PlayerJoinCard card = PlayerCardCreator.Instance.CreateCard(); @@ -44,7 +52,7 @@ public class PlayerManager : MonoBehaviour { Destroy(playerInput.gameObject); GameManager.players.Remove(playerInput.gameObject); - print("Player left"); + Debug.Log("Player left"); } private void Init() @@ -55,13 +63,15 @@ public class PlayerManager : MonoBehaviour } else { - print("A PlayerManager already exists."); + Debug.Log("A PlayerManager already exists."); Destroy(this.gameObject); } } public void StartGame() { + gameStarted = true; + Debug.Log("Game started"); HubManager.Instance.LoadScene(GameManager.map); } @@ -78,7 +88,7 @@ public class PlayerManager : MonoBehaviour } private void ApplyColor(GameObject obj, Color color) - { + { if (obj.TryGetComponent(out _)) { obj.GetComponent().color = color; diff --git a/Assets/Scripts/Punch.cs b/Assets/Scripts/Punch.cs index e066cd0..d94c3d9 100644 --- a/Assets/Scripts/Punch.cs +++ b/Assets/Scripts/Punch.cs @@ -6,6 +6,7 @@ using UnityEngine.InputSystem; public class Punch : MonoBehaviour { public bool cancelable = true; + public static event System.Action OnPlayerPunched; [SerializeField] private BoxCollider2D hurtbox; @@ -30,6 +31,7 @@ public class Punch : MonoBehaviour GetComponent().Punch(); DisableCancellation(); GetComponent().maxSpeedOverride = 1f; + OnPlayerPunched?.Invoke(gameObject); } public void EnableHurtbox() diff --git a/Assets/Scripts/TerminalVelocity.cs b/Assets/Scripts/TerminalVelocity.cs deleted file mode 100644 index b2bfce6..0000000 --- a/Assets/Scripts/TerminalVelocity.cs +++ /dev/null @@ -1,21 +0,0 @@ -using UnityEngine; - -[RequireComponent(typeof(Rigidbody2D))] -public class TerminalVelocity : MonoBehaviour -{ - [SerializeField] private float terminalVelocity = -10f; - private Rigidbody2D rb; - - private void Start() - { - rb = GetComponent(); - } - - private void FixedUpdate() - { - if (rb.linearVelocity.y < terminalVelocity) - { - rb.linearVelocity = new Vector2(rb.linearVelocity.x, terminalVelocity); - } - } -} diff --git a/Assets/Scripts/TerminalVelocity.cs.meta b/Assets/Scripts/TerminalVelocity.cs.meta deleted file mode 100644 index 7580dae..0000000 --- a/Assets/Scripts/TerminalVelocity.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 36267595aa66046ac9e2e140190bbc16 \ No newline at end of file diff --git a/Assets/Scripts/UseItem.cs b/Assets/Scripts/UseItem.cs index 3ac81ab..ecb26ca 100644 --- a/Assets/Scripts/UseItem.cs +++ b/Assets/Scripts/UseItem.cs @@ -2,15 +2,57 @@ using UnityEngine; public class UseItem : MonoBehaviour { - // Start is called once before the first execution of Update after the MonoBehaviour is created - void Start() - { - - } + private GameObject heldItem; + private bool isHoldingItem = false; - // Update is called once per frame void Update() { - + if (isHoldingItem) + { + heldItem.transform.position = transform.position; + } + } + + private void OnTriggerEnter2D(Collider2D collision) + { + if (collision.CompareTag("Item") && !isHoldingItem) + { + PickUpItem(collision.gameObject); + } + } + + private void PickUpItem(GameObject item) + { + heldItem = item; + isHoldingItem = true; + item.GetComponent().enabled = false; + } + + public void DropItem() + { + if (isHoldingItem) + { + heldItem.GetComponent().enabled = true; + heldItem = null; + isHoldingItem = false; + } + } + + private void OnEnable() + { + Punch.OnPlayerPunched += HandlePlayerPunched; + } + + private void OnDisable() + { + Punch.OnPlayerPunched -= HandlePlayerPunched; + } + + private void HandlePlayerPunched(GameObject punchedPlayer) + { + if (punchedPlayer == gameObject) + { + DropItem(); + } } }