1
1
mirror of https://github.com/Rundll86/Dog-Lynx-And-HCN.git synced 2026-05-28 06:51:54 +08:00
Files
Dog-Lynx-And-HCN/scripts/Statemachine/ItemDropped.gd
T
fallingshrimp efa2912ae5 feat(FieldShow): 添加实体视图转换功能及新字段
为FieldShow组件添加useViewCast参数和entity引用,支持通过视图转换函数动态计算显示值
新增DROPPED_ITEM_GRAVITY相关字段到FieldStore
调整ItemDropped状态机的物品移动速度计算参数
2025-08-29 07:54:00 +08:00

58 lines
1.7 KiB
GDScript

extends RigidBody2D
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"
func _ready():
apply_force(MathTool.randv2_range(30000), MathTool.randv2_range(10))
func _process(_delta):
texture.texture = ItemStore.getTexture(item)
func _physics_process(_delta):
if !is_instance_valid(targetPlayer):
targetPlayer = findPlayer()
if is_instance_valid(targetPlayer):
if collecting:
linear_velocity = Vector2.ZERO
else:
var direction = (targetPlayer.position - position).normalized()
var speed = 5000.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
var lastDistance = INF
for player in get_tree().get_nodes_in_group("players"):
if player is EntityBase:
if position.distance_to(player.position) < lastDistance:
lastDistance = position.distance_to(player.position)
result = player
return result
func collect():
collecting = true
animator.play("collect")
await animator.animation_finished
queue_free()
static func generate(
itemType: ItemStore.ItemType,
count: int,
spawnPosition: Vector2,
addToWorld: bool = true
):
var instance: ItemDropped = preload("res://components/UI/ItemDropped.tscn").instantiate()
instance.item = itemType
instance.stackCount = count
instance.position = spawnPosition
if addToWorld:
WorldManager.rootNode.call_deferred("add_child", instance)
return instance