Skip to content

Commit

Permalink
Combine update loops in game client
Browse files Browse the repository at this point in the history
  • Loading branch information
Kvel2D committed Apr 6, 2024
1 parent fab81f8 commit 28c6e2b
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions Scenes/GameScene/GameClient.gd
Original file line number Diff line number Diff line change
Expand Up @@ -201,45 +201,31 @@ func _execute_action(action: Dictionary):
Action.Type.CHANGE_BUFFGROUP: ActionChangeBuffgroup.execute(action, player)


# NOTE: need to check that nodes are inside tree during
# iteration because they may get removed during iteration.
# For example, timer_list is obtained once before iteration
# starts. Then timer A triggers an explosion which kills a
# creep which carries timer B. Timer B is now outside tree
# but still inside timer_list!
func _update_state():
_game_time.update(_tick_delta)

# NOTE: use separate groups so that update() calls are
# ordered by type. This makes gameplay logic more
# consistent.
var timer_list: Array = get_tree().get_nodes_in_group("manual_timers")
for timer in timer_list:
if !_should_update(timer):
continue

timer.update(_tick_delta)

var creep_list: Array[Creep] = Utils.get_creep_list()
for creep in creep_list:
if !_should_update(creep):
continue

creep.update(_tick_delta)

var projectile_list: Array = get_tree().get_nodes_in_group("projectiles")
for projectile in projectile_list:
if !_should_update(projectile):
continue

projectile.update(_tick_delta)

var tower_list: Array[Tower] = Utils.get_tower_list()
for tower in tower_list:
if !_should_update(tower):
var node_list: Array = []
node_list.append_array(timer_list)
node_list.append_array(creep_list)
node_list.append_array(projectile_list)
node_list.append_array(tower_list)

# NOTE: need to check is_inside_tree() because nodes may
# get removed during iteration. For example, timer_list is
# obtained once before iteration starts. Then timer A
# triggers an explosion which kills a creep which carries
# timer B. Timer B is now outside tree but still inside
# timer_list!
for node in node_list:
var should_update: bool = node.is_inside_tree() && !node.is_queued_for_deletion()
if !should_update:
continue

tower.update(_tick_delta)


func _should_update(node: Node) -> bool:
var should_update: bool = node.is_inside_tree() && !node.is_queued_for_deletion()

return should_update
node.update(_tick_delta)

0 comments on commit 28c6e2b

Please sign in to comment.