1
1
mirror of https://github.com/Rundll86/Dog-Lynx-And-HCN.git synced 2026-07-02 00:02:13 +08:00

feat(Statemachine): 添加物品收集状态并优化收集逻辑

添加collecting状态变量控制物品收集过程,防止重复收集
优化物品移动逻辑,根据距离动态调整移动速度
This commit is contained in:
2025-08-28 14:26:57 +08:00
parent 4b689e23fb
commit f19f1376ed
+11 -4
View File
@@ -4,6 +4,7 @@ class_name ItemDropped
var item: ItemStore.ItemType = ItemStore.ItemType.BASEBALL var item: ItemStore.ItemType = ItemStore.ItemType.BASEBALL
var stackCount: int = 1 var stackCount: int = 1
var targetPlayer: EntityBase = null var targetPlayer: EntityBase = null
var collecting: bool = false
@onready var texture: Sprite2D = $"%texture" @onready var texture: Sprite2D = $"%texture"
@onready var animator: AnimationPlayer = $"%animator" @onready var animator: AnimationPlayer = $"%animator"
@@ -16,10 +17,15 @@ func _physics_process(_delta):
if !is_instance_valid(targetPlayer): if !is_instance_valid(targetPlayer):
targetPlayer = findPlayer() targetPlayer = findPlayer()
if is_instance_valid(targetPlayer): if is_instance_valid(targetPlayer):
apply_central_force((targetPlayer.position - position).normalized() * 1000) if collecting:
if position.distance_to(targetPlayer.position) < 60: linear_velocity = Vector2.ZERO
targetPlayer.collectItem(item, stackCount) else:
collect() 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: func findPlayer() -> EntityBase:
var result = null var result = null
@@ -31,6 +37,7 @@ func findPlayer() -> EntityBase:
result = player result = player
return result return result
func collect(): func collect():
collecting = true
animator.play("collect") animator.play("collect")
await animator.animation_finished await animator.animation_finished
queue_free() queue_free()