1
1
mirror of https://github.com/Rundll86/Dog-Lynx-And-HCN.git synced 2026-05-27 22:41:56 +08:00
Files
Dog-Lynx-And-HCN/scripts/Statemachine/ItemDropped.gd
T
fallingshrimp 30527a18a8 feat: 添加游戏控制和管理功能
- 新增GameControl节点用于统一处理游戏重启和退出
- 添加GameBusManager管理游戏重启时的资源清理
- 修改Pause和GameOver面板使用新的GameControl
- 为EffectController和ItemDropped添加分组管理
- 统一使用WorldManager管理游戏时间
2026-05-05 06:51:48 +08:00

82 lines
2.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():
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())