diff --git a/scripts/Statemachine/EntityBase.gd b/scripts/Statemachine/EntityBase.gd index 7605d13..36f1c9c 100644 --- a/scripts/Statemachine/EntityBase.gd +++ b/scripts/Statemachine/EntityBase.gd @@ -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") diff --git a/scripts/Statemachine/ItemDropped.gd b/scripts/Statemachine/ItemDropped.gd index fdefadf..d442688 100644 --- a/scripts/Statemachine/ItemDropped.gd +++ b/scripts/Statemachine/ItemDropped.gd @@ -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()) diff --git a/scripts/Tools/Managers/WorldManager.gd b/scripts/Tools/Managers/WorldManager.gd index 69466f1..c124993 100644 --- a/scripts/Tools/Managers/WorldManager.gd +++ b/scripts/Tools/Managers/WorldManager.gd @@ -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():