Health bar and player management refined

This commit is contained in:
djkellerman
2025-03-06 01:27:42 -05:00
parent 47314da759
commit bd3e57428f
18 changed files with 2070 additions and 213 deletions

View File

@@ -229,6 +229,9 @@ AnimatorStateMachine:
- serializedVersion: 1
m_State: {fileID: -6105423640591385822}
m_Position: {x: -90, y: 230, z: 0}
- serializedVersion: 1
m_State: {fileID: 2419565110853745188}
m_Position: {x: -55, y: 295, z: 0}
m_ChildStateMachines: []
m_AnyStateTransitions:
- {fileID: 5263795009836994554}
@@ -413,6 +416,32 @@ AnimatorState:
m_MirrorParameter:
m_CycleOffsetParameter:
m_TimeParameter:
--- !u!1102 &2419565110853745188
AnimatorState:
serializedVersion: 6
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Die
m_Speed: 1
m_CycleOffset: 0
m_Transitions: []
m_StateMachineBehaviours: []
m_Position: {x: 50, y: 50, z: 0}
m_IKOnFeet: 0
m_WriteDefaultValues: 1
m_Mirror: 0
m_SpeedParameterActive: 0
m_MirrorParameterActive: 0
m_CycleOffsetParameterActive: 0
m_TimeParameterActive: 0
m_Motion: {fileID: 7400000, guid: f81637d914d3dde469fd4b25e1d67a76, type: 2}
m_Tag:
m_SpeedParameter:
m_MirrorParameter:
m_CycleOffsetParameter:
m_TimeParameter:
--- !u!1102 &2500419510660092995
AnimatorState:
serializedVersion: 6

