1
1
mirror of https://github.com/Rundll86/Dog-Lynx-And-HCN.git synced 2026-05-28 06:51:54 +08:00

refactor(Statemachine): 优化实体和物品掉落管理逻辑

重构 EntityBase 的 mobCount 方法为 getMobs 以返回完整列表
在 WorldManager 中添加 canNextWave 方法检查怪物和可收集物品
优化 ItemDropped 的玩家查找和收集条件判断逻辑
This commit is contained in:
2025-12-14 14:07:29 +08:00
parent 7168c690f3
commit c0d5faf668
3 changed files with 25 additions and 7 deletions
+2 -2
View File
@@ -489,5 +489,5 @@ static func generate(
if addToWorld:
WorldManager.rootNode.spawn(instance)
return instance
static func mobCount():
return len(WorldManager.tree.get_nodes_in_group("mobs"))
static func getMobs():
return WorldManager.tree.get_nodes_in_group("mobs")
+20 -3
View File
@@ -21,12 +21,10 @@ func _ready():
func _process(_delta):
texture.texture = ItemStore.getTexture(item)
func _physics_process(_delta):
if !is_instance_valid(targetPlayer):
targetPlayer = EntityTool.findClosetPlayer(position, WorldManager.tree)
if is_instance_valid(targetPlayer):
if collecting:
linear_velocity = Vector2.ZERO
elif targetPlayer.inventoryMax[item] > targetPlayer.inventory[item]:
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)
@@ -37,12 +35,20 @@ func _physics_process(_delta):
else:
targetPlayer.collectItem(item, stackCount)
collect()
else:
refindPlayer()
else:
refindPlayer()
func canICollect():
return targetPlayer.inventoryMax[item] > targetPlayer.inventory[item]
func collect():
collecting = true
animator.play("collect")
await animator.animation_finished
queue_free()
func refindPlayer():
targetPlayer = EntityTool.findClosetPlayer(position, get_tree())
static func generate(
itemType: ItemStore.ItemType,
@@ -56,4 +62,15 @@ static func generate(
instance.position = spawnPosition
if addToWorld:
WorldManager.rootNode.call_deferred("add_child", instance)
instance.add_to_group("drops")
return instance
static func getDrops() -> Array[ItemDropped]:
return WorldManager.tree.get_nodes_in_group("drops") as Array[ItemDropped]
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())
+3 -2
View File
@@ -15,13 +15,15 @@ func _ready():
spawner.spawn_function = justReturn
func _physics_process(delta):
runningTime += delta * 1000
if EntityBase.mobCount() == 0 and runningTime > 1000:
if canNextWave() and runningTime > 1000:
UIState.setPanel("MakeFeed")
@rpc("authority")
func nextWave(waves: Array):
Wave.next(waves)
func canNextWave():
return len(EntityBase.getMobs()) == 0 and len(ItemDropped.getDropsCanCollet()) == 0
func spawnWave():
var waves = Wave.spawn()
nextWave(waves)
@@ -34,7 +36,6 @@ func spawn(node: Node):
else:
add_child(node)
func justReturn(data):
print(ArrayTool.parseEncodedObject(data))
return ArrayTool.parseEncodedObject(data)[0]
static func getTime():