Script reorganization and UseItem created
This commit is contained in:
@@ -18,7 +18,6 @@ GameObject:
|
|||||||
- component: {fileID: 1169583671018505929}
|
- component: {fileID: 1169583671018505929}
|
||||||
- component: {fileID: 2344147968324054325}
|
- component: {fileID: 2344147968324054325}
|
||||||
- component: {fileID: 8121950008105218343}
|
- component: {fileID: 8121950008105218343}
|
||||||
- component: {fileID: 3303109471384786876}
|
|
||||||
- component: {fileID: 2813634395235005482}
|
- component: {fileID: 2813634395235005482}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: Bones
|
m_Name: Bones
|
||||||
@@ -137,6 +136,7 @@ MonoBehaviour:
|
|||||||
force: 200
|
force: 200
|
||||||
damage: 0
|
damage: 0
|
||||||
maxDamage: 1000
|
maxDamage: 1000
|
||||||
|
healthBar: {fileID: 0}
|
||||||
--- !u!114 &5200627265685207284
|
--- !u!114 &5200627265685207284
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@@ -262,19 +262,6 @@ BoxCollider2D:
|
|||||||
m_AutoTiling: 0
|
m_AutoTiling: 0
|
||||||
m_Size: {x: 0.4505486, y: 1.9437933}
|
m_Size: {x: 0.4505486, y: 1.9437933}
|
||||||
m_EdgeRadius: 0
|
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
|
--- !u!114 &2813634395235005482
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -127,6 +127,18 @@ PrefabInstance:
|
|||||||
serializedVersion: 3
|
serializedVersion: 3
|
||||||
m_TransformParent: {fileID: 0}
|
m_TransformParent: {fileID: 0}
|
||||||
m_Modifications:
|
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}
|
- target: {fileID: 7581121400164956810, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3}
|
||||||
propertyPath: m_Name
|
propertyPath: m_Name
|
||||||
value: Player Select
|
value: Player Select
|
||||||
@@ -176,6 +188,17 @@ PrefabInstance:
|
|||||||
m_AddedGameObjects: []
|
m_AddedGameObjects: []
|
||||||
m_AddedComponents: []
|
m_AddedComponents: []
|
||||||
m_SourcePrefab: {fileID: 100100000, guid: 52622bff9e983494eb206a8735b4dbbf, type: 3}
|
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
|
--- !u!1660057539 &9223372036854775807
|
||||||
SceneRoots:
|
SceneRoots:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -10,8 +10,12 @@ public class Damageable : MonoBehaviour
|
|||||||
public float maxDamage = 1000f;
|
public float maxDamage = 1000f;
|
||||||
public HealthBar healthBar;
|
public HealthBar healthBar;
|
||||||
|
|
||||||
|
private GameManager gameManager;
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
|
gameManager = GameManager.Instance;
|
||||||
|
|
||||||
if (healthBar != null)
|
if (healthBar != null)
|
||||||
{
|
{
|
||||||
healthBar.SetMaxHealth(maxDamage);
|
healthBar.SetMaxHealth(maxDamage);
|
||||||
@@ -78,10 +82,9 @@ public class Damageable : MonoBehaviour
|
|||||||
|
|
||||||
private void Die()
|
private void Die()
|
||||||
{
|
{
|
||||||
PlayerLives playerLives = GetComponent<PlayerLives>(); //add death animation trigger
|
if (gameManager != null)
|
||||||
if (playerLives != null)
|
|
||||||
{
|
{
|
||||||
playerLives.PlayerDied();
|
gameManager.PlayerDied(gameObject); //add death animation trigger
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,22 +3,7 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class GameManager : MonoBehaviour
|
public class GameManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
public void StartGame()
|
public static GameManager Instance { get; private set; }
|
||||||
{
|
|
||||||
if (gameMode == GameMode.freeForAll)
|
|
||||||
{
|
|
||||||
StartFreeForAll();
|
|
||||||
}
|
|
||||||
if (gameMode == GameMode.keepAway)
|
|
||||||
{
|
|
||||||
StartKeepAway();
|
|
||||||
}
|
|
||||||
if (gameMode == GameMode.obstacleCourse)
|
|
||||||
{
|
|
||||||
StartObstacleCourse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum GameMode
|
public enum GameMode
|
||||||
{
|
{
|
||||||
freeForAll,
|
freeForAll,
|
||||||
@@ -27,33 +12,63 @@ public class GameManager : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static GameMode gameMode = GameMode.freeForAll;
|
public static GameMode gameMode = GameMode.freeForAll;
|
||||||
|
public static string map = "Platformer With Headroom";
|
||||||
public static string map = "Platformer With Headroom"; //is called for in playermanager but should probably be removed.
|
|
||||||
|
|
||||||
public static List<GameObject> players = new List<GameObject>();
|
public static List<GameObject> players = new List<GameObject>();
|
||||||
|
|
||||||
public Vector2 spawnPosition;
|
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)
|
foreach (GameObject player in players)
|
||||||
{
|
{
|
||||||
|
if (gameMode == GameMode.freeForAll)
|
||||||
|
{
|
||||||
|
playerLives[player] = maxLives;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
playerLives[player] = 1;
|
||||||
|
}
|
||||||
player.transform.position = spawnPosition;
|
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;
|
public GameObject playerSelect;
|
||||||
|
|
||||||
|
private bool gameStarted = false;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
Init();
|
Init();
|
||||||
@@ -26,9 +28,15 @@ public class PlayerManager : MonoBehaviour
|
|||||||
|
|
||||||
private void OnPlayerJoined(PlayerInput playerInput)
|
private void OnPlayerJoined(PlayerInput playerInput)
|
||||||
{
|
{
|
||||||
print("Player joined");
|
|
||||||
|
|
||||||
//playerInput.transform.SetParent(transform);
|
//playerInput.transform.SetParent(transform);
|
||||||
|
if (gameStarted)
|
||||||
|
{
|
||||||
|
Destroy(playerInput.gameObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug.Log("Player joined");
|
||||||
|
|
||||||
DontDestroyOnLoad(playerInput.gameObject);
|
DontDestroyOnLoad(playerInput.gameObject);
|
||||||
|
|
||||||
PlayerJoinCard card = PlayerCardCreator.Instance.CreateCard();
|
PlayerJoinCard card = PlayerCardCreator.Instance.CreateCard();
|
||||||
@@ -44,7 +52,7 @@ public class PlayerManager : MonoBehaviour
|
|||||||
{
|
{
|
||||||
Destroy(playerInput.gameObject);
|
Destroy(playerInput.gameObject);
|
||||||
GameManager.players.Remove(playerInput.gameObject);
|
GameManager.players.Remove(playerInput.gameObject);
|
||||||
print("Player left");
|
Debug.Log("Player left");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Init()
|
private void Init()
|
||||||
@@ -55,13 +63,15 @@ public class PlayerManager : MonoBehaviour
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
print("A PlayerManager already exists.");
|
Debug.Log("A PlayerManager already exists.");
|
||||||
Destroy(this.gameObject);
|
Destroy(this.gameObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartGame()
|
public void StartGame()
|
||||||
{
|
{
|
||||||
|
gameStarted = true;
|
||||||
|
Debug.Log("Game started");
|
||||||
HubManager.Instance.LoadScene(GameManager.map);
|
HubManager.Instance.LoadScene(GameManager.map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +88,7 @@ public class PlayerManager : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void ApplyColor(GameObject obj, Color color)
|
private void ApplyColor(GameObject obj, Color color)
|
||||||
{
|
{
|
||||||
if (obj.TryGetComponent<SpriteRenderer>(out _))
|
if (obj.TryGetComponent<SpriteRenderer>(out _))
|
||||||
{
|
{
|
||||||
obj.GetComponent<SpriteRenderer>().color = color;
|
obj.GetComponent<SpriteRenderer>().color = color;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using UnityEngine.InputSystem;
|
|||||||
public class Punch : MonoBehaviour
|
public class Punch : MonoBehaviour
|
||||||
{
|
{
|
||||||
public bool cancelable = true;
|
public bool cancelable = true;
|
||||||
|
public static event System.Action<GameObject> OnPlayerPunched;
|
||||||
|
|
||||||
[SerializeField] private BoxCollider2D hurtbox;
|
[SerializeField] private BoxCollider2D hurtbox;
|
||||||
|
|
||||||
@@ -30,6 +31,7 @@ public class Punch : MonoBehaviour
|
|||||||
GetComponent<AnimationPlayer>().Punch();
|
GetComponent<AnimationPlayer>().Punch();
|
||||||
DisableCancellation();
|
DisableCancellation();
|
||||||
GetComponent<PlayerMovement>().maxSpeedOverride = 1f;
|
GetComponent<PlayerMovement>().maxSpeedOverride = 1f;
|
||||||
|
OnPlayerPunched?.Invoke(gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EnableHurtbox()
|
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
|
public class UseItem : MonoBehaviour
|
||||||
{
|
{
|
||||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
private GameObject heldItem;
|
||||||
void Start()
|
private bool isHoldingItem = false;
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update is called once per frame
|
|
||||||
void Update()
|
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