From 5afbf04037a3aa70e1c4b962adb8f5887bc493bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20P=C4=9Bnkava?= Date: Wed, 6 Aug 2025 00:55:06 +0200 Subject: [PATCH] HealthBar --- .../Prefabs/Object_Prefabs/Campfire.prefab | 14 +- .../Assets/Prefabs/PlayerContainer.prefab | 149 ++++++++++++++++++ .../Assets/Scripts/Player/PlayerMovement.cs | 26 +++ .../Assets/Scripts/Player/PlayerSkillTree.cs | 23 +++ 4 files changed, 206 insertions(+), 6 deletions(-) diff --git a/3D blobici/Assets/Prefabs/Object_Prefabs/Campfire.prefab b/3D blobici/Assets/Prefabs/Object_Prefabs/Campfire.prefab index 9d64937..0a8cf42 100644 --- a/3D blobici/Assets/Prefabs/Object_Prefabs/Campfire.prefab +++ b/3D blobici/Assets/Prefabs/Object_Prefabs/Campfire.prefab @@ -9,7 +9,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 8408032505180852164} - - component: {fileID: 2872484363306069477} + - component: {fileID: 6032689825102274941} m_Layer: 0 m_Name: Campfire m_TagString: Untagged @@ -33,8 +33,8 @@ Transform: - {fileID: 5887615839681625652} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!65 &2872484363306069477 -BoxCollider: +--- !u!136 &6032689825102274941 +CapsuleCollider: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} @@ -51,9 +51,11 @@ BoxCollider: m_IsTrigger: 0 m_ProvidesContacts: 0 m_Enabled: 1 - serializedVersion: 3 - m_Size: {x: 19.52797, y: 9.579633, z: 19.593073} - m_Center: {x: -0.19529533, y: 4.7055793, z: -0.6103029} + serializedVersion: 2 + m_Radius: 8.34 + m_Height: 8.941787 + m_Direction: 1 + m_Center: {x: 0, y: 3.9708934, z: 0} --- !u!1001 &6214890609959950815 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/3D blobici/Assets/Prefabs/PlayerContainer.prefab b/3D blobici/Assets/Prefabs/PlayerContainer.prefab index 9e6416c..09b9be0 100644 --- a/3D blobici/Assets/Prefabs/PlayerContainer.prefab +++ b/3D blobici/Assets/Prefabs/PlayerContainer.prefab @@ -13,6 +13,7 @@ GameObject: - component: {fileID: 1030185587407834460} - component: {fileID: 1542319922315403782} - component: {fileID: 4745202387735708878} + - component: {fileID: 7540455923916979795} m_Layer: 0 m_Name: UI Canvas m_TagString: Untagged @@ -35,6 +36,7 @@ RectTransform: - {fileID: 8991658504906099272} - {fileID: 1831891111370080197} - {fileID: 3235429507807963288} + - {fileID: 6559853192459065261} m_Father: {fileID: 1302341320372733696} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} @@ -129,6 +131,14 @@ MonoBehaviour: - {fileID: 2718716012460538970, guid: be66b8bfca2b02845accf87205f3b53b, type: 3} - {fileID: 2718716012460538970, guid: 2a7de02be9763204180603fa87749453, type: 3} cardsParent: {fileID: 1831891111370080197} +--- !u!222 &7540455923916979795 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1246198998110393468} + m_CullTransparentMesh: 1 --- !u!1 &1910830737072409311 GameObject: m_ObjectHideFlags: 0 @@ -408,6 +418,144 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 3041196932494545810} m_CullTransparentMesh: 1 +--- !u!1 &3601505735882174674 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 6559853192459065261} + - component: {fileID: 8817225252079022943} + - component: {fileID: 4603696628922152095} + m_Layer: 0 + m_Name: HealtBar + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &6559853192459065261 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3601505735882174674} + 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} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 8807664616337023072} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 1, y: 1} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: -700, y: -250} + m_SizeDelta: {x: 700, y: 150} + m_Pivot: {x: 0, y: 0} +--- !u!222 &8817225252079022943 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3601505735882174674} + m_CullTransparentMesh: 1 +--- !u!114 &4603696628922152095 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3601505735882174674} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_text: '0 + +' + m_isRightToLeft: 0 + m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_fontSharedMaterials: [] + m_fontMaterial: {fileID: 0} + m_fontMaterials: [] + m_fontColor32: + serializedVersion: 2 + rgba: 4294967295 + m_fontColor: {r: 1, g: 1, b: 1, a: 1} + m_enableVertexGradient: 0 + m_colorMode: 3 + m_fontColorGradient: + topLeft: {r: 1, g: 1, b: 1, a: 1} + topRight: {r: 1, g: 1, b: 1, a: 1} + bottomLeft: {r: 1, g: 1, b: 1, a: 1} + bottomRight: {r: 1, g: 1, b: 1, a: 1} + m_fontColorGradientPreset: {fileID: 0} + m_spriteAsset: {fileID: 0} + m_tintAllSprites: 0 + m_StyleSheet: {fileID: 0} + m_TextStyleHashCode: -1183493901 + m_overrideHtmlColors: 0 + m_faceColor: + serializedVersion: 2 + rgba: 4294967295 + m_fontSize: 144 + m_fontSizeBase: 144 + m_fontWeight: 400 + m_enableAutoSizing: 0 + m_fontSizeMin: 71.21 + m_fontSizeMax: 72 + m_fontStyle: 0 + m_HorizontalAlignment: 1 + m_VerticalAlignment: 256 + m_textAlignment: 65535 + m_characterSpacing: 0 + m_wordSpacing: 0 + m_lineSpacing: 0 + m_lineSpacingMax: 0 + m_paragraphSpacing: 0 + m_charWidthMaxAdj: 0 + m_TextWrappingMode: 1 + m_wordWrappingRatios: 0.4 + m_overflowMode: 0 + m_linkedTextComponent: {fileID: 0} + parentLinkedComponent: {fileID: 0} + m_enableKerning: 0 + m_ActiveFontFeatures: 6e72656b + m_enableExtraPadding: 0 + checkPaddingRequired: 0 + m_isRichText: 1 + m_EmojiFallbackSupport: 1 + m_parseCtrlCharacters: 1 + m_isOrthographic: 1 + m_isCullingEnabled: 0 + m_horizontalMapping: 0 + m_verticalMapping: 0 + m_uvLineOffset: 0 + m_geometrySortingOrder: 0 + m_IsTextObjectScaleStatic: 0 + m_VertexBufferAutoSizeReduction: 0 + m_useMaxVisibleDescender: 1 + m_pageToDisplay: 1 + m_margin: {x: 0, y: 0, z: 0, w: 0} + m_isUsingLegacyAnimationComponent: 0 + m_isVolumetricText: 0 + m_hasFontAssetChanged: 0 + m_baseMaterial: {fileID: 0} + m_maskOffset: {x: 0, y: 0, z: 0, w: 0} --- !u!1 &3653993672432327505 GameObject: m_ObjectHideFlags: 0 @@ -458,6 +606,7 @@ MonoBehaviour: sprintSpeed: 10 gravity: -9.81 rotationSpeed: 10 + healthBar: {fileID: 4603696628922152095} --- !u!143 &1781483820777133190 CharacterController: m_ObjectHideFlags: 0 diff --git a/3D blobici/Assets/Scripts/Player/PlayerMovement.cs b/3D blobici/Assets/Scripts/Player/PlayerMovement.cs index df5bba8..73213c6 100644 --- a/3D blobici/Assets/Scripts/Player/PlayerMovement.cs +++ b/3D blobici/Assets/Scripts/Player/PlayerMovement.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using UnityEngine; +using TMPro; public class PlayerMovement : MonoBehaviour { @@ -11,15 +12,24 @@ public class PlayerMovement : MonoBehaviour private CharacterController controller; private Vector3 velocity; private PlayerSkillTree PlayerSkills; + private int health; + private int maxHealth; + [Header ("Health")] + [SerializeField] private TMP_Text healthBar; private void Awake() { PlayerSkills = PlayerSkillTree.Instance; + maxHealth = PlayerSkills.MaxHealth; + health = PlayerSkills.GetHealth(); + + Debug.Assert(maxHealth > 0); } void Start() { controller = GetComponent(); + healthBar.text = GetHealthText(); } private void Update() @@ -40,6 +50,17 @@ public class PlayerMovement : MonoBehaviour Debug.Log("Potion not available!"); } } + + // TEMPORARY!!! LATER USED FOR COLISIONS + + if (Input.GetKeyDown(KeyCode.F)) + { + PlayerSkills.SetHealth(-20); + health = PlayerSkills.GetHealth(); + + healthBar.text = GetHealthText(); + } + } void FixedUpdate() @@ -95,4 +116,9 @@ public class PlayerMovement : MonoBehaviour { return PlayerSkills.GetPlayerSkills(); } + + private string GetHealthText() + { + return (health.ToString() + " / " + maxHealth.ToString()); + } } \ No newline at end of file diff --git a/3D blobici/Assets/Scripts/Player/PlayerSkillTree.cs b/3D blobici/Assets/Scripts/Player/PlayerSkillTree.cs index dfbec7c..5567969 100644 --- a/3D blobici/Assets/Scripts/Player/PlayerSkillTree.cs +++ b/3D blobici/Assets/Scripts/Player/PlayerSkillTree.cs @@ -9,6 +9,10 @@ public class PlayerSkillTree private static PlayerSkillTree _instance; private Dictionary potionHandlers = new Dictionary(); + private int health = 100; + private int maxHealth = 100; + + public int MaxHealth { get => maxHealth; set => maxHealth = value; } public static PlayerSkillTree Instance { @@ -81,6 +85,25 @@ public class PlayerSkillTree return false; } + public int GetHealth() { return health; } + + public void SetHealth(int value) + { + if(value + health > maxHealth) + { + health = maxHealth; + return; + } + + if(health + value < 0) + { + health = 0; + return; + } + + health += value; + } + public bool IsSkillUnlocked(Skills skill) {