From cf0fbfa45faaf99f71fb9c11ca63853e0363450c Mon Sep 17 00:00:00 2001 From: KingGurke Date: Sat, 30 Sep 2023 21:36:13 +0200 Subject: [PATCH] Went to driving school --- project.godot | 12 +++++++++--- src/game/player.tscn | 5 ----- src/game/player_controller.gd | 26 ++++++++++++++++++++------ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/project.godot b/project.godot index 1d856f0..f30f100 100644 --- a/project.godot +++ b/project.godot @@ -38,9 +38,9 @@ accelerate={ } brake={ "deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null) +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null) ] } left={ @@ -55,3 +55,9 @@ right={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null) ] } +reverse={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} diff --git a/src/game/player.tscn b/src/game/player.tscn index bcc66e3..ac5592a 100644 --- a/src/game/player.tscn +++ b/src/game/player.tscn @@ -8,13 +8,9 @@ size = Vector2(36, 20) [node name="player" type="StaticBody2D" node_paths=PackedStringArray("camera")] script = ExtResource("1_g4ij8") -acc = 600.0 -dec = 400.0 -max_speed = 3000.0 turn_speed = 0.8 brake_strength = 600.0 drift_factor = 1.2 -camera_offset_factor = 0.5 camera = NodePath("Camera2D") [node name="Sprite2D" type="Sprite2D" parent="."] @@ -30,6 +26,5 @@ shape = SubResource("RectangleShape2D_w2nj7") [node name="Camera2D" type="Camera2D" parent="."] rotation = 1.5708 ignore_rotation = false -position_smoothing_enabled = true rotation_smoothing_enabled = true rotation_smoothing_speed = 10.0 diff --git a/src/game/player_controller.gd b/src/game/player_controller.gd index b1db54f..d9a2840 100644 --- a/src/game/player_controller.gd +++ b/src/game/player_controller.gd @@ -1,25 +1,30 @@ extends StaticBody2D # constants ## acceleration of the car while holding the accelerate button -@export var acc := 20. +@export var acc := 600. +## acceleration of the car while driving backwards +@export var rev := 500. ## natural deceleration of the car -@export var dec := 4. +@export var dec := 400. ## maximum speed of the car -@export var max_speed := 100. +@export var max_speed := 1200. +## maximum speed of the car while driving backwards +@export var max_reverse := 500. ## how many radians the car turns per second, higher value -> faster turns @export var turn_speed := 1. ## affects how fast the brakes, higher value -> quicker to stand still @export var brake_strength := 30. ## regulates how much more you can turn while breaking, values below 1 mean you can turn worse while drifting @export var drift_factor := 1.5 -## regulates how far the camera looks ahead of the player (affected by speed) -@export var camera_offset_factor = .2 +## maximum number of pixels how far the camera looks ahead of the player (affected by speed) +@export var camera_offset = 400. # references @export var camera : Camera2D # variables var momentum: Vector2 +#var direction = 0 # 1 for forward, 0 for standing still, -1 for reverse # Called when the node enters the scene tree for the first time. func _ready(): @@ -30,6 +35,7 @@ func _ready(): func _process(delta): var acc_d = acc * delta var dec_d = dec * delta + var rev_d = rev * delta var turn_d = turn_speed * delta var brake_d = brake_strength * delta var turn_factor = 1 @@ -42,6 +48,11 @@ func _process(delta): if Input.is_action_pressed("accelerate"): momentum += acc_d * Vector2(1,0).rotated(rotation) +# if Input.is_action_pressed("reverse"): +# print_debug("reverse vector: ", rev_d * Vector2(-1,0).rotated(rotation)) +# momentum += rev_d * Vector2(-1,0).rotated(rotation) +# print_debug("momentum vector = ", momentum) + if Input.is_action_pressed("brake"): turn_factor = drift_factor if momentum.length() > brake_d: @@ -53,8 +64,11 @@ func _process(delta): rotate(turn_d*turn_factor*-1) if Input.is_action_pressed("right"): rotate(turn_d*turn_factor) + + if !Input.is_action_pressed("brake"): + momentum = Vector2(1,0).rotated(rotation) * momentum.length() - camera.position = Vector2(momentum.length() * camera_offset_factor, 0) + camera.position = Vector2((momentum.length() / max_speed) * camera_offset, 0) camera.rotation_degrees = momentum.angle() + 90 if momentum.length() > max_speed: