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(): await TickTool.millseconds(100) body_entered.connect( func(body): if body is ItemDropped and !body.collecting: if body.item == item: body.stackCount += stackCount collect() ) func _process(_delta): texture.texture = ItemStore.getTexture(item) func _physics_process(_delta): if is_instance_valid(targetPlayer): if collecting: linear_velocity = Vector2.ZERO elif canICollect(): var direction = (targetPlayer.position - position).normalized() var speed = 1000.0 * targetPlayer.fields.get(FieldStore.Entity.GRAVITY) / ((targetPlayer.position - position).length() ** (1 / 3.0)) apply_central_force(direction * speed) angular_velocity = linear_velocity.length() ** (1.0 / 2.25) # 角速度=线速度的2.25次根号 if position.distance_to(targetPlayer.position) < targetPlayer.fields.get(FieldStore.Entity.DROPPED_ITEM_COLLECT_RADIUS): if targetPlayer.sprinting: apply_central_force((position - targetPlayer.texture.global_position).normalized() * targetPlayer.velocity.length() * 10) else: targetPlayer.collectItem(item, stackCount) collect() else: refindPlayer(get_tree()) else: refindPlayer(get_tree()) func canICollect(): return is_instance_valid(targetPlayer) && targetPlayer.inventoryMax[item] > targetPlayer.inventory[item] func collect(): collecting = true animator.play("collect") await animator.animation_finished queue_free() func refindPlayer(tree: SceneTree): targetPlayer = EntityTool.findClosetPlayer(position, tree) static func generate( itemType: ItemStore.ItemType, count: int, spawnPosition: Vector2, addToWorld: bool = true ): var instance: ItemDropped = ComponentManager.getUIComponent("ItemDropped").instantiate() instance.item = itemType instance.stackCount = count instance.position = spawnPosition if addToWorld: WorldManager.rootNode.call_deferred("add_child", instance) instance.add_to_group("drops") instance.refindPlayer(WorldManager.tree) instance.add_to_group("items") return instance static func getDrops() -> Array[ItemDropped]: var result: Array[ItemDropped] = [] for drop in WorldManager.tree.get_nodes_in_group("drops"): result.append(drop) return result static func getDropsCanCollet() -> Array[ItemDropped]: var result: Array[ItemDropped] = [] for drop in getDrops(): if drop.canICollect(): result.append(drop) return result static func itemCount(): return len(getDrops())