Fixed an issue where the health bar was drawn incorrectly.

修复健康条绘制不正确的问题。
This commit is contained in:
Cold-Mint 2024-05-07 17:38:50 +08:00
parent 9435f76fc0
commit 802eb599da
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
7 changed files with 259 additions and 219 deletions

View File

@ -1,3 +1,4 @@
using ColdMint.scripts.utils;
using Godot; using Godot;
namespace ColdMint.scripts; namespace ColdMint.scripts;
@ -17,28 +18,44 @@ public partial class HealthBarUi : HBoxContainer
return; return;
} }
if (_currentHp > _maxHp)
{
//Prohibit the current health to exceed the maximum health. When the maximum health is exceeded, the UI cannot be drawn.
//禁止当前血量超过最大血量当超过最大值时无法绘制UI。
return;
}
var heartCount = GetChildCount(); var heartCount = GetChildCount();
//A few hearts are full
//有几颗心是满的 //有几颗心是满的
var fullHeartCount = value / Config.HeartRepresentsHealthValue; var fullHeartCount = value / Config.HeartRepresentsHealthValue;
for (int i = 0; i < fullHeartCount; i++) for (int i = 0; i < fullHeartCount; i++)
{ {
//Brush up the Ui
//把Ui刷满 //把Ui刷满
var textureRect = GetChild<TextureRect>(i); var textureRect = GetChild<TextureRect>(i);
textureRect.Texture = _heartFull; textureRect.Texture = _heartFull;
} }
//How many hollows
//有多少空心 //有多少空心
var emptyHeartCount = heartCount - fullHeartCount; var emptyHeartCount = heartCount - fullHeartCount;
if (emptyHeartCount > 0)
{
//How much blood is left on the last one
//最后那颗剩余多少血 //最后那颗剩余多少血
var leftOverTextureRect = GetChild<TextureRect>(fullHeartCount); var leftOverTextureRect = GetChild<TextureRect>(fullHeartCount);
var leftOver = value % Config.HeartRepresentsHealthValue; var leftOver = value % Config.HeartRepresentsHealthValue;
if (leftOver > 0) if (leftOver > 0)
{ {
//Percentage of total
//占总数的百分比 //占总数的百分比
var percent = leftOver / (float)Config.HeartRepresentsHealthValue; var percent = leftOver / (float)Config.HeartRepresentsHealthValue;
leftOverTextureRect.Texture = GetTexture2DByPercent(percent); leftOverTextureRect.Texture = GetTexture2DByPercent(percent);
emptyHeartCount--; emptyHeartCount--;
} }
}
for (int i = heartCount - emptyHeartCount; i < heartCount; i++) for (int i = heartCount - emptyHeartCount; i < heartCount; i++)
{ {
@ -68,11 +85,13 @@ public partial class HealthBarUi : HBoxContainer
AddChild(heart); AddChild(heart);
} }
//How much blood is left on the last one
//最后那颗剩余多少血 //最后那颗剩余多少血
var leftOver = value % Config.HeartRepresentsHealthValue; var leftOver = value % Config.HeartRepresentsHealthValue;
if (leftOver > 0) if (leftOver > 0)
{ {
var lastHeart = CreateTextureRect(); var lastHeart = CreateTextureRect();
//Percentage of total
//占总数的百分比 //占总数的百分比
var percent = leftOver / (float)Config.HeartRepresentsHealthValue; var percent = leftOver / (float)Config.HeartRepresentsHealthValue;
lastHeart.Texture = GetTexture2DByPercent(percent); lastHeart.Texture = GetTexture2DByPercent(percent);
@ -129,6 +148,7 @@ public partial class HealthBarUi : HBoxContainer
public override void _Ready() public override void _Ready()
{ {
base._Ready(); base._Ready();
NodeUtils.DeleteAllChild(this);
_heartEmpty = GD.Load<Texture2D>("res://sprites/ui/HeartEmpty.png"); _heartEmpty = GD.Load<Texture2D>("res://sprites/ui/HeartEmpty.png");
_heartQuarter = GD.Load<Texture2D>("res://sprites/ui/HeartQuarter.png"); _heartQuarter = GD.Load<Texture2D>("res://sprites/ui/HeartQuarter.png");
_heartHalf = GD.Load<Texture2D>("res://sprites/ui/HeartHalf.png"); _heartHalf = GD.Load<Texture2D>("res://sprites/ui/HeartHalf.png");

View File

@ -134,10 +134,10 @@ public partial class CharacterTemplate : CharacterBody2D
} }
_initialHp = GetMeta("InitialHp", "0").AsInt32(); _initialHp = GetMeta("InitialHp", "0").AsInt32();
if (_initialHp <= 0) if (_initialHp <= 0 || _initialHp > MaxHp)
{ {
//If the initial blood volume is less than or equal to 0, the initial blood volume is set to the maximum blood volume //If the initial health is less than or equal to 0 or greater than the maximum health, then set it to the maximum health
//若初始血量小于等于0则将初始血量设置为最大血量 //如果初始血量小于等于0或者大于最大血量那么将其设置为最大血量
_initialHp = MaxHp; _initialHp = MaxHp;
} }

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ColdMint.scripts.debug; using ColdMint.scripts.debug;
using ColdMint.scripts.utils;
using ColdMint.scripts.weapon; using ColdMint.scripts.weapon;
using Godot; using Godot;
@ -19,6 +20,7 @@ public partial class HotBar : HBoxContainer, IItemContainer
public override void _Ready() public override void _Ready()
{ {
base._Ready(); base._Ready();
NodeUtils.DeleteAllChild(this);
_itemSlotNodes = new List<ItemSlotNode>(); _itemSlotNodes = new List<ItemSlotNode>();
_itemSlotPackedScene = GD.Load<PackedScene>("res://prefab/ui/ItemSlot.tscn"); _itemSlotPackedScene = GD.Load<PackedScene>("res://prefab/ui/ItemSlot.tscn");
for (var i = 0; i < Config.HotBarSize; i++) for (var i = 0; i < Config.HotBarSize; i++)

View File

@ -1,3 +1,4 @@
using System.Threading.Tasks;
using ColdMint.scripts.debug; using ColdMint.scripts.debug;
using ColdMint.scripts.inventory; using ColdMint.scripts.inventory;
using ColdMint.scripts.loader.uiLoader; using ColdMint.scripts.loader.uiLoader;
@ -18,15 +19,13 @@ public partial class GameSceneLoader : SceneLoaderTemplate
private IMapGenerator _mapGenerator; private IMapGenerator _mapGenerator;
private IMapGeneratorConfig _mapGeneratorConfig; private IMapGeneratorConfig _mapGeneratorConfig;
public override void InitializeData() public override async Task InitializeData()
{ {
//加载血条场景 //加载血条场景
var healthBarUI = GetNode<HealthBarUi>("CanvasLayer/Control/VBoxContainer/HealthBarUi"); var healthBarUI = GetNode<HealthBarUi>("CanvasLayer/Control/VBoxContainer/HealthBarUi");
NodeUtils.DeleteAllChild(healthBarUI);
GameSceneNodeHolder.HealthBarUi = healthBarUI; GameSceneNodeHolder.HealthBarUi = healthBarUI;
//加载HotBar //加载HotBar
var hotBar = GetNode<HotBar>("CanvasLayer/Control/VBoxContainer/HotBar"); var hotBar = GetNode<HotBar>("CanvasLayer/Control/VBoxContainer/HotBar");
NodeUtils.DeleteAllChild(hotBar);
GameSceneNodeHolder.HotBar = hotBar; GameSceneNodeHolder.HotBar = hotBar;
//加载操作提示 //加载操作提示
var operationTip = GetNode<Label>("CanvasLayer/Control/VBoxContainer/OperationTip"); var operationTip = GetNode<Label>("CanvasLayer/Control/VBoxContainer/OperationTip");
@ -59,9 +58,9 @@ public partial class GameSceneLoader : SceneLoaderTemplate
_mapGenerator.RoomPlacer = roomPlacer; _mapGenerator.RoomPlacer = roomPlacer;
} }
public override void LoadScene() public override async Task LoadScene()
{ {
_mapGenerator.Generate(_mapGeneratorConfig); await _mapGenerator.Generate(_mapGeneratorConfig);
var packedScene = GD.Load<PackedScene>("res://prefab/entitys/Character.tscn"); var packedScene = GD.Load<PackedScene>("res://prefab/entitys/Character.tscn");
//Register players in the holder //Register players in the holder
//在持有者内注册玩家 //在持有者内注册玩家

View File

@ -1,3 +1,5 @@
using System.Threading.Tasks;
namespace ColdMint.scripts.loader.sceneLoader; namespace ColdMint.scripts.loader.sceneLoader;
public interface ISceneLoaderContract public interface ISceneLoaderContract
@ -6,11 +8,11 @@ public interface ISceneLoaderContract
/// <para>initialization data</para> /// <para>initialization data</para>
/// <para>初始化数据</para> /// <para>初始化数据</para>
/// </summary> /// </summary>
void InitializeData(); Task InitializeData();
/// <summary> /// <summary>
/// <para>load scene</para> /// <para>load scene</para>
/// <para>加载场景</para> /// <para>加载场景</para>
/// </summary> /// </summary>
void LoadScene(); Task LoadScene();
} }

View File

@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Godot; using Godot;
namespace ColdMint.scripts.loader.sceneLoader; namespace ColdMint.scripts.loader.sceneLoader;
@ -8,19 +9,20 @@ namespace ColdMint.scripts.loader.sceneLoader;
/// </summary> /// </summary>
public partial class SceneLoaderTemplate : Node2D, ISceneLoaderContract public partial class SceneLoaderTemplate : Node2D, ISceneLoaderContract
{ {
public sealed override void _Ready() public sealed override async void _Ready()
{ {
InitializeData(); await InitializeData();
LoadScene(); await LoadScene();
} }
public virtual void InitializeData()
public virtual Task InitializeData()
{ {
return Task.CompletedTask;
} }
public virtual void LoadScene() public virtual Task LoadScene()
{ {
return Task.CompletedTask;
} }
} }

View File

@ -1,4 +1,5 @@
using Godot; using System.Threading.Tasks;
using Godot;
namespace ColdMint.scripts.utils; namespace ColdMint.scripts.utils;
@ -9,16 +10,30 @@ public class NodeUtils
/// <para>删除所有子节点</para> /// <para>删除所有子节点</para>
/// </summary> /// </summary>
/// <param name="parent"></param> /// <param name="parent"></param>
public static void DeleteAllChild(Node parent) public static int DeleteAllChild(Node parent)
{ {
var deleteNumber = 0;
var count = parent.GetChildCount(); var count = parent.GetChildCount();
if (count > 0) if (count <= 0) return deleteNumber;
{ for (var i = 0; i < count; i++)
for (int i = 0; i < count; i++)
{ {
var node = parent.GetChild(0); var node = parent.GetChild(0);
parent.RemoveChild(node);
node.QueueFree(); node.QueueFree();
} deleteNumber++;
} }
return deleteNumber;
}
/// <summary>
/// <para>All child nodes are removed asynchronously</para>
/// <para>异步删除所有子节点</para>
/// </summary>
/// <param name="parent"></param>
/// <returns></returns>
public static async Task<int> DeleteAllChildAsync(Node parent)
{
return await Task.Run<int>(() => DeleteAllChild(parent));
} }
} }