@@ -10,12 +10,15 @@ const OPTION_TYPE_CONCAVE_POLYGON = "Shape type/Concave Polygon"
1010
1111export (Array ) var spawns = Array ()
1212export (int ) var spawn_count = 100
13- export (int , 1 , 10 ) var spawn_multiplier = 5
1413
1514onready var options = $ Options
1615
1716var _object_templates = []
1817
18+ var _log_physics = false
19+ var _log_physics_time = 0
20+ var _log_physics_time_start = 0
21+
1922
2023func _ready ():
2124 yield (start_timer (0.5 ), "timeout" )
@@ -40,6 +43,25 @@ func _ready():
4043 _start_all_types ()
4144
4245
46+ func _physics_process (_delta ):
47+ if _log_physics :
48+ var time = OS .get_ticks_usec ()
49+ var time_delta = time - _log_physics_time
50+ var time_total = time - _log_physics_time_start
51+ _log_physics_time = time
52+ Log .print_log (" Physics Tick: %.3f ms (total = %.3f ms)" % [0.001 * time_delta , 0.001 * time_total ])
53+
54+
55+ func _log_physics_start ():
56+ _log_physics = true
57+ _log_physics_time_start = OS .get_ticks_usec ()
58+ _log_physics_time = _log_physics_time_start
59+
60+
61+ func _log_physics_stop ():
62+ _log_physics = false
63+
64+
4365func _exit_tree ():
4466 for object_template in _object_templates :
4567 object_template .free ()
@@ -66,7 +88,7 @@ func _on_option_selected(option):
6688
6789
6890func _find_type_index (type_name ):
69- for type_index in _object_templates .size ():
91+ for type_index in range ( _object_templates .size () ):
7092 var type_node = _object_templates [type_index ]
7193 if type_node .name .find (type_name ) > - 1 :
7294 return type_index
@@ -85,44 +107,47 @@ func _start_type(type_index):
85107 if is_timer_canceled ():
86108 return
87109
110+ _log_physics_start ()
111+
88112 _spawn_objects (type_index )
89113
114+ yield (wait_for_physics_ticks (5 ), "wait_done" )
115+ _log_physics_stop ()
116+
90117 yield (start_timer (1.0 ), "timeout" )
91118 if is_timer_canceled ():
92119 return
93120
121+ _log_physics_start ()
122+
94123 _activate_objects ()
95124
125+ yield (wait_for_physics_ticks (5 ), "wait_done" )
126+ _log_physics_stop ()
127+
96128 yield (start_timer (5.0 ), "timeout" )
97129 if is_timer_canceled ():
98130 return
99131
100- _despawn_objects ()
101-
102- Log .print_log ("* Done." )
132+ _log_physics_start ()
103133
134+ _despawn_objects ()
104135
105- func _start_all_types ():
106- for type_index in _object_templates .size ():
107- yield (start_timer (1.0 ), "timeout" )
108- if is_timer_canceled ():
109- return
136+ yield (wait_for_physics_ticks (5 ), "wait_done" )
137+ _log_physics_stop ()
110138
111- _spawn_objects ( type_index )
139+ yield ( start_timer ( 1.0 ), "timeout" )
112140
113- yield (start_timer (1.0 ), "timeout" )
114- if is_timer_canceled ():
115- return
116141
117- _activate_objects ()
142+ func _start_all_types ():
143+ Log .print_log ("* Start all types." )
118144
119- yield (start_timer (5.0 ), "timeout" )
145+ for type_index in range (_object_templates .size ()):
146+ yield (_start_type (type_index ), "completed" )
120147 if is_timer_canceled ():
121148 return
122149
123- _despawn_objects ()
124-
125- Log .print_log ("* Done." )
150+ Log .print_log ("* Done all types." )
126151
127152
128153func _spawn_objects (type_index ):
@@ -132,33 +157,37 @@ func _spawn_objects(type_index):
132157
133158 Log .print_log ("* Spawning: " + template_node .name )
134159
135- for _index in range (spawn_multiplier ):
136- for _node_index in spawn_count / spawn_multiplier :
137- var node = template_node .duplicate () as Node2D
138- spawn_parent .add_child (node )
160+ for _node_index in range (spawn_count ):
161+ # Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
162+ var collision = template_node .get_child (0 )
163+ var body = create_rigidbody_collision (collision , false , collision .transform )
164+ body .set_sleeping (true )
165+ spawn_parent .add_child (body )
139166
140167
141168func _activate_objects ():
142- var spawn_parent = $ SpawnTarget1
169+ for spawn in spawns :
170+ var spawn_parent = get_node (spawn )
143171
144- Log .print_log ("* Activating" )
172+ Log .print_log ("* Activating" )
145173
146- for node_index in spawn_parent .get_child_count ():
147- var node = spawn_parent .get_child (node_index ) as RigidBody2D
148- node .set_sleeping (false )
174+ for node_index in range ( spawn_parent .get_child_count () ):
175+ var node = spawn_parent .get_child (node_index ) as RigidBody2D
176+ node .set_sleeping (false )
149177
150178
151179func _despawn_objects ():
152180 for spawn in spawns :
153181 var spawn_parent = get_node (spawn )
154182
155- if spawn_parent .get_child_count () == 0 :
156- return
183+ var object_count = spawn_parent .get_child_count ()
184+ if object_count == 0 :
185+ continue
157186
158187 Log .print_log ("* Despawning" )
159188
160- while spawn_parent . get_child_count ():
161- var node_index = spawn_parent . get_child_count () - 1
162- var node = spawn_parent .get_child (node_index )
189+ # Remove objects in reversed order to avoid the overhead of changing children index in parent.
190+ for object_index in range ( object_count ):
191+ var node = spawn_parent .get_child (object_count - object_index - 1 )
163192 spawn_parent .remove_child (node )
164193 node .queue_free ()
0 commit comments