From debb450044842a0c641f012c76dab6e98114aa35 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: Sat, 6 Sep 2025 07:40:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=AD=A6=E5=99=A8=E7=B3=BB=E7=BB=9F):?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84=E6=AD=A6=E5=99=A8=E5=86=B7=E5=8D=B4?= =?UTF-8?q?=E5=92=8C=E6=94=BB=E5=87=BB=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将CooldownTimer的startCooldown方法重命名为更简洁的start - 为EntityBase添加weaponStore节点管理武器 - 修改武器攻击逻辑,现在由Weapon类自身处理冷却和攻击 - 调整武器卡片的UI布局和描述居中显示 - 为Rooster角色添加预设武器 --- components/Abstracts/EntityBase.tscn | 3 +++ components/Abstracts/WeaponCardBase.tscn | 4 ++-- components/Characters/Rooster.tscn | 23 +++++++++++++++-------- components/Weapons/PurpleCrystal.tscn | 7 +++++-- scripts/Contents/Weapons/PurpleCrystal.gd | 2 ++ scripts/Statemachine/CooldownTimer.gd | 2 +- scripts/Statemachine/EntityBase.gd | 23 +++++++++++++++++++---- scripts/Structs/Weapon.gd | 5 ++++- 8 files changed, 51 insertions(+), 18 deletions(-) diff --git a/components/Abstracts/EntityBase.tscn b/components/Abstracts/EntityBase.tscn index e6c0dc6..91b43f6 100644 --- a/components/Abstracts/EntityBase.tscn +++ b/components/Abstracts/EntityBase.tscn @@ -258,6 +258,9 @@ alpha_curve = SubResource("CurveTexture_vvfxd") [node name="EntityBase" type="CharacterBody2D"] script = ExtResource("1_mvol6") +[node name="weaponStore" type="Node2D" parent="."] +unique_name_in_owner = true + [node name="sounds" type="Node2D" parent="."] unique_name_in_owner = true diff --git a/components/Abstracts/WeaponCardBase.tscn b/components/Abstracts/WeaponCardBase.tscn index 4b64c2e..0e178ef 100644 --- a/components/Abstracts/WeaponCardBase.tscn +++ b/components/Abstracts/WeaponCardBase.tscn @@ -23,7 +23,7 @@ corner_radius_bottom_right = 5 corner_radius_bottom_left = 5 [node name="WeaponCard" type="PanelContainer"] -offset_right = 300.0 +offset_right = 350.0 offset_bottom = 304.0 theme_override_styles/panel = SubResource("StyleBoxFlat_n2ewr") script = ExtResource("1_g802t") @@ -32,6 +32,7 @@ metadata/_edit_lock_ = true [node name="container" type="VBoxContainer" parent="."] layout_mode = 2 theme_override_constants/separation = 10 +alignment = 1 [node name="selectBtn" type="Button" parent="container"] unique_name_in_owner = true @@ -80,7 +81,6 @@ quality = 1 [node name="description" type="RichTextLabel" parent="container"] unique_name_in_owner = true layout_mode = 2 -size_flags_vertical = 0 bbcode_enabled = true text = "造成[color=cyan]10.0[/color]点伤害。" fit_content = true diff --git a/components/Characters/Rooster.tscn b/components/Characters/Rooster.tscn index 838a7e1..d50b277 100644 --- a/components/Characters/Rooster.tscn +++ b/components/Characters/Rooster.tscn @@ -1,11 +1,13 @@ -[gd_scene load_steps=13 format=3 uid="uid://bm7ymrri6pykb"] +[gd_scene load_steps=15 format=3 uid="uid://bm7ymrri6pykb"] [ext_resource type="PackedScene" uid="uid://cvogxi7mktumf" path="res://components/Abstracts/EntityBase.tscn" id="1_e5pl8"] [ext_resource type="Script" path="res://scripts/Contents/Characters/Rooster.gd" id="2_oqdqd"] [ext_resource type="Texture2D" uid="uid://fn8qx72clh38" path="res://resources/characters/cock/rooster-a.svg" id="2_q0j6j"] [ext_resource type="AudioStream" uid="uid://dclinyhu256xi" path="res://resources/sounds/effect/Low Whoosh.mp3" id="3_4syso"] [ext_resource type="Texture2D" uid="uid://ci2ik43ce82uy" path="res://resources/characters/cock/rooster-b.svg" id="3_b0fgx"] +[ext_resource type="PackedScene" uid="uid://c0n3igy4hucrg" path="res://components/Weapons/PurpleCrystal.tscn" id="3_ms5sq"] [ext_resource type="AudioStream" uid="uid://cdrevrq7n6yqa" path="res://resources/sounds/effect/Boing.mp3" id="4_66s6c"] +[ext_resource type="PackedScene" uid="uid://cxabqjo7skxev" path="res://components/Weapons/BigLaser.tscn" id="4_0862n"] [ext_resource type="AudioStream" uid="uid://benyec5bqni0b" path="res://resources/sounds/effect/Chomp.wav" id="4_k0yme"] [ext_resource type="AudioStream" uid="uid://dmxh3bpk8vyy5" path="res://resources/sounds/effect/Coin.mp3" id="5_xnbhq"] [ext_resource type="AudioStream" uid="uid://4wuuf1osk0yv" path="res://resources/sounds/effect/Low Boing.wav" id="6_m5px1"] @@ -38,8 +40,13 @@ radius = 41.0122 [node name="Rooster" instance=ExtResource("1_e5pl8")] script = ExtResource("2_oqdqd") -cooldownUnit = 200.0 -energy = 50.0 + +[node name="weaponStore" parent="." index="0"] +process_mode = 4 + +[node name="PurpleCrystal" parent="weaponStore" index="0" instance=ExtResource("3_ms5sq")] + +[node name="BigLaser" parent="weaponStore" index="1" instance=ExtResource("4_0862n")] [node name="sprint" parent="sounds" index="0"] stream = ExtResource("4_66s6c") @@ -53,13 +60,13 @@ stream = ExtResource("5_xnbhq") [node name="hurt" parent="sounds" index="3"] stream = ExtResource("6_m5px1") -[node name="attack0" parent="sounds" index="4"] -stream = ExtResource("3_4syso") - [node name="attack1" type="AudioStreamPlayer2D" parent="sounds" index="5"] stream = ExtResource("8_7dhim") -[node name="texture" parent="." index="1"] +[node name="attack0" parent="sounds" index="6"] +stream = ExtResource("3_4syso") + +[node name="texture" parent="." index="2"] position = Vector2(0, -70) sprite_frames = SubResource("SpriteFrames_4v2ol") animation = &"walk" @@ -71,6 +78,6 @@ shape = SubResource("CircleShape2D_h1v0q") [node name="normal" type="Node2D" parent="texture/weapons" index="0"] position = Vector2(54, -45) -[node name="statebar" parent="." index="2" node_paths=PackedStringArray("entity")] +[node name="statebar" parent="." index="3" node_paths=PackedStringArray("entity")] position = Vector2(0, -151) entity = NodePath("..") diff --git a/components/Weapons/PurpleCrystal.tscn b/components/Weapons/PurpleCrystal.tscn index 5414c86..092a1c8 100644 --- a/components/Weapons/PurpleCrystal.tscn +++ b/components/Weapons/PurpleCrystal.tscn @@ -1,16 +1,19 @@ -[gd_scene load_steps=3 format=3 uid="uid://c0n3igy4hucrg"] +[gd_scene load_steps=4 format=3 uid="uid://c0n3igy4hucrg"] [ext_resource type="PackedScene" uid="uid://ckq2cq6m23hq3" path="res://components/Abstracts/WeaponCardBase.tscn" id="1_hyubh"] +[ext_resource type="Script" path="res://scripts/Contents/Weapons/PurpleCrystal.gd" id="2_0xgcv"] [ext_resource type="Texture2D" uid="uid://16yhngg3jpun" path="res://resources/weapons/purple-crystal.svg" id="2_wgtcw"] [node name="PurpleCrystal" instance=ExtResource("1_hyubh")] offset_right = 208.0 -offset_bottom = 280.0 +offset_bottom = 282.0 +script = ExtResource("2_0xgcv") avatarTexture = ExtResource("2_wgtcw") displayName = "紫水晶簇" costs = Array[int]([0, 1]) costCounts = Array[int]([50, 10]) descriptionTemplate = "撞击造成$atk点伤害。" +cooldown = 200.0 [node name="avatar" parent="container/info" index="0"] texture = ExtResource("2_wgtcw") diff --git a/scripts/Contents/Weapons/PurpleCrystal.gd b/scripts/Contents/Weapons/PurpleCrystal.gd index 548690b..472e9ba 100644 --- a/scripts/Contents/Weapons/PurpleCrystal.gd +++ b/scripts/Contents/Weapons/PurpleCrystal.gd @@ -7,3 +7,5 @@ func update(to: int, origin: Dictionary, _entity: EntityBase): func attack(entity: EntityBase): var weaponPos = entity.findWeaponAnchor("normal") BulletBase.generate(preload("res://components/Bullets/PurpleCrystal.tscn"), entity, weaponPos, (get_global_mouse_position() - weaponPos).angle()) + print("test2") + return true diff --git a/scripts/Statemachine/CooldownTimer.gd b/scripts/Statemachine/CooldownTimer.gd index a5d4835..ecb15ab 100644 --- a/scripts/Statemachine/CooldownTimer.gd +++ b/scripts/Statemachine/CooldownTimer.gd @@ -5,7 +5,7 @@ var lastStart: int = 0 func isCooldowned(): return WorldManager.getTime() - lastStart >= cooldown -func startCooldown(): +func start(): var state = isCooldowned() if state: lastStart = WorldManager.getTime() diff --git a/scripts/Statemachine/EntityBase.gd b/scripts/Statemachine/EntityBase.gd index 6ecab32..aa55a99 100644 --- a/scripts/Statemachine/EntityBase.gd +++ b/scripts/Statemachine/EntityBase.gd @@ -64,7 +64,6 @@ var inventoryMax = { @export var dropCounts: Array[Vector2] = [] @export var appleCount: Vector2i = Vector2(0, 2) # 死亡后掉落的苹果数量 @export var level: int = 1 -@export var weapons: Array[Weapon] = [] @onready var animatree: AnimationTree = $"%animatree" @onready var texture: AnimatedSprite2D = $"%texture" @@ -73,6 +72,7 @@ var inventoryMax = { @onready var hurtAnimator: AnimationPlayer = $"%hurtAnimator" @onready var damageAnchor: Node2D = $"%damageAnchor" @onready var trailParticle: GPUParticles2D = $"%trailParticle" +@onready var weaponStore = $"%weaponStore" var statebar: EntityStateBar var health: float = 0 @@ -84,6 +84,7 @@ var lastDirection: int = 1 var currentFocusedBoss: EntityBase = null var charginup: bool = false var cooldownTimer = CooldownTimer.new() +var weapons: Array[Weapon] = [] func _ready(): register() @@ -94,6 +95,9 @@ func _ready(): statebar = selfStatebar statebar.entity = self if isPlayer(): + for i in weaponStore.get_children(): + i.hide() + weapons.append(i) statebar.levelLabels.hide() UIState.player = self energyChanged.connect( @@ -190,16 +194,27 @@ func useEnergy(value: float): energyChanged.emit(energy) return state func tryAttack(type: int, needChargeUp: bool = false): + var weapon: Weapon + if isPlayer(): + weapon = weapons[type] var state - if !isPlayer(): + if isPlayer(): + state = weapon.cooldownTimer.start() + else: cooldownTimer.cooldown = attackCooldownMap.get(type, defaultCooldownUnit) - state = cooldownTimer.startCooldown() + state = cooldownTimer.start() if state: if needChargeUp: charginup = true await EffectController.create(preload("res://components/Effects/AttackStar.tscn"), damageAnchor.global_position).shot() charginup = false - if attack(type): + var done + if isPlayer(): + done = weapon.attack(self) + print("test", done, weapon.name) + else: + done = attack(type) + if done: playSound("attack" + str(type)) return state func trySprint(): diff --git a/scripts/Structs/Weapon.gd b/scripts/Structs/Weapon.gd index dc0f85c..6ff3820 100644 --- a/scripts/Structs/Weapon.gd +++ b/scripts/Structs/Weapon.gd @@ -24,11 +24,14 @@ signal selected(applied: bool) @onready var costsBox: GridContainer = $"%costs" @onready var selectButton: Button = $"%selectBtn" +var cooldownTimer = CooldownTimer.new() + func _ready(): selectButton.pressed.connect( func(): apply(UIState.player) ) + cooldownTimer.cooldown = cooldown rebuildInfo() func _physics_process(_delta: float): descriptionLabel.text = buildDescription() @@ -76,7 +79,7 @@ func buildDescription(): var result = descriptionTemplate for key in store.keys(): result = result.replace("$" + key, "[color=cyan]%.1f[/color]" % readStore(key)) - return result + return "[center]%s[/center]" % result func readStore(key: String, default: Variant = null): return store.get(key, default)