diff --git a/assets/spracer.png b/assets/spracer.png new file mode 100644 index 0000000..85f14b1 Binary files /dev/null and b/assets/spracer.png differ diff --git a/assets/spracer.png.import b/assets/spracer.png.import new file mode 100644 index 0000000..0c33b06 --- /dev/null +++ b/assets/spracer.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://d1t1g7hyil82v" +path="res://.godot/imported/spracer.png-0b29359c32dc2c4ac7964da0766ceff0.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/spracer.png" +dest_files=["res://.godot/imported/spracer.png-0b29359c32dc2c4ac7964da0766ceff0.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/project.godot b/project.godot index fe7f1d1..1d856f0 100644 --- a/project.godot +++ b/project.godot @@ -26,4 +26,32 @@ version_control/autoload_on_startup=true [editor_plugins] -enabled=PackedStringArray("res://addons/godot-git-plugin/plugin.cfg") +enabled=PackedStringArray() + +[input] + +accelerate={ +"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":87,"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":4194320,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +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) +] +} +left={ +"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":65,"key_label":0,"unicode":97,"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":4194319,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +right={ +"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":4194321,"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":68,"key_label":0,"unicode":100,"echo":false,"script":null) +] +} diff --git a/src/game/level1.tscn b/src/game/level1.tscn index 5a099ab..3e96935 100644 --- a/src/game/level1.tscn +++ b/src/game/level1.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=3 format=3 uid="uid://b4dnui6vvlta7"] +[gd_scene load_steps=4 format=3 uid="uid://c77xk0mywwt5f"] [ext_resource type="Script" path="res://src/game/level.gd" id="1_hv75i"] +[ext_resource type="PackedScene" uid="uid://ckau5s2tsb3oc" path="res://src/game/player.tscn" id="2_5nf51"] [sub_resource type="Curve2D" id="Curve2D_ll8bj"] _data = { @@ -18,3 +19,6 @@ line = NodePath("TrackLine") curve = SubResource("Curve2D_ll8bj") [node name="TrackLine" type="Line2D" parent="."] + +[node name="player" parent="." instance=ExtResource("2_5nf51")] +position = Vector2(42.7545, -14.9219) diff --git a/src/game/player.tscn b/src/game/player.tscn new file mode 100644 index 0000000..5df65c9 --- /dev/null +++ b/src/game/player.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=4 format=3 uid="uid://ckau5s2tsb3oc"] + +[ext_resource type="Script" path="res://src/game/player_controller.gd" id="1_g4ij8"] +[ext_resource type="Texture2D" uid="uid://d1t1g7hyil82v" path="res://assets/spracer.png" id="2_xal47"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_w2nj7"] +size = Vector2(36, 20) + +[node name="player" type="StaticBody2D" node_paths=PackedStringArray("camera")] +script = ExtResource("1_g4ij8") +dec = 15.0 +max_speed = 50.0 +turn_speed = 2.0 +brake_strength = 12.0 +camera = NodePath("Camera2D") + +[node name="Sprite2D" type="Sprite2D" parent="."] +position = Vector2(0, 3.09944e-05) +rotation = 1.5708 +scale = Vector2(0.0489796, 0.0489796) +texture = ExtResource("2_xal47") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(-1, 0) +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 new file mode 100644 index 0000000..c43875c --- /dev/null +++ b/src/game/player_controller.gd @@ -0,0 +1,62 @@ +extends StaticBody2D +# constants +## acceleration of the car while holding the accelerate button +@export var acc := 20. +## natural deceleration of the car +@export var dec := 4. +## maximum speed of the car +@export var max_speed := 100. +## 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 = 20. + +# references +@export var camera : Camera2D + +# variables +var momentum: Vector2 + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + var acc_d = acc * delta + var dec_d = dec * delta + var turn_d = turn_speed * delta + var brake_d = brake_strength * delta + var turn_factor = 1 + + if momentum.length() > dec_d: + momentum -= momentum.normalized()*dec_d + else: + momentum = Vector2(0,0) + + if Input.is_action_pressed("accelerate"): + momentum += acc_d * Vector2(1,0).rotated(rotation) + + if Input.is_action_pressed("brake"): + turn_factor = drift_factor + if momentum.length() > brake_d: + momentum -= momentum.normalized() * brake_d + else: + momentum = Vector2(0,0) + + if Input.is_action_pressed("left"): + rotate(turn_d*turn_factor*-1) + if Input.is_action_pressed("right"): + rotate(turn_d*turn_factor) + + camera.position = Vector2(momentum.length() * camera_offset_factor, 0) + camera.rotation_degrees = momentum.angle() + 90 + + if momentum.length() > max_speed: + momentum = momentum.normalized() * max_speed + move_and_collide(momentum)