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:
Hugo Locurcio
2024-06-01 12:12:18 +02:00
committed by GitHub
parent 8e9c180278
commit bac1e69164
498 changed files with 5218 additions and 4776 deletions

File diff suppressed because one or more lines are too long

View File

@@ -104,7 +104,7 @@ data = {
environment = SubResource("Environment_qep2e")
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(-0.766045, 0.45452, -0.45452, 0, 0.707107, 0.707107, 0.642788, 0.541676, -0.541675, 0, 5, 0)
transform = Transform3D(-0.436154, 0.776648, -0.454519, 0.353553, 0.612372, 0.707107, 0.827508, 0.147711, -0.541675, 0, 5, 0)
light_energy = 1.3
shadow_enabled = true
shadow_bias = 0.032

View File

@@ -5,11 +5,11 @@ const JUMP_SPEED = 6.5
const ACCELERATION = 4
const DECELERATION = 4
@onready var camera = $Target/Camera3D
@onready var gravity = -ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var start_position = position
@onready var camera: Camera3D = $Target/Camera3D
@onready var gravity := float(-ProjectSettings.get_setting("physics/3d/default_gravity"))
@onready var start_position := position
func _physics_process(delta):
func _physics_process(delta: float) -> void:
if Input.is_action_just_pressed(&"exit"):
get_tree().quit()
if Input.is_action_just_pressed(&"reset_position") or global_position.y < - 6:
@@ -17,17 +17,18 @@ func _physics_process(delta):
position = start_position
velocity = Vector3.ZERO
var dir = Vector3()
var dir := Vector3()
dir.x = Input.get_axis(&"move_left", &"move_right")
dir.z = Input.get_axis(&"move_forward", &"move_back")
# Get the camera's transform basis, but remove the X rotation such
# that the Y axis is up and Z is horizontal.
var cam_basis = camera.global_transform.basis
var cam_basis := camera.global_transform.basis
cam_basis = cam_basis.rotated(cam_basis.x, -cam_basis.get_euler().x)
dir = cam_basis * dir
# Limit the input to a length of 1. length_squared is faster to check.
# Limit the input to a length of 1. `length_squared()` is faster to check
# than `length()`.
if dir.length_squared() > 1:
dir /= dir.length()
@@ -35,11 +36,11 @@ func _physics_process(delta):
velocity.y += delta * gravity
# Using only the horizontal velocity, interpolate towards the input.
var hvel = velocity
var hvel := velocity
hvel.y = 0
var target = dir * MAX_SPEED
var acceleration
var target := dir * MAX_SPEED
var acceleration := 0.0
if dir.dot(hvel) > 0:
acceleration = ACCELERATION
else:
@@ -60,6 +61,6 @@ func _physics_process(delta):
velocity.y = JUMP_SPEED
func _on_tcube_body_entered(body):
func _on_tcube_body_entered(body: PhysicsBody3D) -> void:
if body == self:
get_node(^"WinText").show()
$WinText.show()

View File

@@ -1,37 +1,36 @@
extends Camera3D
# Member variables
var collision_exception = []
@export var min_distance = 0.5
@export var max_distance = 3.0
@export var angle_v_adjust = 0.0
var max_height = 2.0
var min_height = 0
@export var min_distance := 0.5
@export var max_distance := 3.0
@export var angle_v_adjust := 0.0
var collision_exception := []
var max_height := 2.0
var min_height := 0
@onready var target_node: Node3D = get_parent()
func _ready():
func _ready() -> void:
collision_exception.append(target_node.get_parent().get_rid())
# Detaches the camera transform from the parent spatial node
set_as_top_level(true)
# Detaches the camera transform from the parent spatial node.
top_level = true
func _physics_process(_delta):
var target_pos: Vector3 = target_node.global_transform.origin
var camera_pos: Vector3 = global_transform.origin
func _physics_process(_delta: float) -> void:
var target_pos := target_node.global_transform.origin
var camera_pos := global_transform.origin
var delta_pos: Vector3 = camera_pos - target_pos
var delta_pos := camera_pos - target_pos
# Regular delta follow
# Regular delta follow.
# Check ranges
# Check ranges.
if delta_pos.length() < min_distance:
delta_pos = delta_pos.normalized() * min_distance
elif delta_pos.length() > max_distance:
delta_pos = delta_pos.normalized() * max_distance
# Check upper and lower height
# Check upper and lower height.
if delta_pos.y > max_height:
delta_pos.y = max_height
if delta_pos.y < min_height:
@@ -41,7 +40,7 @@ func _physics_process(_delta):
look_at_from_position(camera_pos, target_pos, Vector3.UP)
# Turn a little up or down
var t = transform
# Turn a little up or down.
var t := transform
t.basis = Basis(t.basis[0], deg_to_rad(angle_v_adjust)) * t.basis
transform = t

View File

@@ -18,6 +18,10 @@ run/main_scene="res://level.tscn"
config/features=PackedStringArray("4.2")
config/icon="res://icon.webp"
[debug]
gdscript/warnings/untyped_declaration=1
[display]
window/stretch/mode="canvas_items"