Add basic GUI

soundtrack
Ensar Sarajčić 2021-11-13 23:55:18 +01:00
parent 0499ef52cd
commit 2ab280fb92
10 changed files with 198 additions and 2 deletions

BIN
assets/gauge.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/gauge.png-9154e7a04792a5507193a3fa8e514e90.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/gauge.png"
dest_files=[ "res://.import/gauge.png-9154e7a04792a5507193a3fa8e514e90.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

BIN
assets/needle.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/needle.png-0f8fd3822e6cc0a1cd19175fda72da7b.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/needle.png"
dest_files=[ "res://.import/needle.png-0f8fd3822e6cc0a1cd19175fda72da7b.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

26
player/gui.gd 100644
View File

@ -0,0 +1,26 @@
extends MarginContainer
onready var rpm_needle = $HBoxContainer/RPMGauge/RPMNeedle
onready var speed_needle = $HBoxContainer/SpeedGauge/SpeedNeedle
onready var gear_label = $HBoxContainer/GearLabel
var min_rotation = -85
var max_rotation = 75
func _get_rotation(percent: float) -> float:
return min_rotation + (max_rotation - min_rotation) * percent
func update_speed(speed_percent: float) -> void:
speed_needle.rect_rotation = _get_rotation(speed_percent)
func update_rpm(rpm_percent: float) -> void:
rpm_needle.rect_rotation = _get_rotation(rpm_percent)
func update_gear(gear: int) -> void:
if gear == -1:
gear_label.text = "R"
elif gear == 0:
gear_label.text = "N"
else:
gear_label.text = str(gear)

82
player/gui.tscn 100644
View File

@ -0,0 +1,82 @@
[gd_scene load_steps=7 format=2]
[ext_resource path="res://assets/gauge.png" type="Texture" id=1]
[ext_resource path="res://assets/needle.png" type="Texture" id=2]
[ext_resource path="res://player/gui.gd" type="Script" id=3]
[ext_resource path="res://assets/fonts/kenney-future-narrow.ttf" type="DynamicFontData" id=4]
[sub_resource type="CanvasItemMaterial" id=3]
[sub_resource type="DynamicFont" id=1]
size = 48
font_data = ExtResource( 4 )
[node name="GUIContainer" type="MarginContainer"]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 20.0
margin_top = 20.0
margin_right = -20.0
margin_bottom = -20.0
script = ExtResource( 3 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="HBoxContainer" type="HBoxContainer" parent="."]
material = SubResource( 3 )
margin_top = 432.0
margin_right = 318.0
margin_bottom = 560.0
size_flags_horizontal = 0
size_flags_vertical = 8
custom_constants/separation = 20
__meta__ = {
"_edit_use_anchors_": false
}
[node name="RPMGauge" type="TextureRect" parent="HBoxContainer"]
margin_right = 128.0
margin_bottom = 128.0
texture = ExtResource( 1 )
[node name="RPMNeedle" type="TextureRect" parent="HBoxContainer/RPMGauge"]
margin_left = 54.0
margin_top = 11.0
margin_right = 72.0
margin_bottom = 66.0
rect_pivot_offset = Vector2( 9, 50 )
size_flags_horizontal = 5
size_flags_vertical = 5
texture = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="SpeedGauge" type="TextureRect" parent="HBoxContainer"]
margin_left = 148.0
margin_right = 276.0
margin_bottom = 128.0
texture = ExtResource( 1 )
[node name="SpeedNeedle" type="TextureRect" parent="HBoxContainer/SpeedGauge"]
margin_left = 54.0
margin_top = 11.0
margin_right = 72.0
margin_bottom = 66.0
rect_pivot_offset = Vector2( 9, 50 )
size_flags_horizontal = 5
size_flags_vertical = 5
texture = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="GearLabel" type="Label" parent="HBoxContainer"]
margin_left = 296.0
margin_top = 37.0
margin_right = 318.0
margin_bottom = 91.0
custom_colors/font_color = Color( 0.282353, 0.0196078, 0.0196078, 1 )
custom_fonts/font = SubResource( 1 )
text = "1"

View File

@ -3,6 +3,7 @@ extends Panel
const buggy = preload("res://vehicles/buggy.tscn") const buggy = preload("res://vehicles/buggy.tscn")
const beetle = preload("res://vehicles/beetlecar.tscn") const beetle = preload("res://vehicles/beetlecar.tscn")
const test_scene = preload("res://scenes/test_level.tscn") const test_scene = preload("res://scenes/test_level.tscn")
const gui_scene = preload("res://player/gui.tscn")
func _on_BuggyButton_pressed() -> void: func _on_BuggyButton_pressed() -> void:
_start_with_vehicle(buggy.instance()) _start_with_vehicle(buggy.instance())
@ -11,8 +12,12 @@ func _on_BeetleButton_pressed() -> void:
_start_with_vehicle(beetle.instance()) _start_with_vehicle(beetle.instance())
func _start_with_vehicle(vehicle: Node) -> void: func _start_with_vehicle(vehicle: Node) -> void:
var gui = gui_scene.instance()
var scene = test_scene.instance() var scene = test_scene.instance()
scene.call_deferred("spawn_player", vehicle) vehicle.connect("speed_updated", gui, "update_speed")
vehicle.connect("rpm_updated", gui, "update_rpm")
vehicle.connect("gear_updated", gui, "update_gear")
scene.call_deferred("spawn_player", vehicle, gui)
get_tree().root.call_deferred("add_child", scene) get_tree().root.call_deferred("add_child", scene)
queue_free() queue_free()

