chore():整理了一下文件夹和玩家的血量逻辑
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
||||
|
8
Assets/Script/Gameplay/Interface/IDamageable.cs
Normal file
8
Assets/Script/Gameplay/Interface/IDamageable.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Interface
|
||||
{
|
||||
public interface IDamageable
|
||||
{
|
||||
public void TakeDamage(int damage);
|
||||
public void Heal(int heal);
|
||||
}
|
||||
}
|
11
Assets/Script/Gameplay/Interface/IDamageable.cs.meta
Normal file
11
Assets/Script/Gameplay/Interface/IDamageable.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86b0cd642bb840a49e1ff26b4da53132
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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)
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user