mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 23:10:08 +01: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:
@@ -4,28 +4,28 @@ const ROT_SPEED = 0.003
|
||||
const ZOOM_SPEED = 0.125
|
||||
const MAIN_BUTTONS = MOUSE_BUTTON_MASK_LEFT | MOUSE_BUTTON_MASK_RIGHT | MOUSE_BUTTON_MASK_MIDDLE
|
||||
|
||||
var tester_index = 0
|
||||
var rot_x = -TAU / 16 # This must be kept in sync with RotationX.
|
||||
var rot_y = TAU / 8 # This must be kept in sync with CameraHolder.
|
||||
var camera_distance = 2.0
|
||||
var base_height = ProjectSettings.get_setting("display/window/size/viewport_height")
|
||||
var tester_index := 0
|
||||
var rot_x := -TAU / 16 # This must be kept in sync with RotationX.
|
||||
var rot_y := TAU / 8 # This must be kept in sync with CameraHolder.
|
||||
var camera_distance := 2.0
|
||||
var base_height: = int(ProjectSettings.get_setting("display/window/size/viewport_height"))
|
||||
|
||||
@onready var testers = $Testers
|
||||
@onready var camera_holder = $CameraHolder # Has a position and rotates on Y.
|
||||
@onready var rotation_x = $CameraHolder/RotationX
|
||||
@onready var camera = $CameraHolder/RotationX/Camera3D
|
||||
@onready var testers: Node3D = $Testers
|
||||
@onready var camera_holder: Node3D = $CameraHolder # Has a position and rotates on Y.
|
||||
@onready var rotation_x: Node3D = $CameraHolder/RotationX
|
||||
@onready var camera: Camera3D = $CameraHolder/RotationX/Camera3D
|
||||
|
||||
|
||||
func _ready():
|
||||
func _ready() -> void:
|
||||
camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
|
||||
rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
|
||||
update_gui()
|
||||
|
||||
|
||||
func _unhandled_input(event):
|
||||
if event.is_action_pressed("ui_left"):
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed(&"ui_left"):
|
||||
_on_previous_pressed()
|
||||
if event.is_action_pressed("ui_right"):
|
||||
if event.is_action_pressed(&"ui_right"):
|
||||
_on_next_pressed()
|
||||
|
||||
if event is InputEventMouseButton:
|
||||
@@ -37,7 +37,7 @@ func _unhandled_input(event):
|
||||
|
||||
if event is InputEventMouseMotion and event.button_mask & MAIN_BUTTONS:
|
||||
# Compensate motion speed to be resolution-independent (based on the window height).
|
||||
var relative_motion = event.relative * DisplayServer.window_get_size().y / base_height
|
||||
var relative_motion: Vector2 = event.relative * DisplayServer.window_get_size().y / base_height
|
||||
rot_y -= relative_motion.x * ROT_SPEED
|
||||
rot_x -= relative_motion.y * ROT_SPEED
|
||||
rot_x = clamp(rot_x, -1.57, 0)
|
||||
@@ -45,26 +45,26 @@ func _unhandled_input(event):
|
||||
rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
|
||||
|
||||
|
||||
func _process(delta):
|
||||
var current_tester = testers.get_child(tester_index)
|
||||
func _process(delta: float) -> void:
|
||||
var current_tester: Node3D = testers.get_child(tester_index)
|
||||
# This code assumes CameraHolder's X and Y coordinates are already correct.
|
||||
var current_position = camera_holder.global_transform.origin.z
|
||||
var target_position = current_tester.global_transform.origin.z
|
||||
var current_position := camera_holder.global_transform.origin.z
|
||||
var target_position := current_tester.global_transform.origin.z
|
||||
camera_holder.global_transform.origin.z = lerpf(current_position, target_position, 3 * delta)
|
||||
camera.position.z = lerpf(camera.position.z, camera_distance, 10 * delta)
|
||||
|
||||
|
||||
func _on_previous_pressed():
|
||||
func _on_previous_pressed() -> void:
|
||||
tester_index = max(0, tester_index - 1)
|
||||
update_gui()
|
||||
|
||||
|
||||
func _on_next_pressed():
|
||||
func _on_next_pressed() -> void:
|
||||
tester_index = min(tester_index + 1, testers.get_child_count() - 1)
|
||||
update_gui()
|
||||
|
||||
|
||||
func update_gui():
|
||||
func update_gui() -> void:
|
||||
$TestName.text = str(testers.get_child(tester_index).name).capitalize()
|
||||
$Previous.disabled = tester_index == 0
|
||||
$Next.disabled = tester_index == testers.get_child_count() - 1
|
||||
@@ -74,5 +74,5 @@ func update_gui():
|
||||
$Testers/Label3DHealthBar/LineEdit.visible = str(testers.get_child(tester_index).name) == "Label3DHealthBar"
|
||||
|
||||
|
||||
func _on_line_edit_text_submitted(new_text):
|
||||
func _on_line_edit_text_submitted(_new_text: String) -> void:
|
||||
$Testers/Label3DHealthBar/LineEdit.release_focus()
|
||||
|
||||
@@ -146,10 +146,10 @@ environment = SubResource("11")
|
||||
script = ExtResource("18")
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
|
||||
autoplay = "move"
|
||||
libraries = {
|
||||
"": SubResource("AnimationLibrary_ecfcr")
|
||||
}
|
||||
autoplay = "move"
|
||||
|
||||
[node name="Plane" type="MeshInstance3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -22)
|
||||
@@ -286,7 +286,7 @@ text = "Label3D
|
||||
(MSDF)"
|
||||
font = ExtResource("4_omdth")
|
||||
font_size = 40
|
||||
outline_size = 9
|
||||
outline_size = 4
|
||||
|
||||
[node name="RasterMipmaps" type="Label3D" parent="Testers/Label3DFontTypes/AnimationOrigin"]
|
||||
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 1.8, 1)
|
||||
@@ -306,7 +306,7 @@ text = "Label3D
|
||||
mipmaps)"
|
||||
font = ExtResource("5_syv27")
|
||||
font_size = 40
|
||||
outline_size = 9
|
||||
outline_size = 4
|
||||
|
||||
[node name="Label3DBillboardModes" type="Node3D" parent="Testers"]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -16)
|
||||
|
||||
@@ -16,7 +16,7 @@ Rendering=null
|
||||
antialiasing=1
|
||||
generate_mipmaps=true
|
||||
multichannel_signed_distance_field=true
|
||||
msdf_pixel_range=8
|
||||
msdf_pixel_range=10
|
||||
msdf_size=48
|
||||
allow_system_fallback=true
|
||||
force_autohinter=false
|
||||
|
||||
@@ -16,7 +16,7 @@ Rendering=null
|
||||
antialiasing=1
|
||||
generate_mipmaps=false
|
||||
multichannel_signed_distance_field=true
|
||||
msdf_pixel_range=8
|
||||
msdf_pixel_range=10
|
||||
msdf_size=48
|
||||
allow_system_fallback=true
|
||||
force_autohinter=false
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
# must be adjusted instead of adjusting the `position` property.
|
||||
extends Node3D
|
||||
|
||||
var health = 0
|
||||
var counter = 0.0
|
||||
var health := 0: set = set_health
|
||||
var counter := 0.0
|
||||
|
||||
# The margin to apply between the name and health percentage (in pixels).
|
||||
const HEALTH_MARGIN = 25
|
||||
@@ -15,18 +15,26 @@ const HEALTH_MARGIN = 25
|
||||
# (since more characters may need to be rendered at once).
|
||||
const BAR_WIDTH = 100
|
||||
|
||||
|
||||
func _ready():
|
||||
func _ready() -> void:
|
||||
$LineEdit.text = $Name.text
|
||||
|
||||
|
||||
func _process(delta):
|
||||
func _process(delta: float) -> void:
|
||||
# Animate the health percentage.
|
||||
counter += delta
|
||||
set_health(50 + sin(counter * 0.5) * 50)
|
||||
health = roundi(50 + sin(counter * 0.5) * 50)
|
||||
|
||||
|
||||
func set_health(p_health):
|
||||
func _on_line_edit_text_changed(new_text: String) -> void:
|
||||
$Name.text = new_text
|
||||
|
||||
# Adjust name's font size to fit within the allowed width.
|
||||
$Name.font_size = 32
|
||||
while $Name.font.get_string_size($Name.text, $Name.horizontal_alignment, -1, $Name.font_size).x > $Name.width:
|
||||
$Name.font_size -= 1
|
||||
|
||||
|
||||
func set_health(p_health: int) -> void:
|
||||
health = p_health
|
||||
|
||||
$Health.text = "%d%%" % round(health)
|
||||
@@ -48,21 +56,12 @@ func set_health(p_health):
|
||||
|
||||
# Construct an health bar with `|` symbols brought very close to each other using
|
||||
# a custom FontVariation on the HealthBarForeground and HealthBarBackground nodes.
|
||||
var bar_text = ""
|
||||
var bar_text_bg = ""
|
||||
for i in round((health / 100.0) * BAR_WIDTH):
|
||||
var bar_text := ""
|
||||
var bar_text_bg := ""
|
||||
for i in roundi((health / 100.0) * BAR_WIDTH):
|
||||
bar_text += "|"
|
||||
for i in BAR_WIDTH:
|
||||
bar_text_bg += "|"
|
||||
|
||||
$HealthBarForeground.text = str(bar_text)
|
||||
$HealthBarBackground.text = str(bar_text_bg)
|
||||
|
||||
|
||||
func _on_line_edit_text_changed(new_text):
|
||||
$Name.text = new_text
|
||||
|
||||
# Adjust name's font size to fit within the allowed width.
|
||||
$Name.font_size = 32
|
||||
while $Name.font.get_string_size($Name.text, $Name.horizontal_alignment, -1, $Name.font_size).x > $Name.width:
|
||||
$Name.font_size -= 1
|
||||
|
||||
@@ -17,6 +17,10 @@ run/main_scene="res://3d_labels_and_texts.tscn"
|
||||
config/features=PackedStringArray("4.2")
|
||||
config/icon="res://icon.webp"
|
||||
|
||||
[debug]
|
||||
|
||||
gdscript/warnings/untyped_declaration=1
|
||||
|
||||
[display]
|
||||
|
||||
window/stretch/mode="canvas_items"
|
||||
|
||||
@@ -3,20 +3,19 @@
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://chjqieyps5n5r"
|
||||
path.s3tc="res://.godot/imported/checker.png-d334a8ae07de292fd4162f184b9dd7bc.s3tc.ctex"
|
||||
path="res://.godot/imported/checker.png-d334a8ae07de292fd4162f184b9dd7bc.ctex"
|
||||
metadata={
|
||||
"imported_formats": ["s3tc_bptc"],
|
||||
"vram_texture": true
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://textures/checker.png"
|
||||
dest_files=["res://.godot/imported/checker.png-d334a8ae07de292fd4162f184b9dd7bc.s3tc.ctex"]
|
||||
dest_files=["res://.godot/imported/checker.png-d334a8ae07de292fd4162f184b9dd7bc.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=2
|
||||
compress/mode=0
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/hdr_compression=1
|
||||
|
||||
Reference in New Issue
Block a user