mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-06 14:10:55 +03:00
Use static typing in all demos (#1063)
This leads to code that is easier to understand and runs faster thanks to GDScript's typed instructions. The untyped declaration warning is now enabled on all projects where type hints were added. All projects currently run without any untyped declration warnings. Dodge the Creeps and Squash the Creeps demos intentionally don't use type hints to match the documentation, where type hints haven't been adopted yet (given its beginner focus).
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
extends Test
|
||||
|
||||
|
||||
const BOX_SIZE = Vector3(0.8, 0.8, 0.8)
|
||||
const BOX_SPACE = Vector3(1.0, 1.0, 1.0)
|
||||
|
||||
@export_range(1, 1000) var row_size = 20
|
||||
@export_range(1, 1000) var column_size = 20
|
||||
@export_range(1, 1000) var depth_size = 20
|
||||
@export_range(1, 1000) var row_size := 20
|
||||
@export_range(1, 1000) var column_size := 20
|
||||
@export_range(1, 1000) var depth_size := 20
|
||||
|
||||
var _objects = []
|
||||
var _objects: Array[Node3D] = []
|
||||
|
||||
var _log_physics = false
|
||||
var _log_physics_time = 0
|
||||
var _log_physics_time_start = 0
|
||||
var _log_physics := false
|
||||
var _log_physics_time := 0
|
||||
var _log_physics_time_start := 0
|
||||
|
||||
|
||||
func _ready():
|
||||
func _ready() -> void:
|
||||
await start_timer(1.0).timeout
|
||||
if is_timer_canceled():
|
||||
return
|
||||
@@ -67,49 +65,49 @@ func _ready():
|
||||
Log.print_log("* Done.")
|
||||
|
||||
|
||||
func _exit_tree():
|
||||
func _exit_tree() -> void:
|
||||
for object in _objects:
|
||||
object.free()
|
||||
|
||||
|
||||
func _physics_process(delta):
|
||||
func _physics_process(delta: float) -> void:
|
||||
super._physics_process(delta)
|
||||
|
||||
if _log_physics:
|
||||
var time = Time.get_ticks_usec()
|
||||
var time_delta = time - _log_physics_time
|
||||
var time_total = time - _log_physics_time_start
|
||||
var time := Time.get_ticks_usec()
|
||||
var time_delta := time - _log_physics_time
|
||||
var time_total := time - _log_physics_time_start
|
||||
_log_physics_time = time
|
||||
Log.print_log(" Physics Tick: %.3f ms (total = %.3f ms)" % [0.001 * time_delta, 0.001 * time_total])
|
||||
|
||||
|
||||
func _log_physics_start():
|
||||
func _log_physics_start() -> void:
|
||||
_log_physics = true
|
||||
_log_physics_time_start = Time.get_ticks_usec()
|
||||
_log_physics_time = _log_physics_time_start
|
||||
|
||||
|
||||
func _log_physics_stop():
|
||||
func _log_physics_stop() -> void:
|
||||
_log_physics = false
|
||||
|
||||
|
||||
func _create_objects():
|
||||
func _create_objects() -> void:
|
||||
_objects.clear()
|
||||
|
||||
Log.print_log("* Creating objects...")
|
||||
var timer = Time.get_ticks_usec()
|
||||
var timer := Time.get_ticks_usec()
|
||||
|
||||
var pos_x = -0.5 * (row_size - 1) * BOX_SPACE.x
|
||||
var pos_x := -0.5 * (row_size - 1) * BOX_SPACE.x
|
||||
|
||||
for row in row_size:
|
||||
var pos_y = -0.5 * (column_size - 1) * BOX_SPACE.y
|
||||
var pos_y := -0.5 * (column_size - 1) * BOX_SPACE.y
|
||||
|
||||
for column in column_size:
|
||||
var pos_z = -0.5 * (depth_size - 1) * BOX_SPACE.z
|
||||
var pos_z := -0.5 * (depth_size - 1) * BOX_SPACE.z
|
||||
|
||||
for depth in depth_size:
|
||||
# Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
|
||||
var box = create_rigidbody_box(BOX_SIZE)
|
||||
var box: RigidBody3D = create_rigidbody_box(BOX_SIZE)
|
||||
box.gravity_scale = 0.0
|
||||
box.transform.origin = Vector3(pos_x, pos_y, pos_z)
|
||||
_objects.push_back(box)
|
||||
@@ -124,11 +122,11 @@ func _create_objects():
|
||||
Log.print_log(" Create Time: %.3f ms" % (0.001 * timer))
|
||||
|
||||
|
||||
func _add_objects():
|
||||
var root_node = $Objects
|
||||
func _add_objects() -> void:
|
||||
var root_node: Node3D = $Objects
|
||||
|
||||
Log.print_log("* Adding objects...")
|
||||
var timer = Time.get_ticks_usec()
|
||||
var timer := Time.get_ticks_usec()
|
||||
|
||||
for object in _objects:
|
||||
root_node.add_child(object)
|
||||
@@ -137,9 +135,9 @@ func _add_objects():
|
||||
Log.print_log(" Add Time: %.3f ms" % (0.001 * timer))
|
||||
|
||||
|
||||
func _move_objects():
|
||||
func _move_objects() -> void:
|
||||
Log.print_log("* Moving objects...")
|
||||
var timer = Time.get_ticks_usec()
|
||||
var timer := Time.get_ticks_usec()
|
||||
|
||||
for object in _objects:
|
||||
object.transform.origin += BOX_SPACE
|
||||
@@ -148,15 +146,15 @@ func _move_objects():
|
||||
Log.print_log(" Move Time: %.3f ms" % (0.001 * timer))
|
||||
|
||||
|
||||
func _remove_objects():
|
||||
var root_node = $Objects
|
||||
func _remove_objects() -> void:
|
||||
var root_node: Node3D = $Objects
|
||||
|
||||
Log.print_log("* Removing objects...")
|
||||
var timer = Time.get_ticks_usec()
|
||||
var timer := Time.get_ticks_usec()
|
||||
|
||||
# Remove objects in reversed order to avoid the overhead of changing children index in parent.
|
||||
var object_count = _objects.size()
|
||||
for object_index in range(object_count):
|
||||
var object_count := _objects.size()
|
||||
for object_index in object_count:
|
||||
root_node.remove_child(_objects[object_count - object_index - 1])
|
||||
|
||||
timer = Time.get_ticks_usec() - timer
|
||||
|
||||
Reference in New Issue
Block a user