From d6cd74729be64e57cea97f7cbdb6484cad8a7805 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: Wed, 27 Aug 2025 20:47:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0UI=E5=92=8C=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BC=98=E5=8C=96=E8=83=BD?= =?UTF-8?q?=E9=87=8F=E6=9D=A1=E6=98=BE=E7=A4=BA=E5=92=8C=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Scenes/UI.tscn | 1 - components/UI/ColorBar.tscn | 14 +++++++++++--- scripts/Contents/Characters/Rooster.gd | 1 + scripts/Statemachine/BossBar.gd | 3 +-- scripts/Statemachine/ColorBar.gd | 7 +++++-- scripts/Statemachine/EntityBase.gd | 15 ++++++++++++--- scripts/Statemachine/EntityStateBar.gd | 9 ++++++--- scripts/Statemachine/UIState.gd | 6 ++++-- 8 files changed, 40 insertions(+), 16 deletions(-) diff --git a/components/Scenes/UI.tscn b/components/Scenes/UI.tscn index d5a0368..839874c 100644 --- a/components/Scenes/UI.tscn +++ b/components/Scenes/UI.tscn @@ -117,4 +117,3 @@ text = "100.0" unique_name_in_owner = true custom_minimum_size = Vector2(0, 5) layout_mode = 2 -speed1 = 0.9 diff --git a/components/UI/ColorBar.tscn b/components/UI/ColorBar.tscn index 54f04da..975b667 100644 --- a/components/UI/ColorBar.tscn +++ b/components/UI/ColorBar.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=5 format=3 uid="uid://d1ulrvupa76ap"] +[gd_scene load_steps=6 format=3 uid="uid://d1ulrvupa76ap"] [ext_resource type="Script" path="res://scripts/Statemachine/ColorBar.gd" id="1_a106p"] @@ -9,13 +9,20 @@ corner_radius_top_right = 5 corner_radius_bottom_right = 5 corner_radius_bottom_left = 5 -[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_gkydp"] +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_sh754"] bg_color = Color(1, 0, 0, 0.5) corner_radius_top_left = 5 corner_radius_top_right = 5 corner_radius_bottom_right = 5 corner_radius_bottom_left = 5 +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_8n8i8"] +bg_color = Color(0, 1, 0, 0.5) +corner_radius_top_left = 5 +corner_radius_top_right = 5 +corner_radius_bottom_right = 5 +corner_radius_bottom_left = 5 + [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_x7p33"] bg_color = Color(1, 1, 1, 1) corner_radius_top_left = 5 @@ -30,7 +37,8 @@ offset_right = 100.0 offset_bottom = 10.0 script = ExtResource("1_a106p") backBox = SubResource("StyleBoxFlat_kf2fc") -middleBox = SubResource("StyleBoxFlat_gkydp") +middleBox1 = SubResource("StyleBoxFlat_sh754") +middleBox2 = SubResource("StyleBoxFlat_8n8i8") frontBox = SubResource("StyleBoxFlat_x7p33") speed1 = 0.9 speed2 = 0.05 diff --git a/scripts/Contents/Characters/Rooster.gd b/scripts/Contents/Characters/Rooster.gd index f6e232b..5f39481 100644 --- a/scripts/Contents/Characters/Rooster.gd +++ b/scripts/Contents/Characters/Rooster.gd @@ -35,3 +35,4 @@ func sprint(): func heal(count: float): health += count DamageLabel.create(-count, false, damageAnchor.global_position + MathTool.randv2_range(GameRule.damageLabelSpawnOffset)) + return count diff --git a/scripts/Statemachine/BossBar.gd b/scripts/Statemachine/BossBar.gd index 914ac84..b139ece 100644 --- a/scripts/Statemachine/BossBar.gd +++ b/scripts/Statemachine/BossBar.gd @@ -4,8 +4,7 @@ class_name BossBar @onready var nameLabel: Label = $"%name" @onready var valueLabel: Label = $"%value" -func _physics_process(delta): - super._physics_process(delta) +func _physics_process(_delta): if is_instance_valid(entity): nameLabel.text = entity.displayName valueLabel.text = "%.2f" % (entity.health / entity.fields[FieldStore.Entity.MAX_HEALTH] * 100) diff --git a/scripts/Statemachine/ColorBar.gd b/scripts/Statemachine/ColorBar.gd index c026834..d538395 100644 --- a/scripts/Statemachine/ColorBar.gd +++ b/scripts/Statemachine/ColorBar.gd @@ -6,7 +6,8 @@ class_name ColorBar @export var maxValue: float = 100 @export var currentValue: float = 50 @export var backBox: StyleBox -@export var middleBox: StyleBox +@export var middleBox1: StyleBox +@export var middleBox2: StyleBox @export var frontBox: StyleBox @export var speed1: float = 0.8 @export var speed2: float = 0.01 @@ -18,6 +19,8 @@ var forwardDirection = -1 func getPercent(value: float): return (value - minValue) / (maxValue - minValue) func setCurrent(value: float): + if value == currentValue: + return forwardDirection = sign(value - currentValue) currentValue = value @@ -26,7 +29,7 @@ func _ready(): frontValue = currentValue func _draw(): draw_style_box(backBox, Rect2(0, 0, size.x, size.y)) - draw_style_box(middleBox, Rect2(0, 0, size.x * getPercent(middleValue), size.y)) + draw_style_box(middleBox2 if forwardDirection > 0 else middleBox1, Rect2(0, 0, size.x * getPercent(middleValue), size.y)) draw_style_box(frontBox, Rect2(0, 0, size.x * getPercent(frontValue), size.y)) func _physics_process(_delta: float) -> void: middleValue = lerpf(middleValue, currentValue, speed1 if forwardDirection > 0 else speed2) diff --git a/scripts/Statemachine/EntityBase.gd b/scripts/Statemachine/EntityBase.gd index a7535e2..0238148 100644 --- a/scripts/Statemachine/EntityBase.gd +++ b/scripts/Statemachine/EntityBase.gd @@ -1,6 +1,10 @@ extends CharacterBody2D class_name EntityBase # 这是个抽象类 +signal hit(damage: float, bullet: BulletBase, crit: bool) +signal healed(amount: float) +signal healthChanged(health: float) + var fields = { FieldStore.Entity.MAX_HEALTH: 100, FieldStore.Entity.DAMAGE_MULTIPILER: 1, @@ -69,6 +73,7 @@ func _ready(): ) else: currentFocusedBoss = get_tree().get_nodes_in_group("players")[0] + healthChanged.emit(health) func _process(_delta): health = clamp(health, 0, fields.get(FieldStore.Entity.MAX_HEALTH)) energy = clamp(energy, 0, fields.get(FieldStore.Entity.MAX_ENERGY)) @@ -107,6 +112,8 @@ func takeDamage(bullet: BulletBase, crit: bool): playSound("hurt") bullet.launcher.storeEnergy(damage * 0.05) storeEnergy(damage * -0.1) + hit.emit(damage, bullet, crit) + healthChanged.emit(health) health -= damage DamageLabel.create(damage, crit, damageAnchor.global_position + MathTool.randv2_range(GameRule.damageLabelSpawnOffset)) if isBoss and bullet.launcher.isPlayer(): @@ -154,7 +161,8 @@ func tryHeal(count: float): if inventory[ItemStore.ItemType.APPLE] > 0 and health < fields.get(FieldStore.Entity.MAX_HEALTH): inventory[ItemStore.ItemType.APPLE] -= 1 playSound("heal") - heal(count * fields.get(FieldStore.Entity.HEAL_ABILITY)) + healed.emit(heal(count * fields.get(FieldStore.Entity.HEAL_ABILITY))) + healthChanged.emit(health) func findWeaponAnchor(weaponName: String): var anchor = $"%weapons".get_node(weaponName) if anchor is Node2D: @@ -187,8 +195,9 @@ func die(): queue_free() func sprint(): pass -func heal(_count: float): - pass +func heal(count: float): + health += count + return count static func generate( entity: PackedScene, diff --git a/scripts/Statemachine/EntityStateBar.gd b/scripts/Statemachine/EntityStateBar.gd index 13f54ef..cee186a 100644 --- a/scripts/Statemachine/EntityStateBar.gd +++ b/scripts/Statemachine/EntityStateBar.gd @@ -5,7 +5,10 @@ class_name EntityStateBar @onready var healthBar: ColorBar = $"%health" -func _physics_process(_delta): +func _ready(): if is_instance_valid(entity): - healthBar.maxValue = entity.fields.get(FieldStore.Entity.MAX_HEALTH) - healthBar.setCurrent(entity.health) + entity.healthChanged.connect( + func(health) -> void: + healthBar.maxValue = entity.fields.get(FieldStore.Entity.MAX_HEALTH) + healthBar.setCurrent(health) + ) diff --git a/scripts/Statemachine/UIState.gd b/scripts/Statemachine/UIState.gd index cbdb0dc..384e512 100644 --- a/scripts/Statemachine/UIState.gd +++ b/scripts/Statemachine/UIState.gd @@ -5,7 +5,8 @@ class_name UIState @onready var basketball = $"%basketball" @onready var apple = $"%apple" @onready var items = $"%items" -@onready var energy = $"%energy" +@onready var energyLabel: Label = $"%energy" +@onready var energyMaxLabel: Label = $"%energyMax" @onready var energyPercent: ColorBar = $"%percent" static var player: EntityBase = null @@ -20,7 +21,8 @@ func _process(_delta): bossbar.visible = !!bossbar.entity func _physics_process(_delta): if is_instance_valid(player): - energy.text = "%.1f"%player.energy + energyLabel.text = "%.1f" % player.energy + energyMaxLabel.text = "%.1f" % player.fields.get(FieldStore.Entity.MAX_ENERGY) energyPercent.maxValue = player.fields.get(FieldStore.Entity.MAX_ENERGY) energyPercent.setCurrent(player.energy) for i in items.get_children():