Script reorganization and UseItem created
This commit is contained in:
@@ -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<PlayerLives>(); //add death animation trigger
|
||||
if (playerLives != null)
|
||||
if (gameManager != null)
|
||||
{
|
||||
playerLives.PlayerDied();
|
||||
gameManager.PlayerDied(gameObject); //add death animation trigger
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<GameObject> players = new List<GameObject>();
|
||||
|
||||
public Vector2 spawnPosition;
|
||||
|
||||
private void StartFreeForAll()
|
||||
private Dictionary<GameObject, int> playerLives = new Dictionary<GameObject, int>();
|
||||
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<RespawnOnTriggerEnter>();
|
||||
if (respawnScript != null)
|
||||
{
|
||||
player.transform.position = spawnPosition;
|
||||
player.transform.position = respawnScript.spawnPoint;
|
||||
player.GetComponent<Damageable>().ResetDamage();
|
||||
}
|
||||
}
|
||||
|
||||
private void GameOver(GameObject player)
|
||||
{
|
||||
// Disable player controls and show game over screen
|
||||
player.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<RespawnOnTriggerEnter>();
|
||||
if (respawnScript != null)
|
||||
{
|
||||
transform.position = respawnScript.spawnPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 207bf152528fc954cb2c73fd9d62c9b0
|
||||
@@ -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<SpriteRenderer>(out _))
|
||||
{
|
||||
obj.GetComponent<SpriteRenderer>().color = color;
|
||||
|
||||
@@ -6,6 +6,7 @@ using UnityEngine.InputSystem;
|
||||
public class Punch : MonoBehaviour
|
||||
{
|
||||
public bool cancelable = true;
|
||||
public static event System.Action<GameObject> OnPlayerPunched;
|
||||
|
||||
[SerializeField] private BoxCollider2D hurtbox;
|
||||
|
||||
@@ -30,6 +31,7 @@ public class Punch : MonoBehaviour
|
||||
GetComponent<AnimationPlayer>().Punch();
|
||||
DisableCancellation();
|
||||
GetComponent<PlayerMovement>().maxSpeedOverride = 1f;
|
||||
OnPlayerPunched?.Invoke(gameObject);
|
||||
}
|
||||
|
||||
public void EnableHurtbox()
|
||||
|
||||
@@ -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<Rigidbody2D>();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (rb.linearVelocity.y < terminalVelocity)
|
||||
{
|
||||
rb.linearVelocity = new Vector2(rb.linearVelocity.x, terminalVelocity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 36267595aa66046ac9e2e140190bbc16
|
||||
@@ -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<Collider2D>().enabled = false;
|
||||
}
|
||||
|
||||
public void DropItem()
|
||||
{
|
||||
if (isHoldingItem)
|
||||
{
|
||||
heldItem.GetComponent<Collider2D>().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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user