diff options
author | IcECreAm777 <hgk.1998@googlemail.com> | 2023-07-08 16:43:32 +0200 |
---|---|---|
committer | IcECreAm777 <hgk.1998@googlemail.com> | 2023-07-08 16:43:32 +0200 |
commit | 78b6db9f792e18d848383856693cff4ef223a10f (patch) | |
tree | 4d0385ae8eb8f450a6b2c40c1886a964134d154d | |
parent | bf17c8a97b77cdb783a2751e7ee4a90843a00f06 (diff) | |
download | 2023-78b6db9f792e18d848383856693cff4ef223a10f.tar.gz 2023-78b6db9f792e18d848383856693cff4ef223a10f.tar.bz2 2023-78b6db9f792e18d848383856693cff4ef223a10f.zip |
checkpoint detection and UI
-rw-r--r-- | Nodes/car.tscn | 12 | ||||
-rw-r--r-- | Nodes/checkpoint.tscn | 22 | ||||
-rw-r--r-- | Nodes/drawing_and_driving.tscn | 27 | ||||
-rw-r--r-- | Nodes/ui.tscn | 92 | ||||
-rw-r--r-- | Scripts/Checkpoint.gd | 14 | ||||
-rw-r--r-- | Scripts/UI_Control.gd | 27 | ||||
-rw-r--r-- | Scripts/car_behaviour.gd | 9 | ||||
-rw-r--r-- | Scripts/scene_control.gd | 52 |
8 files changed, 239 insertions, 16 deletions
diff --git a/Nodes/car.tscn b/Nodes/car.tscn index 25505a0..dbfb4e1 100644 --- a/Nodes/car.tscn +++ b/Nodes/car.tscn @@ -2,16 +2,16 @@ [ext_resource type="Script" path="res://Scripts/car_behaviour.gd" id="1_0e4ux"] -[sub_resource type="RectangleShape2D" id="RectangleShape2D_wvlyu"] - [sub_resource type="CanvasTexture" id="CanvasTexture_aacgf"] -[node name="Car" type="StaticBody2D"] -script = ExtResource("1_0e4ux") +[sub_resource type="RectangleShape2D" id="RectangleShape2D_wvlyu"] -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource("RectangleShape2D_wvlyu") +[node name="Car" type="CharacterBody2D"] +script = ExtResource("1_0e4ux") [node name="Sprite2D" type="Sprite2D" parent="."] scale = Vector2(19.92, 19.76) texture = SubResource("CanvasTexture_aacgf") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_wvlyu") diff --git a/Nodes/checkpoint.tscn b/Nodes/checkpoint.tscn new file mode 100644 index 0000000..088da4b --- /dev/null +++ b/Nodes/checkpoint.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=4 format=3 uid="uid://bi1dw58p4tky3"] + +[ext_resource type="Script" path="res://Scripts/Checkpoint.gd" id="1_fwyov"] + +[sub_resource type="CanvasTexture" id="CanvasTexture_lawdg"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_f8epl"] + +[node name="Checkpoint" type="Node2D"] +position = Vector2(225, 128) +script = ExtResource("1_fwyov") + +[node name="Sprite2D" type="Sprite2D" parent="."] +scale = Vector2(19.32, 19.8) +texture = SubResource("CanvasTexture_lawdg") + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("RectangleShape2D_f8epl") + +[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"] diff --git a/Nodes/drawing_and_driving.tscn b/Nodes/drawing_and_driving.tscn index 865fd4e..70cf332 100644 --- a/Nodes/drawing_and_driving.tscn +++ b/Nodes/drawing_and_driving.tscn @@ -1,12 +1,18 @@ -[gd_scene load_steps=6 format=3 uid="uid://cb06k14t2slax"] +[gd_scene load_steps=10 format=3 uid="uid://cb06k14t2slax"] [ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_6bj13"] [ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="1_ktvsb"] [ext_resource type="Script" path="res://Scripts/drawing.gd" id="3_el555"] [ext_resource type="Texture2D" uid="uid://dg3v488vx0oj6" path="res://Assets/checkpoint.png" id="4_ar5ae"] +[ext_resource type="PackedScene" uid="uid://bi1dw58p4tky3" path="res://Nodes/checkpoint.tscn" id="5_lbroe"] +[ext_resource type="PackedScene" uid="uid://bunwe03ob38mr" path="res://Nodes/ui.tscn" id="6_y2lbh"] [sub_resource type="Curve2D" id="Curve2D_wmfhm"] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_uuifd"] + +[sub_resource type="CanvasTexture" id="CanvasTexture_agpfu"] + [node name="drawing_and_driving" type="Node2D"] script = ExtResource("1_6bj13") @@ -27,3 +33,22 @@ curve = SubResource("Curve2D_wmfhm") [node name="Sprite2D" type="Sprite2D" parent="DrawNode/TrackPath/TrackFollower"] scale = Vector2(0.604, 0.604) texture = ExtResource("4_ar5ae") + +[node name="Checkpoints" type="Node" parent="."] + +[node name="Checkpoint" parent="Checkpoints" instance=ExtResource("5_lbroe")] + +[node name="Checkpoint2" parent="Checkpoints" instance=ExtResource("5_lbroe")] +position = Vector2(349, 407) + +[node name="UI" parent="." instance=ExtResource("6_y2lbh")] + +[node name="FinishLine" type="Area2D" parent="."] +position = Vector2(420, 222) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="FinishLine"] +shape = SubResource("RectangleShape2D_uuifd") + +[node name="Sprite2D" type="Sprite2D" parent="FinishLine"] +scale = Vector2(19.96, 19.76) +texture = SubResource("CanvasTexture_agpfu") diff --git a/Nodes/ui.tscn b/Nodes/ui.tscn new file mode 100644 index 0000000..8467074 --- /dev/null +++ b/Nodes/ui.tscn @@ -0,0 +1,92 @@ +[gd_scene load_steps=4 format=3 uid="uid://bunwe03ob38mr"] + +[ext_resource type="Script" path="res://Scripts/UI_Control.gd" id="1_77hb8"] + +[sub_resource type="LabelSettings" id="LabelSettings_p2s2c"] +font_size = 64 + +[sub_resource type="LabelSettings" id="LabelSettings_hw7sw"] +line_spacing = 32.0 + +[node name="UI" type="CanvasLayer"] +follow_viewport_enabled = true + +[node name="Control" type="Control" parent="."] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource("1_77hb8") + +[node name="InGameUI" type="Control" parent="Control"] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="CurrentTime" type="Label" parent="Control/InGameUI"] +layout_mode = 0 +offset_left = 64.0 +offset_top = 17.0 +offset_right = 119.0 +offset_bottom = 43.0 +text = "0:00:00" + +[node name="CurrentTimeLabel" type="Label" parent="Control/InGameUI"] +layout_mode = 0 +offset_left = 14.0 +offset_top = 16.0 +offset_right = 69.0 +offset_bottom = 42.0 +text = "Time: " + +[node name="PostGameUI" type="Control" parent="Control"] +visible = false +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="Label" type="Label" parent="Control/PostGameUI"] +layout_mode = 1 +anchors_preset = -1 +anchor_left = 0.3 +anchor_top = 0.1 +anchor_right = 0.7 +anchor_bottom = 0.1 +offset_right = 40.0 +offset_bottom = 23.0 +scale = Vector2(1, 1.02025) +text = "LEVEL FINISHED" +label_settings = SubResource("LabelSettings_p2s2c") + +[node name="Label2" type="Label" parent="Control/PostGameUI"] +layout_mode = 1 +anchors_preset = -1 +anchor_left = 0.3 +anchor_top = 0.3 +anchor_right = 0.5 +anchor_bottom = 0.3 +offset_right = 10.0 +offset_bottom = 23.0 +scale = Vector2(1, 1.02025) +text = "Final Time:" +label_settings = SubResource("LabelSettings_hw7sw") +horizontal_alignment = 2 + +[node name="FinalTime" type="Label" parent="Control/PostGameUI"] +layout_mode = 1 +anchors_preset = -1 +anchor_left = 0.5 +anchor_top = 0.3 +anchor_right = 0.7 +anchor_bottom = 0.3 +offset_left = 10.0 +offset_bottom = 23.0 +scale = Vector2(1, 1.02025) +text = "0.00" +label_settings = SubResource("LabelSettings_hw7sw") diff --git a/Scripts/Checkpoint.gd b/Scripts/Checkpoint.gd new file mode 100644 index 0000000..ed10f74 --- /dev/null +++ b/Scripts/Checkpoint.gd @@ -0,0 +1,14 @@ +extends Node2D + + +var was_visited = false + +@onready var sprite : Sprite2D = $Sprite2D + + +func set_was_visited(visited: bool): + was_visited = visited + # TODO change the sprite to indicate the change + +func _on_area_2d_body_entered(body): + set_was_visited(true) diff --git a/Scripts/UI_Control.gd b/Scripts/UI_Control.gd new file mode 100644 index 0000000..b7ce2a5 --- /dev/null +++ b/Scripts/UI_Control.gd @@ -0,0 +1,27 @@ +extends Control + +@onready var in_game_ui = $InGameUI +@onready var post_game_ui = $PostGameUI + +@onready var time_label = $InGameUI/CurrentTime + + +func update_timer_label(new_time: float): + if(new_time < 0): + time_label.text = "Waiting for player to start driving..." + return + time_label.text = str(new_time).pad_decimals(3) + +func switch_to_post_game_UI(): + in_game_ui.visible = false + post_game_ui.visible = true + $PostGameUI/FinalTime.text = time_label.text + +func switch_to_in_game_UI(): + in_game_ui.visible = true + post_game_ui.visible = false + +func _on_start_button_pressed(): + var root = owner.owner + if root and root.has_method("start_driving"): + root.start_driving() diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd index 0463dc1..d5fc292 100644 --- a/Scripts/car_behaviour.gd +++ b/Scripts/car_behaviour.gd @@ -1,4 +1,4 @@ -extends StaticBody2D +extends CharacterBody2D # editor variables @export var max_speed = 300 @@ -14,7 +14,6 @@ var interest = [] var danger = [] var chosen_dir = Vector2.ZERO -var velocity = Vector2.ZERO var current_speed = 0 @@ -26,7 +25,6 @@ func _ready(): for i in num_rays: var angle = i * 2 * PI / num_rays ray_directions[i] = Vector2.RIGHT.rotated(angle) - print(ray_directions) func _physics_process(delta): if not driving: @@ -97,9 +95,8 @@ func own_movement(delta): var desired_velocity = chosen_dir.rotated(rotation) * max_speed var actual_speed = lerpf(velocity.length(), max_speed, acceleration) - var actual_angle = lerp_angle(velocity.angle(), desired_velocity.angle(), steer_force) - - print("lerped speed: ", actual_speed, " | lerped angle: ", actual_angle) + var actual_angle = lerp_angle(velocity.angle(), desired_velocity.angle(), + steer_force) velocity = Vector2.from_angle(actual_angle) * actual_speed rotation = actual_angle diff --git a/Scripts/scene_control.gd b/Scripts/scene_control.gd index fdaad14..0b55d0f 100644 --- a/Scripts/scene_control.gd +++ b/Scripts/scene_control.gd @@ -2,9 +2,28 @@ extends Node class_name SceneControl -@onready var path : Path2D = $DrawNode/TrackPath -@onready var path_follow : PathFollow2D = $DrawNode/TrackPath/TrackFollower -@onready var car : StaticBody2D = $Car +@onready var path: Path2D = $DrawNode/TrackPath +@onready var path_follow: PathFollow2D = $DrawNode/TrackPath/TrackFollower +@onready var car = $Car +@onready var checkpoints = $Checkpoints.get_children() +@onready var ui = $UI/Control +@onready var finish_line: Area2D + +var is_driving = false +var current_time = 0.0 + + +func _ready(): + finish_line = $FinishLine as Area2D + finish_line.body_entered.connect(_on_finish_line_body_entered) + reset_level() + +func _physics_process(delta): + if car.driving: + current_time += delta + print(current_time) + ui.update_timer_label(current_time) + func get_path_direction(pos): var offset = path.curve.get_closest_offset(pos) @@ -18,3 +37,30 @@ func get_path_next_position(pos): func set_driving(driving: bool): car.driving = driving + is_driving = driving + +func reset_level(): + ui.update_timer_label(-1) + for cp in checkpoints: + cp.set_was_visited(false) + +func start_driving(): + current_time = 0.0 + set_driving(true) + +func evaluate_driving(): + for cp in checkpoints: + if not cp.was_visited: + return false + return true + + +func _on_finish_line_body_entered(node: Node2D): + if evaluate_driving(): + ui.switch_to_post_game_UI() + + set_driving(false) + reset_level() + # TODO get this from the start position + car.global_position = Vector2.ZERO + car.global_rotation = 0; |