1653
Assets/Animations/Die.anim Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f81637d914d3dde469fd4b25e1d67a76
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 7400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,3 +1,4 @@
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
@@ -6,7 +7,6 @@ using UnityEngine.UI;
public class HubManager : MonoBehaviour
{
public static HubManager Instance;
public GameObject hubCamera;
public GameObject gameButtonsParent;
@@ -52,7 +52,8 @@ public class HubManager : MonoBehaviour
{
UnloadGameScene();
ChangeGameButtonsInteractability(true);
foreach (GameObject player in GameManager.players)
foreach (GameObject player in GameManager.players.ToList())
{
GameManager.players.Remove(player);
Destroy(player);

View File

@@ -10,7 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 210773656378718009}
- component: {fileID: 210773656378718014}
m_Layer: 0
m_Layer: 7
m_Name: Death
m_TagString: Untagged
m_Icon: {fileID: 0}
@@ -97,7 +97,7 @@ GameObject:
m_Component:
- component: {fileID: 210773656934484430}
- component: {fileID: 210773656934484428}
m_Layer: 0
m_Layer: 7
m_Name: Health
m_TagString: Untagged
m_Icon: {fileID: 0}
@@ -184,21 +184,20 @@ GameObject:
m_Component:
- component: {fileID: 210773657359985456}
- component: {fileID: 7422290698914274434}
m_Layer: 0
m_Layer: 7
m_Name: Healthbar
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &210773657359985456
Transform:
--- !u!224 &210773657359985456
RectTransform:
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}
@@ -211,6 +210,11 @@ Transform:
- {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
@@ -231,6 +235,7 @@ MonoBehaviour:
deathVisual: {fileID: 210773656378718008}
smoothSpeed: 0.1
text: {fileID: 210773657480500664}
player: {fileID: 0}
--- !u!1 &210773657480500666
GameObject:
m_ObjectHideFlags: 0
@@ -242,7 +247,7 @@ GameObject:
- component: {fileID: 210773657480500667}
- component: {fileID: 210773657480500665}
- component: {fileID: 210773657480500664}
m_Layer: 0
m_Layer: 7
m_Name: Text
m_TagString: Untagged
m_Icon: {fileID: 0}
@@ -377,7 +382,7 @@ GameObject:
m_Component:
- component: {fileID: 210773657645469775}
- component: {fileID: 210773657645469772}
m_Layer: 0
m_Layer: 7
m_Name: Border
m_TagString: Untagged
m_Icon: {fileID: 0}
@@ -466,7 +471,7 @@ GameObject:
- component: {fileID: 210773657761048654}
- component: {fileID: 210773657761048649}
- component: {fileID: 210773657761048648}
m_Layer: 0
m_Layer: 7
m_Name: Number
m_TagString: Untagged
m_Icon: {fileID: 0}
@@ -566,7 +571,7 @@ GameObject:
m_Component:
- component: {fileID: 170974732944509535}
- component: {fileID: 8850558015437586293}
m_Layer: 0
m_Layer: 7
m_Name: Actual
m_TagString: Untagged
m_Icon: {fileID: 0}

View File

@@ -100,7 +100,7 @@ GameObject:
- component: {fileID: -5768905494704828194}
m_Layer: 0
m_Name: Player
m_TagString: Player
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
@@ -411,3 +411,5 @@ MonoBehaviour:
m_EditorClassIdentifier:
teleportPoint: {x: 0, y: 0}
teleportTag: Teleporter
playerTag: Player
isPlatform: 1

View File

@@ -304,6 +304,133 @@ Transform:
m_CorrespondingSourceObject: {fileID: 5794044177371189323, guid: 5ce441c5fe33344a292ef80ef22aaffd, type: 3}
m_PrefabInstance: {fileID: 612903695}
m_PrefabAsset: {fileID: 0}
--- !u!1001 &646086199
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 1703536538}
m_Modifications:
- target: {fileID: 210773656378718008, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Layer
value: 5
objectReference: {fileID: 0}
- target: {fileID: 210773656934484425, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Layer
value: 5
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.x
value: -0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.y
value: -0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.z
value: -0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_AnchoredPosition.x
value: 9
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_AnchoredPosition.y
value: 11
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985459, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Name
value: Healthbar
objectReference: {fileID: 0}
- target: {fileID: 210773657359985459, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Layer
value: 5
objectReference: {fileID: 0}
- target: {fileID: 210773657480500666, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Layer
value: 5
objectReference: {fileID: 0}
- target: {fileID: 210773657645469774, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Layer
value: 5
objectReference: {fileID: 0}
- target: {fileID: 210773657761048650, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Layer
value: 5
objectReference: {fileID: 0}
- target: {fileID: 904379579236604940, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Layer
value: 5
objectReference: {fileID: 0}
- target: {fileID: 1847290122477952015, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_PresetInfoIsWorld
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7422290698914274434, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: player
value:
objectReference: {fileID: 1375850422501237868, guid: 1fd31b88e5609482ab7e8ab16a932947, type: 3}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents:
- targetCorrespondingSourceObject: {fileID: 210773657359985459, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
insertIndex: -1
addedObject: {fileID: 646086201}
m_SourcePrefab: {fileID: 100100000, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
--- !u!1 &646086200 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 210773657359985459, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
m_PrefabInstance: {fileID: 646086199}
m_PrefabAsset: {fileID: 0}
--- !u!114 &646086201
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 646086200}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a80b536f823e50142b142b4e0b64ea97, type: 3}
m_Name:
m_EditorClassIdentifier:
healthBarPrefab: {fileID: 646086200}
--- !u!224 &646086202 stripped
RectTransform:
m_CorrespondingSourceObject: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
m_PrefabInstance: {fileID: 646086199}
m_PrefabAsset: {fileID: 0}
--- !u!1 &708097981
GameObject:
m_ObjectHideFlags: 0
@@ -888,87 +1015,90 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: eea49ffcaef404cf0b373413357af83b, type: 3}
--- !u!1001 &1664848898
PrefabInstance:
--- !u!1 &1703536534
GameObject:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalScale.x
value: 10.396
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalScale.y
value: 10.396
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalScale.z
value: 10.396
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalPosition.x
value: -12.83
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalPosition.y
value: 3.96
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985456, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 210773657359985459, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_Name
value: Healthbar
objectReference: {fileID: 0}
- target: {fileID: 210773657480500664, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_fontAsset
value:
objectReference: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
- target: {fileID: 210773657480500664, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: m_sharedMaterial
value:
objectReference: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
- target: {fileID: 210773657480500664, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
propertyPath: 'm_ActiveFontFeatures.Array.data[0]'
value: 1801810542
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: e2f1088617eeb46c79bf9d357f913fc4, type: 3}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1703536538}
- component: {fileID: 1703536537}
- component: {fileID: 1703536536}
m_Layer: 5
m_Name: HealthCanvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1703536536
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1703536534}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3}
m_Name:
m_EditorClassIdentifier:
m_UiScaleMode: 1
m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1
m_ReferenceResolution: {x: 1920, y: 1080}
m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0
m_PhysicalUnit: 3
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &1703536537
Canvas:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1703536534}
m_Enabled: 1
serializedVersion: 3
m_RenderMode: 0
m_Camera: {fileID: 0}
m_PlaneDistance: 100
m_PixelPerfect: 0
m_ReceivesEvents: 1
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_VertexColorAlwaysGammaSpace: 0
m_AdditionalShaderChannelsFlag: 0
m_UpdateRectTransformForStandalone: 0
m_SortingLayerID: 0
m_SortingOrder: 10000
m_TargetDisplay: 0
--- !u!224 &1703536538
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1703536534}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 646086202}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!1 &1758254987
GameObject:
m_ObjectHideFlags: 0
@@ -4619,6 +4749,7 @@ SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 8682934954266721942}
- {fileID: 1703536538}
- {fileID: 1758254989}
- {fileID: 1335742587}
- {fileID: 2119246465}
@@ -4627,4 +4758,3 @@ SceneRoots:
- {fileID: 421411299}
- {fileID: 708097985}
- {fileID: 854334845}
- {fileID: 1664848898}

View File

@@ -24,7 +24,6 @@ public class AnimationPlayer : MonoBehaviour
{
animator.SetInteger("state", (int)state);
transform.localScale = new Vector3(Mathf.Sign(backwards ? -1 : 1) * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
animator.SetBool("block", block);
}

View File

@@ -1,3 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
@@ -8,19 +10,13 @@ public class Damageable : MonoBehaviour
public float force = 50f;
public float damage = 0f;
public float maxDamage = 1000f;
public HealthBar healthBar;
private GameManager gameManager;
private Animator animator;
private void Start()
{
gameManager = GameManager.Instance;
if (healthBar != null)
{
healthBar.SetMaxHealth(maxDamage);
healthBar.SetHealth(maxDamage - damage);
}
animator = GetComponent<Animator>();
}
private void OnTriggerEnter2D(Collider2D collision)
@@ -55,10 +51,6 @@ public class Damageable : MonoBehaviour
}
damage += actualForce;
damage = Mathf.Clamp(damage, 0f, maxDamage);
if (healthBar != null)
{
healthBar.SetHealth(maxDamage - damage);
}
if (damage >= maxDamage)
{
Die();
@@ -70,10 +62,6 @@ public class Damageable : MonoBehaviour
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * force, ForceMode2D.Force);
damage += force;
damage = Mathf.Clamp(damage, 0f, maxDamage);
if (healthBar != null)
{
healthBar.SetHealth(maxDamage - damage);
}
if (damage >= maxDamage)
{
Die();
@@ -82,12 +70,18 @@ public class Damageable : MonoBehaviour
private void Die()
{
Debug.Log($"{name}: MAKE THIS WORK.");
if (gameManager != null)
{
gameManager.PlayerDied(gameObject); //add death animation trigger
animator.SetTrigger("Die");
StartCoroutine(HandleDeath());
}
}
private IEnumerator HandleDeath()
{
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
gameManager.PlayerDied(gameObject);
}
public void Respawn()
@@ -96,6 +90,7 @@ public class Damageable : MonoBehaviour
if (TryGetComponent<Rigidbody2D>(out var rb))
{
rb.linearVelocity = Vector2.zero;
rb.angularVelocity = 0f;
}
if (TryGetComponent<Damageable>(out var damageable))
{
@@ -106,10 +101,5 @@ public class Damageable : MonoBehaviour
public void ResetDamage()
{
damage = 0f;
if (healthBar != null)
{
healthBar.SetHealth(maxDamage);
}
//transform.localScale = Vector3.one;
}
}

View File

@@ -6,6 +6,10 @@ public class GameManager : MonoBehaviour
public static GameManager Instance { get; private set; }
public int maxLives = 3;
public int currentLives;
public delegate void GameEvent();
public event GameEvent StartGameEvent;
public event GameEvent EndGameEvent;
public static List<GameObject> players = new List<GameObject>();
private void Awake()
{
@@ -26,21 +30,31 @@ public class GameManager : MonoBehaviour
public void StartGame()
{
StartGameEvent?.Invoke();
print("Starting game with mode: " + gameMode + " and map: " + map);
if (gameMode == GameMode.freeForAll)
{
currentLives = maxLives;
StartFreeForAll();
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
if (gameMode == GameMode.keepAway)
{
currentLives = 1;
StartKeepAway();
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
if (gameMode == GameMode.obstacleCourse)
{
currentLives = 1;
StartObstacleCourse();
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
}
@@ -51,38 +65,10 @@ public class GameManager : MonoBehaviour
obstacleCourse
}
public static GameMode gameMode = GameMode.freeForAll;
public static string map = "Platformer With Headroom"; //called for in PlayerManager and should be changed to load from here instead
public static List<GameObject> players = new List<GameObject>();
public static GameMode gameMode = GameMode.freeForAll; // loads a default gamemode as a safety net
public static string map = "Platformer With Headroom"; // loads a default map as a safety net
public Vector2 spawnPosition;
private void StartFreeForAll()
{
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
private void StartKeepAway()
{
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
private void StartObstacleCourse()
{
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
public void PlayerDied(GameObject player)
{
if (gameMode == GameMode.freeForAll)
@@ -119,7 +105,8 @@ public class GameManager : MonoBehaviour
private void GameOver(GameObject player)
{
// Disable player controls and show game over screen
// Add game over screen
player.SetActive(false);
EndGameEvent?.Invoke();
}
}

View File

@@ -1,24 +0,0 @@
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
public Slider slider;
public void SetMaxHealth(float health)
{
if (slider != null)
{
slider.maxValue = health;
slider.value = health;
}
}
public void SetHealth(float health)
{
if (slider != null)
{
slider.value = health;
}
}
}

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 15c35eadf0afb0e42a41e4d6f5350554

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
using UnityEngine;
public class HealthBarManager : MonoBehaviour
{
public GameObject healthBarPrefab;
private Dictionary<GameObject, GameObject> playerHealthBars = new Dictionary<GameObject, GameObject>();
void Start()
{
GameManager.Instance.StartGameEvent += OnGameStart;
GameManager.Instance.EndGameEvent += OnGameEnd;
}
void OnDestroy()
{
GameManager.Instance.StartGameEvent -= OnGameStart;
GameManager.Instance.EndGameEvent -= OnGameEnd;
}
void Update()
{
foreach (var kvp in playerHealthBars)
{
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;
}
}
private void OnGameStart()
{
foreach (GameObject player in GameManager.players)
{
if (!playerHealthBars.ContainsKey(player))
{
GameObject healthBar = Instantiate(healthBarPrefab);
healthBar.GetComponent<TerribleHealthBarScript>().SetPlayer(player);
playerHealthBars[player] = healthBar;
}
}
}
private void OnGameEnd()
{
foreach (var kvp in playerHealthBars)
{
Destroy(kvp.Value);
}
playerHealthBars.Clear();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a80b536f823e50142b142b4e0b64ea97

View File

@@ -5,15 +5,12 @@ using UnityEngine.InputSystem;
public class PlayerManager : MonoBehaviour
{
public static PlayerManager Instance;
public List<PlayerJoinCard> cards;
[SerializeField] private InputActionAsset playerActions;
public List<Color> playerColors;
public GameObject playerSelect;
private bool gameStarted = false;
private bool isStartPressed = false;
private void Awake()
{
@@ -34,17 +31,12 @@ public class PlayerManager : MonoBehaviour
Destroy(playerInput.gameObject);
return;
}
Debug.Log("Player joined");
DontDestroyOnLoad(playerInput.gameObject);
PlayerJoinCard card = PlayerCardCreator.Instance.CreateCard();
card.playerNumber = GameManager.players.Count + 1;
cards.Add(card);
GameManager.players.Add(playerInput.gameObject);
Colorize(GameManager.players.Count - 1);
}
@@ -63,26 +55,33 @@ public class PlayerManager : MonoBehaviour
}
else
{
Debug.Log("A PlayerManager already exists.");
Destroy(this.gameObject);
}
}
public void StartGame()
{
gameStarted = true;
Debug.Log("Game started");
HubManager.Instance.LoadScene(GameManager.map);
if (GameManager.players.Count == 0)
{
return;
}
if (isStartPressed)
{
gameStarted = true;
HubManager.Instance.LoadScene(GameManager.map);
}
else
{
isStartPressed = true;
}
}
private void Colorize(int index)
{
GameObject player = GameManager.players[index];
Color color = playerColors[(GameManager.players.Count - 1) % playerColors.Count];
float tint = Mathf.Floor((GameManager.players.Count - 1) / playerColors.Count);
color = (color + color + Color.white * tint) / (tint + 2);
ApplyColor(player, color);
ApplyColor(cards[GameManager.players.IndexOf(player)].playerPreview, color);
}

View File

@@ -21,45 +21,64 @@ public class TerribleHealthBarScript : MonoBehaviour
private Vector3 targetPosition;
private Color targetActualColor;
private Damageable player;
public GameObject player;
void Start()
{
if (player == null)
{
return;
}
healthScript = player.GetComponent<Damageable>();
initialScale = healthVisual.transform.localScale;
initialPosition = healthVisual.transform.position;
targetScale = initialScale;
targetPosition = initialPosition;
targetActualColor = actualHealthVisual.GetComponent<SpriteRenderer>().color;
if (healthScript == null)
{
return;
}
Initialize();
}
void Update()
{
if (healthScript.gameObject.transform.localScale.x <= 0f)
if (player == null || healthScript == null)
{
transform.localScale = new Vector3(-1 * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
return;
}
else
{
transform.localScale = new Vector3(1 * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}//
float healthRatio = (healthScript.maxDamage - healthScript.damage) / healthScript.maxDamage;
targetActualColor = Color.Lerp(fullDeathColor, fullHealthColor, healthRatio);
targetScale = new Vector3(Mathf.Lerp(0, 1, healthRatio) * initialScale.x, healthVisual.transform.localScale.y, healthVisual.transform.localScale.z);
targetPosition = new Vector3(Mathf.Lerp(-0.5f, 0, healthRatio), healthVisual.transform.localPosition.y, healthVisual.transform.localPosition.z);
text.text = (healthScript.maxDamage - healthScript.damage).ToString() + "/" + healthScript.maxDamage.ToString();
actualHealthVisual.transform.localScale = targetScale;
actualHealthVisual.transform.localPosition = targetPosition;
healthVisual.transform.localScale = Vector3.Lerp(healthVisual.transform.localScale, targetScale, smoothSpeed);
healthVisual.transform.localPosition = Vector3.Lerp(healthVisual.transform.localPosition, targetPosition, smoothSpeed);
actualHealthVisual.GetComponent<SpriteRenderer>().color = Color.Lerp(actualHealthVisual.GetComponent<SpriteRenderer>().color, targetActualColor, smoothSpeed);
deathVisual.GetComponent<SpriteRenderer>().color = Color.Lerp(deathVisual.GetComponent<SpriteRenderer>().color, targetActualColor * 0.5f, smoothSpeed);
healthVisual.GetComponent<SpriteRenderer>().color = subtractionColor;
}
public void SetPlayer(GameObject player)
{
this.player = player;
if (this.player == null)
{
return;
}
healthScript = player.GetComponent<Damageable>();
if (healthScript == null)
{
return;
}
Initialize();
}
private void Initialize()
{
initialScale = healthVisual.transform.localScale;
initialPosition = healthVisual.transform.position;
targetScale = initialScale;
targetPosition = initialPosition;
targetActualColor = actualHealthVisual.GetComponent<SpriteRenderer>().color;
}
}

View File

@@ -18,7 +18,7 @@ TagManager:
- Water
- UI
- Platformer Ground
-
- HealthBar
-
-
-
@@ -68,5 +68,8 @@ TagManager:
- name: Ground
uniqueID: 1556569731
locked: 0
- name: HealthBar
uniqueID: 1570397069
locked: 0
m_RenderingLayers:
- Default