mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-05 23:40:07 +01:00
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).
39 lines
1.2 KiB
GDScript
39 lines
1.2 KiB
GDScript
extends Button
|
|
|
|
enum SpeedUnit {
|
|
METERS_PER_SECOND,
|
|
KILOMETERS_PER_HOUR,
|
|
MILES_PER_HOUR,
|
|
}
|
|
|
|
var gradient := Gradient.new()
|
|
var car_body: VehicleBody3D
|
|
|
|
@export var speed_unit: SpeedUnit = SpeedUnit.METERS_PER_SECOND
|
|
|
|
func _ready() -> void:
|
|
# The start and end points (offset 0.0 and 1.0) are already defined in a Gradient
|
|
# resource on creation. Override their colors and only create one new point.
|
|
gradient.set_color(0, Color(0.7, 0.9, 1.0))
|
|
gradient.set_color(1, Color(1.0, 0.3, 0.1))
|
|
gradient.add_point(0.2, Color(1.0, 1.0, 1.0))
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
var speed := car_body.linear_velocity.length()
|
|
if speed_unit == SpeedUnit.METERS_PER_SECOND:
|
|
text = "Speed: " + ("%.1f" % speed) + " m/s"
|
|
elif speed_unit == SpeedUnit.KILOMETERS_PER_HOUR:
|
|
speed *= 3.6
|
|
text = "Speed: " + ("%.0f" % speed) + " km/h"
|
|
else: # speed_unit == SpeedUnit.MILES_PER_HOUR:
|
|
speed *= 2.23694
|
|
text = "Speed: " + ("%.0f" % speed) + " mph"
|
|
|
|
# Change speedometer color depending on speed in m/s (regardless of unit).
|
|
add_theme_color_override("font_color", gradient.sample(remap(car_body.linear_velocity.length(), 0.0, 30.0, 0.0, 1.0)))
|
|
|
|
|
|
func _on_spedometer_pressed() -> void:
|
|
speed_unit = ((speed_unit + 1) % SpeedUnit.size()) as SpeedUnit
|