View File

@ -3,13 +3,16 @@ extends Spatial
const camera = preload("res://player/camera.tscn") const camera = preload("res://player/camera.tscn")
onready var spawn_point = $PlayerSpawnLocation onready var spawn_point = $PlayerSpawnLocation
var player_node: Node var player_node: Node
var gui: Node
func _ready() -> void: func _ready() -> void:
player_node.global_transform = spawn_point.global_transform player_node.global_transform = spawn_point.global_transform
add_child(player_node) add_child(player_node)
add_child(gui)
var player_camera = camera.instance() var player_camera = camera.instance()
player_camera.follow_target_path = player_node.get_path() player_camera.follow_target_path = player_node.get_path()
add_child(player_camera) add_child(player_camera)
func spawn_player(player_node: Node) -> void: func spawn_player(player_node: Node, gui: Node) -> void:
self.player_node = player_node self.player_node = player_node
self.gui = gui

View File

@ -44,6 +44,7 @@ rings = 3
[node name="beetlecar" instance=ExtResource( 2 )] [node name="beetlecar" instance=ExtResource( 2 )]
mass = 140.0 mass = 140.0
script = ExtResource( 1 ) script = ExtResource( 1 )
EXPECTED_MAX_SPEED = 180.0
gear_ratios = [ 2.95, 2.2, 1.76, 1.43, 1.25, 1.0 ] gear_ratios = [ 2.95, 2.2, 1.76, 1.43, 1.25, 1.0 ]
final_drive = 5.0 final_drive = 5.0
max_rpm = 8000.0 max_rpm = 8000.0

View File

@ -1,5 +1,9 @@
extends VehicleBody extends VehicleBody
signal speed_updated(speed_percent)
signal rpm_updated(rpm_percent)
signal gear_updated(gear)
export (float) var MAX_STEER_ANGLE = 25 export (float) var MAX_STEER_ANGLE = 25
export (float) var SPEED_STEER_ANGLE = 10 export (float) var SPEED_STEER_ANGLE = 10
export (float) var MAX_STEER_SPEED = 100.0 export (float) var MAX_STEER_SPEED = 100.0
@ -15,6 +19,7 @@ export (float) var MAX_BRAKE_FORCE = 50.0
export (float) var THROTTLE_POWER = 6000.0 export (float) var THROTTLE_POWER = 6000.0
export (float) var MAX_RPM_LOSS_PS = 3000.0 export (float) var MAX_RPM_LOSS_PS = 3000.0
export (float) var BASE_ENGINE_PITCH = 0.5 export (float) var BASE_ENGINE_PITCH = 0.5
export (float) var EXPECTED_MAX_SPEED = 200
export (Array) var gear_ratios = [ 3.4, 2.5, 2.0, 1.5, 1.25 ] export (Array) var gear_ratios = [ 3.4, 2.5, 2.0, 1.5, 1.25 ]
export (float) var reverse_ratio = -3 export (float) var reverse_ratio = -3
@ -69,10 +74,12 @@ func _handle_gear_switch(delta: float):
if gear + 1 <= gear_ratios.size(): if gear + 1 <= gear_ratios.size():
gear += 1 gear += 1
gear_timer = gear_switch_time * (2 - clutch_position) gear_timer = gear_switch_time * (2 - clutch_position)
emit_signal("gear_updated", gear)
if Input.is_action_just_pressed("gear_down"): if Input.is_action_just_pressed("gear_down"):
if gear - 1 >= -1: if gear - 1 >= -1:
gear -= 1 gear -= 1
gear_timer = gear_switch_time * (2 - clutch_position) gear_timer = gear_switch_time * (2 - clutch_position)
emit_signal("gear_updated", gear)
func _has_traction(): func _has_traction():
for wheel in traction_wheels: for wheel in traction_wheels:
@ -132,6 +139,8 @@ func _physics_process(delta: float):
rlwheel.brake = handbrake * MAX_BRAKE_FORCE / 2 rlwheel.brake = handbrake * MAX_BRAKE_FORCE / 2
var speed = wheel_rpm * 2.0 * PI * rrwheel.wheel_radius / 60.0 * 3600.0 / 1000.0 var speed = wheel_rpm * 2.0 * PI * rrwheel.wheel_radius / 60.0 * 3600.0 / 1000.0
emit_signal("speed_updated", speed / EXPECTED_MAX_SPEED)
emit_signal("rpm_updated", rpm_factor)
$Info.text = "Gear: %d, KPH: %.0f, RPM: %.0f, WheelRPM: %.0f, FinalRPM: %.0f, TRPM: %.0f, Engine force: %.0f" % [ gear, speed, rpm, wheel_rpm, final_rpm, transmission_rpm, engine_force ] $Info.text = "Gear: %d, KPH: %.0f, RPM: %.0f, WheelRPM: %.0f, FinalRPM: %.0f, TRPM: %.0f, Engine force: %.0f" % [ gear, speed, rpm, wheel_rpm, final_rpm, transmission_rpm, engine_force ]
var steering_input = Input.get_action_strength("steer_left") - Input.get_action_strength("steer_right") var steering_input = Input.get_action_strength("steer_left") - Input.get_action_strength("steer_right")