spacevroom

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

commit 4e8cada39f5e3d3f5505c7c36fe26c64578486a0
parent b27ab587205881f800135d167279270f488e4d17
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Mon, 20 Sep 2021 21:20:09 +0300

Dynamically loaded level.

Diffstat:
MCamera.gd | 2+-
MHUD.gd | 3+++
MLevel1.gd | 26++++++++++++++++----------
MLevel1.tscn | 114++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
ALevelLoader.gd | 32++++++++++++++++++++++++++++++++
ALevelSelection.gd | 23+++++++++++++++++++++++
ALevelSelection.tscn | 30++++++++++++++++++++++++++++++
MPlayer.gd | 32++++++++++++++++++++++++++++++--
APlayer.tscn | 26++++++++++++++++++++++++++
Alevel1.txt | 7+++++++
Mproject.godot | 11++++++++++-
11 files changed, 242 insertions(+), 64 deletions(-)

diff --git a/Camera.gd b/Camera.gd @@ -9,5 +9,5 @@ func _ready() -> void: offset = translation - player.translation -func _physics_process(delta: float) -> void: +func _process(delta: float) -> void: translation = player.translation + offset diff --git a/HUD.gd b/HUD.gd @@ -9,3 +9,6 @@ func _on_Player_speed_changed(new_speed: int) -> void: speed_label.text = "=".repeat(number_of_speed_bars) print_debug(speed_label.text) + +func _on_Player_reached_goal() -> void: + pass diff --git a/Level1.gd b/Level1.gd @@ -1,19 +1,25 @@ extends Spatial -# Declare member variables here. Examples: -# var a: int = 2 -# var b: String = "text" +onready var boxes := $Boxes -# Called when the node enters the scene tree for the first time. func _ready() -> void: - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta: float) -> void: -# pass + var level_text_file: File = File.new() + var file_open_error := level_text_file.open("res://level1.txt", File.READ) + if file_open_error: + push_error("Level open error: %s" % file_open_error) + var level_text := level_text_file.get_as_text() + level_text_file.close() + var loaded_boxes := LevelLoader.load_level(level_text) + boxes.add_child(loaded_boxes) + + +func _input(event: InputEvent) -> void: + if event.is_action("quit_level"): + get_tree().change_scene("res://LevelSelection.tscn") + if event.is_action("reset_level"): + get_tree().reload_current_scene() func _on_Player_off_the_deep_end() -> void: diff --git a/Level1.tscn b/Level1.tscn @@ -1,16 +1,10 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://Camera.gd" type="Script" id=1] -[ext_resource path="res://Player.gd" type="Script" id=2] -[ext_resource path="res://HUD.gd" type="Script" id=3] +[ext_resource path="res://Player.tscn" type="PackedScene" id=2] +[ext_resource path="res://HUD.tscn" type="PackedScene" id=3] [ext_resource path="res://Level1.gd" type="Script" id=4] -[sub_resource type="SpatialMaterial" id=1] -albedo_color = Color( 0.482353, 0.980392, 0.572549, 1 ) - -[sub_resource type="BoxShape" id=2] -extents = Vector3( 0.125, 0.075, 0.15 ) - [node name="Level1" type="Spatial"] script = ExtResource( 4 ) @@ -18,56 +12,76 @@ script = ExtResource( 4 ) transform = Transform( 1, 0, 0, 0, 0.964893, 0.262644, 0, -0.262644, 0.964893, 0, 0.937895, 0.953137 ) script = ExtResource( 1 ) -[node name="CSGBox" type="CSGBox" parent="."] +[node name="CSGBox" type="CSGBox" parent="." groups=[ +"platforms", +]] +use_collision = true +width = 0.4 +height = 0.5 + +[node name="CSGBox2" type="CSGBox" parent="." groups=[ +"platforms", +]] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.4, 0, -1.50218 ) use_collision = true -width = 1.0 +width = 0.4 height = 0.5 -depth = 33.62 -[node name="Player" type="KinematicBody" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.336291, 0 ) -script = ExtResource( 2 ) +[node name="CSGBox8" type="CSGBox" parent="." groups=[ +"goal", +"platforms", +]] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.4, 0, -17.0286 ) +use_collision = true +width = 0.4 +height = 0.5 + +[node name="CSGBox3" type="CSGBox" parent="." groups=[ +"platforms", +]] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.8, 0, -3.21177 ) +use_collision = true +width = 0.4 +height = 0.5 -[node name="CSGBox2" type="CSGBox" parent="Player"] -width = 0.25 -height = 0.15 -depth = 0.3 -material = SubResource( 1 ) +[node name="CSGBox4" type="CSGBox" parent="." groups=[ +"platforms", +]] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.2, 0, -11.3159 ) +use_collision = true +width = 0.4 +height = 0.5 +depth = 16.938 -[node name="CollisionShape" type="CollisionShape" parent="Player"] -shape = SubResource( 2 ) +[node name="CSGBox5" type="CSGBox" parent="." groups=[ +"platforms", +]] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.4, 0, 0 ) +use_collision = true +width = 0.4 +height = 0.5 -[node name="ForwardSpeedChangeTimer" type="Timer" parent="Player"] -wait_time = 0.2 -one_shot = true +[node name="CSGBox6" type="CSGBox" parent="." groups=[ +"platforms", +]] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.8, 0, 0 ) +use_collision = true +width = 0.4 +height = 0.5 -[node name="HUD" type="Control" parent="."] -margin_right = 40.0 -margin_bottom = 40.0 -script = ExtResource( 3 ) -__meta__ = { -"_edit_use_anchors_": false -} +[node name="CSGBox7" type="CSGBox" parent="." groups=[ +"platforms", +]] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1.2, 0, 0 ) +use_collision = true +width = 0.4 +height = 0.5 -[node name="SpeedHBoxContainer" type="HBoxContainer" parent="HUD"] -margin_left = 447.357 -margin_top = 531.447 -margin_right = 522.357 -margin_bottom = 571.447 -__meta__ = { -"_edit_use_anchors_": false -} +[node name="Player" parent="." instance=ExtResource( 2 )] -[node name="Label" type="Label" parent="HUD/SpeedHBoxContainer"] -margin_top = 13.0 -margin_right = 43.0 -margin_bottom = 27.0 -text = "Speed:" +[node name="HUD" parent="." instance=ExtResource( 3 )] -[node name="SpeedLabel" type="Label" parent="HUD/SpeedHBoxContainer"] -margin_left = 47.0 -margin_top = 13.0 -margin_right = 47.0 -margin_bottom = 27.0 +[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="speed_changed" from="Player" to="HUD" method="_on_Player_speed_changed"] diff --git a/LevelLoader.gd b/LevelLoader.gd @@ -0,0 +1,32 @@ +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.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.add_to_group("goal") + boxes.add_child(box) + column_index += 1 + line_index += 1 + return boxes diff --git a/LevelSelection.gd b/LevelSelection.gd @@ -0,0 +1,23 @@ +extends Control + + +onready var item_list: ItemList = $VBoxContainer/ItemList + + +const item_text_to_level_path: Dictionary = { + "Vanilla Sky": "res://Level1.tscn" +} + + +var level_selected: String = "" + + +func _ready() -> void: + pass + + +func _on_ItemList_item_selected(index: int) -> void: + var item_text: String = item_list.get_item_text(index) + var level_path: String = item_text_to_level_path[item_text] + print_debug(index, " ", item_text, " ", level_path) + get_tree().change_scene(level_path) diff --git a/LevelSelection.tscn b/LevelSelection.tscn @@ -0,0 +1,30 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://LevelSelection.gd" type="Script" id=1] + +[node name="Control" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="VBoxContainer" type="VBoxContainer" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="ItemList" type="ItemList" parent="VBoxContainer"] +margin_right = 1024.0 +margin_bottom = 600.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 +items = [ "Vanilla Sky", null, false ] +[connection signal="item_selected" from="VBoxContainer/ItemList" to="." method="_on_ItemList_item_selected"] diff --git a/Player.gd b/Player.gd @@ -11,10 +11,12 @@ const side_speed: float = 50.0 var velocity: Vector3 = Vector3.ZERO var driving_speed: int = 0 var direction: int = 0 +var last_pressed_direction: String = "" signal speed_changed signal off_the_deep_end +signal reached_goal onready var forward_speed_change_timer: Timer = $ForwardSpeedChangeTimer @@ -31,20 +33,46 @@ func on_off_the_deep_end() -> void: func _physics_process(delta: float) -> void: + var collision = move_and_collide(velocity, true, true, true) + if collision: + var collider = collision.collider + if collider.is_in_group("goal"): + emit_signal("reached_goal") if is_on_floor(): - if Input.is_action_just_pressed("ui_right"): # and Input.is_action_pressed("ui_left"): + direction = 0 + if ( + (not Input.is_action_just_pressed("ui_right")) or + (not Input.is_action_just_pressed("ui_left")) + ): + if Input.is_action_pressed("ui_right") and Input.is_action_pressed("ui_left"): + if last_pressed_direction == "right": + direction = 1 + elif last_pressed_direction == "left": + direction = -1 + else: + if Input.is_action_pressed("ui_right"): + direction = 1 + if Input.is_action_pressed("ui_left"): + direction = -1 + if Input.is_action_just_pressed("ui_right"): + last_pressed_direction = "right" direction = 1 - if Input.is_action_just_pressed("ui_left"): # and Input.is_action_pressed("ui_right"): + if Input.is_action_just_pressed("ui_left"): + last_pressed_direction = "left" direction = -1 if Input.is_action_just_released("ui_right"): if Input.is_action_pressed("ui_left"): + last_pressed_direction = "left" direction = -1 else: + last_pressed_direction = "" direction = 0 if Input.is_action_just_released("ui_left"): if Input.is_action_pressed("ui_right"): + last_pressed_direction = "right" direction = 1 else: + last_pressed_direction = "" direction = 0 if Input.is_action_pressed("ui_up") and forward_speed_change_timer.time_left == 0.0: print_debug(forward_speed_change_timer.time_left) diff --git a/Player.tscn b/Player.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://Player.gd" type="Script" id=1] + +[sub_resource type="SpatialMaterial" id=1] +albedo_color = Color( 0.482353, 0.980392, 0.572549, 1 ) + +[sub_resource type="BoxShape" id=2] +extents = Vector3( 0.125, 0.075, 0.15 ) + +[node name="Player" type="KinematicBody"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.336291, 0 ) +script = ExtResource( 1 ) + +[node name="CSGBox2" type="CSGBox" parent="."] +width = 0.25 +height = 0.15 +depth = 0.3 +material = SubResource( 1 ) + +[node name="CollisionShape" type="CollisionShape" parent="."] +shape = SubResource( 2 ) + +[node name="ForwardSpeedChangeTimer" type="Timer" parent="."] +wait_time = 0.2 +one_shot = true diff --git a/level1.txt b/level1.txt @@ -0,0 +1,7 @@ + ###### + ###### ###### + ###### ###### ###### #### #### zzzz +###### ###### ###### ###### #### #### zzzz + ###### ###### ###### #### #### zzzz + ###### ###### + ###### diff --git a/project.godot b/project.godot @@ -16,9 +16,13 @@ _global_script_class_icons={ [application] config/name="spacevroom" -run/main_scene="res://Level1.tscn" +run/main_scene="res://LevelSelection.tscn" config/icon="res://icon.png" +[autoload] + +LevelLoader="*res://LevelLoader.gd" + [input] jump={ @@ -26,6 +30,11 @@ jump={ "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null) ] } +reset_level={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":82,"unicode":0,"echo":false,"script":null) + ] +} [rendering]