From f19f1376ed33f992e95da58c65d75a5697120cf8 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 14:26:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(Statemachine):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=89=A9=E5=93=81=E6=94=B6=E9=9B=86=E7=8A=B6=E6=80=81=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=94=B6=E9=9B=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加collecting状态变量控制物品收集过程,防止重复收集 优化物品移动逻辑,根据距离动态调整移动速度 --- scripts/Statemachine/ItemDropped.gd | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/scripts/Statemachine/ItemDropped.gd b/scripts/Statemachine/ItemDropped.gd index a94bd1c..b5b7495 100644 --- a/scripts/Statemachine/ItemDropped.gd +++ b/scripts/Statemachine/ItemDropped.gd @@ -4,6 +4,7 @@ class_name ItemDropped var item: ItemStore.ItemType = ItemStore.ItemType.BASEBALL var stackCount: int = 1 var targetPlayer: EntityBase = null +var collecting: bool = false @onready var texture: Sprite2D = $"%texture" @onready var animator: AnimationPlayer = $"%animator" @@ -16,10 +17,15 @@ func _physics_process(_delta): if !is_instance_valid(targetPlayer): targetPlayer = findPlayer() if is_instance_valid(targetPlayer): - apply_central_force((targetPlayer.position - position).normalized() * 1000) - if position.distance_to(targetPlayer.position) < 60: - targetPlayer.collectItem(item, stackCount) - collect() + if collecting: + linear_velocity = Vector2.ZERO + else: + var direction = (targetPlayer.position - position).normalized() + var speed = 4000.0 / ((targetPlayer.position - position).length() ** (1 / 3.0)) + apply_central_force(direction * speed) + if position.distance_to(targetPlayer.position) < 60: + targetPlayer.collectItem(item, stackCount) + collect() func findPlayer() -> EntityBase: var result = null @@ -31,6 +37,7 @@ func findPlayer() -> EntityBase: result = player return result func collect(): + collecting = true animator.play("collect") await animator.animation_finished queue_free()