chore():整理了一下文件夹和玩家的血量逻辑

This commit is contained in:
2025-10-18 15:41:43 +08:00
parent ab403fac4b
commit b709d1219f
17 changed files with 214 additions and 33 deletions

View File

@@ -1,5 +1,6 @@
using System;
using Core;
using Script.Gameplay.Input;
using UnityEngine;
namespace Script.Gameplay.Global
@@ -19,6 +20,13 @@ namespace Script.Gameplay.Global
private void Start()
{
GameCountdownManager.Instance.StartLevelTimer();
GameCountdownManager.Instance.OnFinish.AddListener(RestartGame);
}
public void RestartGame()
{
GameManager.Instance.ReStartGame();
}
}
}

View File

@@ -19,15 +19,25 @@ namespace Script.Gameplay.Input
private bool _hasFocus = true;
private void Awake()
protected override void Awake()
{
Input = new PlayerInputActions();
base.Awake();
}
private void OnEnable()
{
Input.Enable();
}
private void OnDisable()
{
// 可选:取消注册以防止重复订阅(简单项目可不解除)
Input.Disable();
}
private void Start()
{
// 注册事件
Input.Player.Move.performed += ctx => Move = ctx.ReadValue<Vector2>();
Input.Player.Move.canceled += ctx => Move = Vector2.zero;
@@ -46,16 +56,11 @@ namespace Script.Gameplay.Input
Input.Player.Edit.performed += ctx => EditPressed = true;
Input.Player.Edit.canceled += ctx => EditPressed = false;
}
private void OnDisable()
{
// 可选:取消注册以防止重复订阅(简单项目可不解除)
Input.Disable();
}
private void Start()
{
SetCursorState(false, CursorLockMode.Locked);
SetInputForLook(true);
SetInputForMove(true);
UIManager.Instance.RegisterInputManager(this);
}

View File

@@ -0,0 +1,8 @@
namespace Interface
{
public interface IDamageable
{
public void TakeDamage(int damage);
public void Heal(int heal);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 86b0cd642bb840a49e1ff26b4da53132
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -5,43 +5,54 @@ using Gameplay;
using Interface;
using UnityEngine;
using Core;
using Script.Gameplay.Global;
namespace Gameplay.Player
{
public class PlayerController : MonoBehaviour
public class PlayerController : MonoBehaviour, IDamageable
{
public int MaxHealth { get; set; } = 100;
public int CurrentHealth { get; private set; }
private int currentCardIndex = 0;
public bool IsFlight { get; private set; }
public bool IsDead { get; private set; }
[SerializeField] private int MaxHealth = 100;
private int currentHealth;
public int CurrentHealth
{
get => currentHealth;
private set
{
currentHealth = value;
OnHealthChanged?.Invoke(currentHealth);
}
}
public event Action<int> OnHealthChanged;
public event Action OnDeath;
private PlayerMoveController playerMoveController;
private PlayerCameraController playerCameraController;
private void Awake()
{
playerMoveController = GetComponent<PlayerMoveController>();
playerCameraController = GetComponent<PlayerCameraController>();
CurrentHealth = MaxHealth;
ControllerLocator.Instance.Register(this);
}
private void Start()
{
OnDeath += GameFlowManager.Instance.RestartGame;
}
public void TakeDamage(int damage)
{
CurrentHealth -= damage;
if (CurrentHealth <= 0)
{
CurrentHealth = 0;
OnDeath?.Invoke();
}
}
public void Heal(int heal)

View File

@@ -16,17 +16,20 @@ namespace UI
ControllerLocator.Instance.TryGetWait<PlayerController>(OnGet);
}
private void Update()
{
if (_playerController != null)
{
hpText.text = "HP: " + _playerController.CurrentHealth;
}
}
private void OnGet(PlayerController playerController)
{
_playerController = playerController;
_playerController.OnHealthChanged += UpdateHpText;
}
private void UpdateHpText(int currentHealth)
{
hpText.text = "HP: " + currentHealth;
}
private void OnDestroy()
{
_playerController.OnHealthChanged -= UpdateHpText;
}
}
}