spacevroom

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit befbf8bdbfe4ce523ee78f6ae6388981e7e101a2
parent 51db967b5a17e69d6db8490c008700d116295f76
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Tue, 21 Sep 2021 15:45:03 +0300

Add a map loader and move constants to a singleton.

Diffstat:
MCamera.gd | 4+++-
AConstants.gd | 11+++++++++++
MHUD.gd | 5+++--
MLevel1.gd | 4++++
MLevel1.tscn | 1+
MLevelLoader.gd | 32++++++++++++++++++++++----------
MPlayer.gd | 32++++++++++----------------------
Mlevel1.txt | 14+++++++-------
Mproject.godot | 1+
9 files changed, 62 insertions(+), 42 deletions(-)

diff --git a/Camera.gd b/Camera.gd @@ -10,4 +10,6 @@ func _ready() -> void: func _process(delta: float) -> void: - translation = player.translation + offset + translation.x = player.translation.x + offset.x +# translation.y = player.translation.y + offset.y + translation.z = player.translation.z + offset.z diff --git a/Constants.gd b/Constants.gd @@ -0,0 +1,11 @@ +extends Node + + +const road_box_dimensions: Vector3 = Vector3(0.35, 0.5, 0.95) + + +const fall_acceleration: float = 0.5 +const jump_impulse: float = 7.0 +const maximum_forward_speed: float = 20.0 +const forward_speed_increment: float = 0.5 +const side_speed: float = 1.0 diff --git a/HUD.gd b/HUD.gd @@ -4,8 +4,9 @@ extends Control onready var speed_label: Label = $SpeedHBoxContainer/SpeedLabel -func _on_Player_speed_changed(new_speed: int) -> void: - var number_of_speed_bars: int = new_speed / 10 +func _on_Player_speed_changed(new_speed: float) -> void: + print_debug(new_speed) + var number_of_speed_bars: int = new_speed / Constants.forward_speed_increment speed_label.text = "=".repeat(number_of_speed_bars) print_debug(speed_label.text) diff --git a/Level1.gd b/Level1.gd @@ -24,3 +24,7 @@ func _input(event: InputEvent) -> void: func _on_Player_off_the_deep_end() -> void: get_tree().reload_current_scene() + + +func _on_Player_reached_goal() -> void: + get_tree().change_scene("res://LevelSelection.tscn") diff --git a/Level1.tscn b/Level1.tscn @@ -19,4 +19,5 @@ script = ExtResource( 1 ) [node name="Boxes" type="Spatial" parent="."] [connection signal="off_the_deep_end" from="Player" to="." method="_on_Player_off_the_deep_end"] [connection signal="reached_goal" from="Player" to="HUD" method="_on_Player_reached_goal"] +[connection signal="reached_goal" from="Player" to="." method="_on_Player_reached_goal"] [connection signal="speed_changed" from="Player" to="HUD" method="_on_Player_speed_changed"] diff --git a/LevelLoader.gd b/LevelLoader.gd @@ -2,29 +2,41 @@ extends Node func load_level(level_text: String) -> Spatial: - print_debug(level_text) var boxes: Spatial = Spatial.new() var line_index: int = 0 for line in level_text.split("\n"): var column_index: int = 0 for c in line: if c == '#': - print_debug(c) var box: CSGBox = CSGBox.new() box.use_collision = true - box.width = 0.4 - box.height = 0.5 - box.depth = 2 - box.translation = Vector3(0.4 * (line_index - 3), 0, -2 * column_index) + box.width = Constants.road_box_dimensions.x + box.height = Constants.road_box_dimensions.y + box.depth = Constants.road_box_dimensions.z + box.translation = Vector3( + Constants.road_box_dimensions.x * (line_index - 3), + 0, + -Constants.road_box_dimensions.z * column_index + ) +# box.translation = Vector3( +# (line_index - 3), +# 0, +# column_index +# ) * (Constants.road_box_dimensions) box.add_to_group("platforms") boxes.add_child(box) if c == 'z': var box: CSGBox = CSGBox.new() box.use_collision = true - box.width = 0.4 - box.height = 0.5 - box.depth = 1 - box.translation = Vector3(0.4 * (line_index - 3), 0, -2 * column_index) + box.width = Constants.road_box_dimensions.x + box.height = Constants.road_box_dimensions.y + box.depth = Constants.road_box_dimensions.z + box.translation = Vector3( + Constants.road_box_dimensions.x * (line_index - 3), + 0, + -Constants.road_box_dimensions.z * column_index + ) + box.material box.add_to_group("goal") boxes.add_child(box) column_index += 1 diff --git a/Player.gd b/Player.gd @@ -1,15 +1,8 @@ extends KinematicBody -const fall_acceleration: float = 2.0 -const jump_impulse: float = 1.0 -const maximum_forward_speed: int = 500 -const forward_speed_increment: int = 50 -const side_speed: float = 50.0 - - var velocity: Vector3 = Vector3.ZERO -var driving_speed: int = 0 +var driving_speed: float = 0 var direction: int = 0 var last_pressed_direction: String = "" @@ -23,14 +16,9 @@ onready var forward_speed_change_timer: Timer = $ForwardSpeedChangeTimer func _ready() -> void: - connect("off_the_deep_end", self, "on_off_the_deep_end") print_debug(forward_speed_change_timer.time_left) -func on_off_the_deep_end() -> void: - print_debug("off the deep end") - - func _physics_process(delta: float) -> void: var collision = move_and_collide(velocity, true, true, true) if collision: @@ -76,25 +64,25 @@ func _physics_process(delta: float) -> void: if Input.is_action_pressed("ui_up") and forward_speed_change_timer.time_left == 0.0: print_debug(forward_speed_change_timer.time_left) forward_speed_change_timer.start() - driving_speed += forward_speed_increment - driving_speed = clamp(driving_speed, 0, maximum_forward_speed) + driving_speed += Constants.forward_speed_increment + driving_speed = clamp(driving_speed, 0, Constants.maximum_forward_speed) emit_signal("speed_changed", driving_speed) if Input.is_action_pressed("ui_down") and forward_speed_change_timer.time_left == 0.0: print_debug(forward_speed_change_timer.time_left) forward_speed_change_timer.start() - driving_speed -= forward_speed_increment - driving_speed = clamp(driving_speed, 0, maximum_forward_speed) + driving_speed -= Constants.forward_speed_increment + driving_speed = clamp(driving_speed, 0, Constants.maximum_forward_speed) emit_signal("speed_changed", driving_speed) - velocity.x = direction * side_speed * delta - velocity.z = -driving_speed * delta + velocity.x = direction * Constants.side_speed + velocity.z = -driving_speed if is_on_floor() and Input.is_action_just_pressed("jump"): - velocity.y += jump_impulse - velocity.y -= fall_acceleration * delta + velocity.y += Constants.jump_impulse + velocity.y -= Constants.fall_acceleration velocity = move_and_slide(velocity, Vector3.UP) - if translation.y < 0: + if translation.y < -5: emit_signal("off_the_deep_end") diff --git a/level1.txt b/level1.txt @@ -1,7 +1,7 @@ - ###### - ###### ###### - ###### ###### ###### #### #### zzzz -###### ###### ###### ###### #### #### zzzz - ###### ###### ###### #### #### zzzz - ###### ###### - ###### +###################### ###### +################################################# ###### ###### +############################################################################# ###### ###### ###### zzzz #### #### +################################################################################################################ ###### ###### ###### zzzz #### #### +############################################################################# ###### ###### ###### zzzz #### #### +################################################# ###### ###### +###################### ###### diff --git a/project.godot b/project.godot @@ -22,6 +22,7 @@ config/icon="res://icon.png" [autoload] LevelLoader="*res://LevelLoader.gd" +Constants="*res://Constants.gd" [input]