diff --git a/Assets/Scenes/Boot.unity b/Assets/Scenes/Boot.unity index f711965..09ec7a3 100644 --- a/Assets/Scenes/Boot.unity +++ b/Assets/Scenes/Boot.unity @@ -382,6 +382,7 @@ GameObject: m_Component: - component: {fileID: 1350079066} - component: {fileID: 1350079067} + - component: {fileID: 1350079068} m_Layer: 0 m_Name: HUD m_TagString: Untagged @@ -427,6 +428,19 @@ MonoBehaviour: m_PivotReferenceSize: 0 m_Pivot: 0 m_WorldSpaceCollider: {fileID: 0} +--- !u!114 &1350079068 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1350079065} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 2b9d906c9cda89d409b36b2d9dd22164, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::HUDService + uiDocument: {fileID: 1350079067} --- !u!1 &1567913721 GameObject: m_ObjectHideFlags: 0 @@ -502,6 +516,7 @@ MonoBehaviour: m_EditorClassIdentifier: playerJoiner: {fileID: 0} bgm: {fileID: 1072830981} + hudService: {fileID: 1350079068} --- !u!4 &1695492504 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Data/UnitData.cs b/Assets/Scripts/Data/UnitData.cs index 0ee347b..6d1bdf7 100644 --- a/Assets/Scripts/Data/UnitData.cs +++ b/Assets/Scripts/Data/UnitData.cs @@ -7,6 +7,6 @@ namespace Data { public string UnitName; public float MoveSpeed; - public float MaxHP; + public int MaxHP; } } \ No newline at end of file diff --git a/Assets/Scripts/Managers/PlayerJoiner.cs b/Assets/Scripts/Managers/PlayerJoiner.cs index ff92bc0..ead1b21 100644 --- a/Assets/Scripts/Managers/PlayerJoiner.cs +++ b/Assets/Scripts/Managers/PlayerJoiner.cs @@ -1,10 +1,8 @@ using System.Collections.Generic; -using State; using State.GameStateMachine; using State.PawnStateMachine; using UnityEngine; using UnityEngine.InputSystem; -using UnityEngine.InputSystem.UI; namespace Managers { @@ -34,13 +32,18 @@ namespace Managers { Debug.Log("Player joined!"); _players.Add(playerInput); + var player = playerInput.gameObject.GetComponent(); + player.playerIndex = _players.Count - 1; // TODO: Move all of this to a player spawner GameStateMachine.Instance.ChangeState(new PlayLevel()); var character = Instantiate(characterPrefabs[_players.Count - 1]); - var playerStateMachine = playerInput.gameObject.GetComponent(); - playerStateMachine.controlledPawn = character.GetComponent(); - playerInput.gameObject.GetComponent().controlledPawn = character.GetComponent(); + player.controlledPawn = character.GetComponent(); + + var pawnStateMachine = playerInput.gameObject.GetComponent(); + pawnStateMachine.controlledPawn = player.controlledPawn; + + Services.Instance.HUD.Register(player); playerInput.actions.Enable(); } diff --git a/Assets/Scripts/Managers/Services.cs b/Assets/Scripts/Managers/Services.cs index 3fb0817..10ce1da 100644 --- a/Assets/Scripts/Managers/Services.cs +++ b/Assets/Scripts/Managers/Services.cs @@ -1,17 +1,18 @@ +using UI; using UnityEngine; -using UnityEngine.Serialization; namespace Managers { public class Services : MonoBehaviour { public static Services Instance { get; private set; } - public PlayerJoiner PlayerJoiner => playerJoiner; public BGM BGM => bgm; - - [FormerlySerializedAs("inputCoordinator")] [FormerlySerializedAs("inputManager")] [FormerlySerializedAs("inputRouter")] [SerializeField] private PlayerJoiner playerJoiner; + public HUDService HUD => hudService; + + [SerializeField] private PlayerJoiner playerJoiner; [SerializeField] private BGM bgm; + [SerializeField] private HUDService hudService; public void Awake() { diff --git a/Assets/Scripts/Pawn.cs b/Assets/Scripts/Pawn.cs index 51fd68b..d2ca3a8 100644 --- a/Assets/Scripts/Pawn.cs +++ b/Assets/Scripts/Pawn.cs @@ -4,14 +4,15 @@ using UnityEngine; public class Pawn : MonoBehaviour { + public event Action HPUpdated; + public int MaxHP { get; protected set; } + public int CurrentHP { get; protected set; } public Animator Animator => animator; [SerializeField] private UnitData unitData; [SerializeField] private SpriteRenderer spriteRenderer; [SerializeField] private Animator animator; - - protected float MaxHP; - protected float CurrentHP; + protected bool IsDead; private Vector2 _moveInput; @@ -22,10 +23,12 @@ public class Pawn : MonoBehaviour CurrentHP = MaxHP; } - public void TakeDamage(float damage) + public void TakeDamage(int damage) { //animator.Play("HURT"); CurrentHP -= damage; + CurrentHP = Mathf.Clamp(CurrentHP, 0, MaxHP); + HPUpdated?.Invoke(CurrentHP); CheckDeath(); } diff --git a/Assets/Scripts/Player/InputRouter.cs b/Assets/Scripts/Player/InputRouter.cs index d1df23f..2b782c9 100644 --- a/Assets/Scripts/Player/InputRouter.cs +++ b/Assets/Scripts/Player/InputRouter.cs @@ -20,10 +20,13 @@ namespace Player _map = _playerInput.actions.FindActionMap("Player", throwIfNotFound: true); } + private void Update() + { + pawnStateMachine.SetMove(_map["Move"].ReadValue()); + } + private void OnEnable() { - _map["Move"].performed += OnMovePerformed; - _map["Move"].canceled += OnMoveCanceled; _map["Attack"].performed += OnAttackPerformed; _map["Jump"].performed += OnJumpPerformed; _map["Pause"].performed += OnPausePerformed; @@ -31,8 +34,6 @@ namespace Player private void OnDisable() { - _map["Move"].performed -= OnMovePerformed; - _map["Move"].canceled -= OnMoveCanceled; _map["Attack"].performed -= OnAttackPerformed; _map["Jump"].performed -= OnJumpPerformed; _map["Pause"].performed -= OnPausePerformed; @@ -52,15 +53,5 @@ namespace Player { pawnStateMachine.Issue(GameState.Command.Attack); } - - private void OnMoveCanceled(InputAction.CallbackContext obj) - { - pawnStateMachine.SetMove(Vector2.zero); - } - - private void OnMovePerformed(InputAction.CallbackContext obj) - { - pawnStateMachine.SetMove(obj.ReadValue()); - } } } \ No newline at end of file diff --git a/Assets/Scripts/Player/Player.cs b/Assets/Scripts/Player/Player.cs index fb354e6..33e95a3 100644 --- a/Assets/Scripts/Player/Player.cs +++ b/Assets/Scripts/Player/Player.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; using UnityEngine.InputSystem; @@ -6,11 +7,17 @@ namespace Player [RequireComponent(typeof(PlayerInput))] public class Player : MonoBehaviour { + public int playerIndex; public Pawn controlledPawn; private void Awake() { DontDestroyOnLoad(gameObject); } + + public void Possess(Pawn pawn) + { + + } } } diff --git a/Assets/Scripts/State/PawnStateMachine/Attack.cs b/Assets/Scripts/State/PawnStateMachine/Attack.cs index bb6bf03..e42e490 100644 --- a/Assets/Scripts/State/PawnStateMachine/Attack.cs +++ b/Assets/Scripts/State/PawnStateMachine/Attack.cs @@ -8,6 +8,7 @@ namespace State.PawnStateMachine public override void OnEnter(StateMachine machine) { base.OnEnter(machine); + ((PawnStateMachine)StateMachine).controlledPawn.HandleMove(Vector2.zero); StateMachine.StartCoroutine(PlayAttackAnimation()); } diff --git a/Assets/Scripts/UI.meta b/Assets/Scripts/UI.meta new file mode 100644 index 0000000..57c9fb5 --- /dev/null +++ b/Assets/Scripts/UI.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2c601ad143cafd042a99abe0068d953e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UI/HUDService.cs b/Assets/Scripts/UI/HUDService.cs new file mode 100644 index 0000000..fe2d45f --- /dev/null +++ b/Assets/Scripts/UI/HUDService.cs @@ -0,0 +1,37 @@ +using UnityEngine; +using UnityEngine.UIElements; + +namespace UI +{ + public class HUDService : MonoBehaviour + { + [SerializeField] private UIDocument uiDocument; + private VisualElement player1Hp; + private VisualElement player2Hp; + + private void Awake() + { + player1Hp = uiDocument.rootVisualElement.Q("player1HP"); + player2Hp = uiDocument.rootVisualElement.Q("player2HP"); + } + + public void Register(Player.Player player) + { + var progressBar = player.playerIndex == 0 ? player1Hp : player2Hp; + progressBar.Q