diff options
Diffstat (limited to '')
-rw-r--r-- | Scripts/Checkpoint.gd | 13 | ||||
-rw-r--r-- | Scripts/UI_Control.gd | 20 | ||||
-rw-r--r-- | Scripts/car_behaviour.gd | 48 | ||||
-rw-r--r-- | Scripts/drawing.gd | 4 | ||||
-rw-r--r-- | Scripts/game.gd | 13 | ||||
-rw-r--r-- | Scripts/scene_control.gd | 22 |
6 files changed, 90 insertions, 30 deletions
diff --git a/Scripts/Checkpoint.gd b/Scripts/Checkpoint.gd index d8c671e..119419a 100644 --- a/Scripts/Checkpoint.gd +++ b/Scripts/Checkpoint.gd @@ -3,17 +3,12 @@ extends Node2D var was_visited = false -@onready var sprite : Sprite2D = $Sprite2D - func set_was_visited(visited: bool): was_visited = visited - if(visited): - hide() - else: - show() - # TODO change the sprite to indicate the change - + $Sprite2Dclosed.visible = visited + $Sprite2Dopen.visible = !visited + + func _on_area_2d_body_entered(body): - hide() set_was_visited(true) diff --git a/Scripts/UI_Control.gd b/Scripts/UI_Control.gd index b7ce2a5..a45503a 100644 --- a/Scripts/UI_Control.gd +++ b/Scripts/UI_Control.gd @@ -5,6 +5,10 @@ extends Control @onready var time_label = $InGameUI/CurrentTime +@onready var gold_sprite: AnimatedSprite2D = $PostGameUI/Gold +@onready var silver_sprite: AnimatedSprite2D = $PostGameUI/Silver +@onready var bronze_sprite: AnimatedSprite2D = $PostGameUI/Bronze + func update_timer_label(new_time: float): if(new_time < 0): @@ -12,14 +16,28 @@ func update_timer_label(new_time: float): return time_label.text = str(new_time).pad_decimals(3) -func switch_to_post_game_UI(): +func switch_to_post_game_UI(medal: int): in_game_ui.visible = false post_game_ui.visible = true $PostGameUI/FinalTime.text = time_label.text + + if medal == 0: + gold_sprite.visible = true + gold_sprite.play("default") + elif medal == 1: + silver_sprite.visible = true + silver_sprite.play("default") + elif medal == 2: + bronze_sprite.visible = true + bronze_sprite.play("default") func switch_to_in_game_UI(): in_game_ui.visible = true post_game_ui.visible = false + + gold_sprite.visible = false + silver_sprite.visible = false + bronze_sprite.visible = false func _on_start_button_pressed(): var root = owner.owner diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd index 2ef9fb9..1a69b37 100644 --- a/Scripts/car_behaviour.gd +++ b/Scripts/car_behaviour.gd @@ -1,9 +1,10 @@ extends CharacterBody2D # editor variables -@export var max_speed = 300 -@export var steer_force = 0.1 -@export var acceleration = 0.0 +@export var max_speed = 800 +@export var steer_force = 20 +@export var acceleration = 8 +@export var mass = 1.0 @export var look_ahead = 75 @export var num_rays = 32 @export var driving = false @@ -28,15 +29,19 @@ func _ready(): func _physics_process(delta): if not driving: + $Sprite2D.hide() return + + $Sprite2D.show() # Code for getting the next desired direction based on the environment set_interest() set_danger() choose_direction() - # example_movement(delta) - own_movement(delta) +# example_movement(delta) +# own_movement(delta) + own_movement_improved(delta) func set_interest(): @@ -91,13 +96,38 @@ func example_movement(delta): rotation = velocity.angle() move_and_collide(velocity * delta) + func own_movement(delta): var desired_velocity = chosen_dir.rotated(rotation) * max_speed + + var new_speed = lerpf(velocity.length(), max_speed, acceleration) + var new_angle = lerp_angle(velocity.angle(), desired_velocity.angle(), steer_force) + + velocity = Vector2.from_angle(new_angle) * new_speed + rotation = new_angle + move_and_collide(velocity * delta) + + +func own_movement_improved(delta): + var desired_velocity = chosen_dir.rotated(rotation) * max_speed + print('==========') + print('current speed: ', velocity.length(), ' | current angle: ', velocity.angle()) + print('desired speed: ', desired_velocity.length(), ' | desired angle: ', desired_velocity.angle()) - var actual_speed = lerpf(velocity.length(), max_speed, acceleration) - var actual_angle = lerp_angle(velocity.angle(), desired_velocity.angle(), steer_force) +# var possible_radius = mass * velocity.length_squared() / steer_force +# var possible_steer_angle = velocity.length() / possible_radius + var possible_steer_angle = steer_force / (mass * velocity.length()) + var angle_change = clampf(wrapf(desired_velocity.angle() - velocity.angle(), -PI, PI), -possible_steer_angle, possible_steer_angle) + var new_angle = wrapf(velocity.angle() + angle_change, -PI, PI) + print('possible_steer_angle: ', possible_steer_angle, ' | angle_change: ', angle_change, ' | new_angle: ', new_angle ) - velocity = Vector2.from_angle(actual_angle) * actual_speed - rotation = actual_angle + var wanted_speed = steer_force / (mass * absf(desired_velocity.angle() - velocity.angle())) + var new_max_speed = min(velocity.length() + (acceleration), max_speed) + var new_min_speed = max(velocity.length() - (acceleration), 0) + var new_speed = clampf(wanted_speed, new_min_speed, new_max_speed) + print('wanted: ', wanted_speed, ' | min: ', new_min_speed, ' | max: ', new_max_speed, ' | new: ', new_speed) + velocity = Vector2.from_angle(new_angle) * new_speed + rotation = new_angle move_and_collide(velocity * delta) + diff --git a/Scripts/drawing.gd b/Scripts/drawing.gd index a8068a2..6b94f6e 100644 --- a/Scripts/drawing.gd +++ b/Scripts/drawing.gd @@ -26,6 +26,10 @@ func _end_drawing(): func _input(event): if drawing and event is InputEventMouseMotion: line.add_point(event.position) + path.curve.add_point(event.position) + if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT: + drawing = false + reset() func reset(): diff --git a/Scripts/game.gd b/Scripts/game.gd index 72a01e3..c9beed2 100644 --- a/Scripts/game.gd +++ b/Scripts/game.gd @@ -1,17 +1,16 @@ extends Node2D -const NUM_LEVELS = 2 - # level control var current_level_id = 0 var levels = [] var post_game = false var scenes = [ -# preload("res://Nodes/levels/level1/level1.tscn"), -# preload("res://Nodes/levels/level2/level2.tscn") - preload("res://Nodes/Level_test.tscn"), - preload("res://Nodes/Level_test_2.tscn") + preload("res://Levels/Tut01.tscn"), + preload("res://Levels/Tut02.tscn"), + preload("res://Levels/Tut03.tscn"), + preload("res://Levels/Level1.tscn"), + preload("res://Levels/Level2.tscn") ] # Called when the node enters the scene tree for the first time. @@ -53,7 +52,7 @@ func end_level(): func next_level(): current_level_id += 1 - if current_level_id >= NUM_LEVELS: + if current_level_id >= scenes.size(): get_tree().change_scene_to_file("res://Nodes/main_menu.tscn") return diff --git a/Scripts/scene_control.gd b/Scripts/scene_control.gd index ce3332f..453853c 100644 --- a/Scripts/scene_control.gd +++ b/Scripts/scene_control.gd @@ -2,18 +2,22 @@ extends Node class_name SceneControl +@export var gold_medal_time = 5.0 +@export var silver_medal_time = 7.0 +@export var bronze_medal_time = 10 + @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 drawing = $DrawNode +@onready var start = $Start var is_driving = false var current_time = 0.0 var game_control -var start func _ready(): @@ -23,8 +27,8 @@ func _ready(): $UI/Control/PostGameUI/RestartButton.pressed.connect(reset_level) $UI/Control/PostGameUI/NextLevelButton.pressed.connect(next_level) - start = $Start as TextureButton - start.pressed.connect(start_drawing) + var start_button = $Start/Start as TextureButton + start_button.button_down.connect(start_drawing) reset_level() @@ -60,6 +64,7 @@ func reset_level(): car.global_position = start.global_position car.global_rotation = start.rotation; + car.velocity = Vector2.ZERO current_time = 0.0 @@ -75,10 +80,19 @@ func evaluate_driving(): return false return true +func evaluate_time(): + if current_time < gold_medal_time: + return 0 + if current_time < silver_medal_time: + return 1 + if current_time < bronze_medal_time: + return 2 + return 3 + func _on_finish_line_body_entered(node: Node2D): if evaluate_driving(): - ui.switch_to_post_game_UI() + ui.switch_to_post_game_UI(evaluate_time()) set_driving(false) return |