diff --git a/scripts/HealthBarUi.cs b/scripts/HealthBarUi.cs index 34a53db..9d9fd27 100644 --- a/scripts/HealthBarUi.cs +++ b/scripts/HealthBarUi.cs @@ -1,138 +1,158 @@ +using ColdMint.scripts.utils; using Godot; namespace ColdMint.scripts; public partial class HealthBarUi : HBoxContainer { - private int _maxHp; - private int _currentHp; + private int _maxHp; + private int _currentHp; - public int CurrentHp - { - get => _currentHp; - set - { - if (value == _currentHp) - { - return; - } + public int CurrentHp + { + get => _currentHp; + set + { + if (value == _currentHp) + { + return; + } - var heartCount = GetChildCount(); - //有几颗心是满的 - var fullHeartCount = value / Config.HeartRepresentsHealthValue; - for (int i = 0; i < fullHeartCount; i++) - { - //把Ui刷满 - var textureRect = GetChild(i); - textureRect.Texture = _heartFull; - } + 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 emptyHeartCount = heartCount - fullHeartCount; - //最后那颗剩余多少血 - var leftOverTextureRect = GetChild(fullHeartCount); - var leftOver = value % Config.HeartRepresentsHealthValue; - if (leftOver > 0) - { - //占总数的百分比 - var percent = leftOver / (float)Config.HeartRepresentsHealthValue; - leftOverTextureRect.Texture = GetTexture2DByPercent(percent); - emptyHeartCount--; - } + var heartCount = GetChildCount(); + //A few hearts are full + //有几颗心是满的 + var fullHeartCount = value / Config.HeartRepresentsHealthValue; + for (int i = 0; i < fullHeartCount; i++) + { + //Brush up the Ui + //把Ui刷满 + var textureRect = GetChild(i); + textureRect.Texture = _heartFull; + } - for (int i = heartCount - emptyHeartCount; i < heartCount; i++) - { - var textureRect = GetChild(i); - textureRect.Texture = _heartEmpty; - } + //How many hollows + //有多少空心 + var emptyHeartCount = heartCount - fullHeartCount; + if (emptyHeartCount > 0) + { + //How much blood is left on the last one + //最后那颗剩余多少血 + var leftOverTextureRect = GetChild(fullHeartCount); + var leftOver = value % Config.HeartRepresentsHealthValue; + if (leftOver > 0) + { + //Percentage of total + //占总数的百分比 + var percent = leftOver / (float)Config.HeartRepresentsHealthValue; + leftOverTextureRect.Texture = GetTexture2DByPercent(percent); + emptyHeartCount--; + } + } - _currentHp = value; - } - } - public int MaxHp - { - get => _maxHp; - set - { - if (value == _maxHp) - { - return; - } + for (int i = heartCount - emptyHeartCount; i < heartCount; i++) + { + var textureRect = GetChild(i); + textureRect.Texture = _heartEmpty; + } - var heartCount = value / Config.HeartRepresentsHealthValue; - for (var i = 0; i < heartCount; i++) - { - var heart = CreateTextureRect(); - heart.Texture = _heartFull; - AddChild(heart); - } + _currentHp = value; + } + } - //最后那颗剩余多少血 - var leftOver = value % Config.HeartRepresentsHealthValue; - if (leftOver > 0) - { - var lastHeart = CreateTextureRect(); - //占总数的百分比 - var percent = leftOver / (float)Config.HeartRepresentsHealthValue; - lastHeart.Texture = GetTexture2DByPercent(percent); - AddChild(lastHeart); - } + public int MaxHp + { + get => _maxHp; + set + { + if (value == _maxHp) + { + return; + } - _maxHp = value; - } - } + var heartCount = value / Config.HeartRepresentsHealthValue; + for (var i = 0; i < heartCount; i++) + { + var heart = CreateTextureRect(); + heart.Texture = _heartFull; + AddChild(heart); + } - private TextureRect CreateTextureRect() - { - var textureRect = new TextureRect(); - return textureRect; - } + //How much blood is left on the last one + //最后那颗剩余多少血 + var leftOver = value % Config.HeartRepresentsHealthValue; + if (leftOver > 0) + { + var lastHeart = CreateTextureRect(); + //Percentage of total + //占总数的百分比 + var percent = leftOver / (float)Config.HeartRepresentsHealthValue; + lastHeart.Texture = GetTexture2DByPercent(percent); + AddChild(lastHeart); + } - /// - /// Get the texture based on percentage - /// 根据百分比获取纹理 - /// - /// - /// - private Texture2D GetTexture2DByPercent(float percent) - { - if (percent == 0) - { - return _heartEmpty; - } + _maxHp = value; + } + } - if (percent <= 0.25f) - { - return _heartQuarter; - } - else if (percent <= 0.5f) - { - return _heartHalf; - } - else if (percent <= 0.75f) - { - return _heartThreeFourths; - } - else - { - return _heartFull; - } - } + private TextureRect CreateTextureRect() + { + var textureRect = new TextureRect(); + return textureRect; + } - private Texture2D _heartFull; - private Texture2D _heartEmpty; - private Texture2D _heartHalf; - private Texture2D _heartQuarter; - private Texture2D _heartThreeFourths; + /// + /// Get the texture based on percentage + /// 根据百分比获取纹理 + /// + /// + /// + private Texture2D GetTexture2DByPercent(float percent) + { + if (percent == 0) + { + return _heartEmpty; + } - public override void _Ready() - { - base._Ready(); - _heartEmpty = GD.Load("res://sprites/ui/HeartEmpty.png"); - _heartQuarter = GD.Load("res://sprites/ui/HeartQuarter.png"); - _heartHalf = GD.Load("res://sprites/ui/HeartHalf.png"); - _heartThreeFourths = GD.Load("res://sprites/ui/HeartThreeFourths.png"); - _heartFull = GD.Load("res://sprites/ui/HeartFull.png"); - } -} + if (percent <= 0.25f) + { + return _heartQuarter; + } + else if (percent <= 0.5f) + { + return _heartHalf; + } + else if (percent <= 0.75f) + { + return _heartThreeFourths; + } + else + { + return _heartFull; + } + } + + private Texture2D _heartFull; + private Texture2D _heartEmpty; + private Texture2D _heartHalf; + private Texture2D _heartQuarter; + private Texture2D _heartThreeFourths; + + public override void _Ready() + { + base._Ready(); + NodeUtils.DeleteAllChild(this); + _heartEmpty = GD.Load("res://sprites/ui/HeartEmpty.png"); + _heartQuarter = GD.Load("res://sprites/ui/HeartQuarter.png"); + _heartHalf = GD.Load("res://sprites/ui/HeartHalf.png"); + _heartThreeFourths = GD.Load("res://sprites/ui/HeartThreeFourths.png"); + _heartFull = GD.Load("res://sprites/ui/HeartFull.png"); + } +} \ No newline at end of file diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index d2e1251..d0b4d12 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -134,10 +134,10 @@ public partial class CharacterTemplate : CharacterBody2D } _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 - //若初始血量小于等于0,则将初始血量设置为最大血量 + //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或者大于最大血量,那么将其设置为最大血量 _initialHp = MaxHp; } diff --git a/scripts/inventory/HotBar.cs b/scripts/inventory/HotBar.cs index b24d7ab..c2c545c 100644 --- a/scripts/inventory/HotBar.cs +++ b/scripts/inventory/HotBar.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using ColdMint.scripts.debug; +using ColdMint.scripts.utils; using ColdMint.scripts.weapon; using Godot; @@ -19,6 +20,7 @@ public partial class HotBar : HBoxContainer, IItemContainer public override void _Ready() { base._Ready(); + NodeUtils.DeleteAllChild(this); _itemSlotNodes = new List(); _itemSlotPackedScene = GD.Load("res://prefab/ui/ItemSlot.tscn"); for (var i = 0; i < Config.HotBarSize; i++) diff --git a/scripts/loader/sceneLoader/GameSceneLoader.cs b/scripts/loader/sceneLoader/GameSceneLoader.cs index 32b57cd..0215b00 100644 --- a/scripts/loader/sceneLoader/GameSceneLoader.cs +++ b/scripts/loader/sceneLoader/GameSceneLoader.cs @@ -1,3 +1,4 @@ +using System.Threading.Tasks; using ColdMint.scripts.debug; using ColdMint.scripts.inventory; using ColdMint.scripts.loader.uiLoader; @@ -15,82 +16,80 @@ namespace ColdMint.scripts.loader.sceneLoader; public partial class GameSceneLoader : SceneLoaderTemplate { - private IMapGenerator _mapGenerator; - private IMapGeneratorConfig _mapGeneratorConfig; + private IMapGenerator _mapGenerator; + private IMapGeneratorConfig _mapGeneratorConfig; - public override void InitializeData() - { - //加载血条场景 - var healthBarUI = GetNode("CanvasLayer/Control/VBoxContainer/HealthBarUi"); - NodeUtils.DeleteAllChild(healthBarUI); - GameSceneNodeHolder.HealthBarUi = healthBarUI; - //加载HotBar - var hotBar = GetNode("CanvasLayer/Control/VBoxContainer/HotBar"); - NodeUtils.DeleteAllChild(hotBar); - GameSceneNodeHolder.HotBar = hotBar; - //加载操作提示 - var operationTip = GetNode