From ef03aa2922b634f43bd749ef529cdd8f54d3a343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=A8=E8=90=BD=E5=9F=BA=E5=9B=B4=E8=99=BE?= <3161880837@qq.com> Date: Thu, 28 Aug 2025 07:28:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor(Statemachine):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=AE=9E=E4=BD=93=E7=8A=B6=E6=80=81=E6=A0=8F=E5=92=8CBoss?= =?UTF-8?q?=E6=A0=8F=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将EntityStateBar的_ready逻辑移到EntityBase中统一管理 修改BossBar的更新逻辑从_physics_process改为_process 调整Wave配置中Boss的出现波次从8改为1 --- scripts/Contents/Wave.gd | 2 +- scripts/Statemachine/BossBar.gd | 4 +++- scripts/Statemachine/EntityBase.gd | 17 +++++++++++++++-- scripts/Statemachine/EntityStateBar.gd | 8 -------- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/scripts/Contents/Wave.gd b/scripts/Contents/Wave.gd index 4a562d7..ef086c2 100644 --- a/scripts/Contents/Wave.gd +++ b/scripts/Contents/Wave.gd @@ -13,7 +13,7 @@ static var countBoost: float = 0.1 # 每波增加的敌人数量百分比,指 static var data: Array[Wave] = [ # entity, minCount, maxCount, isBoss, from, to, per create(preload("res://components/Characters/Hen.tscn"), 1, 5, false, 0, INF, 1), - create(preload("res://components/Characters/Chick.tscn"), 0, 0, true, 8, INF, 4) + create(preload("res://components/Characters/Chick.tscn"), 0, 0, true, 1, INF, 4) ] static func create( diff --git a/scripts/Statemachine/BossBar.gd b/scripts/Statemachine/BossBar.gd index b139ece..81aba49 100644 --- a/scripts/Statemachine/BossBar.gd +++ b/scripts/Statemachine/BossBar.gd @@ -4,7 +4,9 @@ class_name BossBar @onready var nameLabel: Label = $"%name" @onready var valueLabel: Label = $"%value" -func _physics_process(_delta): +func _ready(): if is_instance_valid(entity): nameLabel.text = entity.displayName +func _process(_delta): + if is_instance_valid(entity): valueLabel.text = "%.2f" % (entity.health / entity.fields[FieldStore.Entity.MAX_HEALTH] * 100) diff --git a/scripts/Statemachine/EntityBase.gd b/scripts/Statemachine/EntityBase.gd index 1abb597..d97f224 100644 --- a/scripts/Statemachine/EntityBase.gd +++ b/scripts/Statemachine/EntityBase.gd @@ -51,10 +51,10 @@ var inventoryMax = { @onready var animatree: AnimationTree = $"%animatree" @onready var texture: AnimatedSprite2D = $"%texture" @onready var hurtbox: Area2D = $"%hurtbox" -@onready var statebar: EntityStateBar = $"%statebar" @onready var sounds: Node2D = $"%sounds" @onready var hurtAnimator: AnimationPlayer = $"%hurtAnimator" @onready var damageAnchor: Node2D = $"%damageAnchor" +var statebar: EntityStateBar var health: float = 0 @export var energy: float = 0 @@ -65,8 +65,13 @@ var lastAttack: int = 0 var currentFocusedBoss: EntityBase = null func _ready(): + var selfStatebar: EntityStateBar = $"%statebar" + if isBoss: + selfStatebar.hide() + else: + statebar = selfStatebar + statebar.entity = self health = fields.get(FieldStore.Entity.MAX_HEALTH) - statebar.visible = !isBoss if isPlayer(): UIState.player = self hurtbox.body_entered.connect( @@ -88,6 +93,14 @@ func _ready(): ) else: currentFocusedBoss = get_tree().get_nodes_in_group("players")[0] + healthChanged.connect( + func(newHealth): + if is_instance_valid(statebar) or UIState.bossbar.entity == self: + if isBoss: + statebar = UIState.bossbar + statebar.healthBar.maxValue = fields.get(FieldStore.Entity.MAX_HEALTH) + statebar.healthBar.setCurrent(newHealth) + ) healthChanged.emit(health) energyChanged.emit(energy) func _process(_delta): diff --git a/scripts/Statemachine/EntityStateBar.gd b/scripts/Statemachine/EntityStateBar.gd index cee186a..24a48fe 100644 --- a/scripts/Statemachine/EntityStateBar.gd +++ b/scripts/Statemachine/EntityStateBar.gd @@ -4,11 +4,3 @@ class_name EntityStateBar @export var entity: EntityBase @onready var healthBar: ColorBar = $"%health" - -func _ready(): - if is_instance_valid(entity): - entity.healthChanged.connect( - func(health) -> void: - healthBar.maxValue = entity.fields.get(FieldStore.Entity.MAX_HEALTH) - healthBar.setCurrent(health) - )