diff options
| author | Jan Tuomi <jan.tuomi@valuemotive.com> | 2020-08-01 16:39:38 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan.tuomi@valuemotive.com> | 2020-08-01 16:39:38 +0300 |
| commit | 9dfb7f8037be735d5e5f88fb47ae07d98dc39274 (patch) | |
| tree | 866584e5ef71b91056a599a5a4b870cb38cb9bd5 | |
| parent | bc73f5f075540c2f60b3ec2e216a3ecda85fdfc3 (diff) | |
Move levels under a Game node
| -rw-r--r-- | project.godot | 8 | ||||
| -rw-r--r-- | scenes/Game.tscn | 14 | ||||
| -rw-r--r-- | scenes/Level1.tscn (renamed from scenes/Scene1.tscn) | 8 | ||||
| -rw-r--r-- | scenes/Level2.tscn (renamed from scenes/Scene2.tscn) | 33 | ||||
| -rw-r--r-- | scripts/Game.gd | 42 | ||||
| -rw-r--r-- | scripts/Ladder.gd | 4 | ||||
| -rw-r--r-- | scripts/PlayerCharacter.gd | 10 |
7 files changed, 77 insertions, 42 deletions
diff --git a/project.godot b/project.godot index 0233336..d677db4 100644 --- a/project.godot +++ b/project.godot @@ -19,6 +19,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://scripts/GUI.gd" }, { +"base": "Node", +"class": "Game", +"language": "GDScript", +"path": "res://scripts/Game.gd" +}, { "base": "KinematicBody2D", "class": "PlayerCharacter", "language": "GDScript", @@ -27,13 +32,14 @@ _global_script_classes=[ { _global_script_class_icons={ "DwarfNPC": "", "GUI": "", +"Game": "", "PlayerCharacter": "" } [application] config/name="Rpg 1" -run/main_scene="res://scenes/Scene1.tscn" +run/main_scene="res://scenes/Game.tscn" config/icon="res://icon.png" [rendering] diff --git a/scenes/Game.tscn b/scenes/Game.tscn new file mode 100644 index 0000000..35d3bfd --- /dev/null +++ b/scenes/Game.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://scenes/Level1.tscn" type="PackedScene" id=1] +[ext_resource path="res://scenes/GUI.tscn" type="PackedScene" id=2] +[ext_resource path="res://scripts/Game.gd" type="Script" id=3] + +[node name="Game" type="Node"] +script = ExtResource( 3 ) + +[node name="Level" parent="." instance=ExtResource( 1 )] + +[node name="CanvasLayer" type="CanvasLayer" parent="."] + +[node name="GUI" parent="CanvasLayer" instance=ExtResource( 2 )] diff --git a/scenes/Scene1.tscn b/scenes/Level1.tscn index d6652d9..87924f5 100644 --- a/scenes/Scene1.tscn +++ b/scenes/Level1.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=53 format=2] +[gd_scene load_steps=52 format=2] [ext_resource path="res://scenes/PlayerCharacter.tscn" type="PackedScene" id=1] [ext_resource path="res://textures/world_tileset.tres" type="Texture" id=2] @@ -6,7 +6,6 @@ [ext_resource path="res://scripts/Scene1.gd" type="Script" id=4] [ext_resource path="res://externals/point_light.png" type="Texture" id=5] [ext_resource path="res://scenes/Ladder.tscn" type="PackedScene" id=6] -[ext_resource path="res://scenes/GUI.tscn" type="PackedScene" id=7] [sub_resource type="OccluderPolygon2D" id=1] polygon = PoolVector2Array( 8, 8, 0, 8, 0, 0, 8, 0 ) @@ -312,7 +311,6 @@ tile_data = PoolIntArray( -262152, 0, 0, -262151, 0, 0, -262150, 0, 0, -262149, [node name="PlayerCharacter" parent="YSort" instance=ExtResource( 1 )] position = Vector2( 12.8563, 19.5552 ) speed = 32 -gui_node = NodePath("../../CanvasLayer/GUI") [node name="Camera2D" type="Camera2D" parent="YSort/PlayerCharacter"] current = true @@ -339,7 +337,3 @@ target = NodePath("../PlayerCharacter") [node name="Ladder" parent="YSort" instance=ExtResource( 6 )] target_scene_num = 2 - -[node name="CanvasLayer" type="CanvasLayer" parent="."] - -[node name="GUI" parent="CanvasLayer" instance=ExtResource( 7 )] diff --git a/scenes/Scene2.tscn b/scenes/Level2.tscn index a19590b..6312f9c 100644 --- a/scenes/Scene2.tscn +++ b/scenes/Level2.tscn @@ -1,12 +1,11 @@ -[gd_scene load_steps=53 format=2] +[gd_scene load_steps=52 format=2] -[ext_resource path="res://scenes/PlayerCharacter.tscn" type="PackedScene" id=1] -[ext_resource path="res://textures/world_tileset.tres" type="Texture" id=2] -[ext_resource path="res://scenes/DwarfNPC.tscn" type="PackedScene" id=3] -[ext_resource path="res://scripts/Scene1.gd" type="Script" id=4] +[ext_resource path="res://textures/world_tileset.tres" type="Texture" id=1] +[ext_resource path="res://scenes/DwarfNPC.tscn" type="PackedScene" id=2] +[ext_resource path="res://scenes/Ladder.tscn" type="PackedScene" id=3] +[ext_resource path="res://scenes/PlayerCharacter.tscn" type="PackedScene" id=4] [ext_resource path="res://externals/point_light.png" type="Texture" id=5] -[ext_resource path="res://scenes/Ladder.tscn" type="PackedScene" id=6] -[ext_resource path="res://scripts/GUI.gd" type="Script" id=7] +[ext_resource path="res://scripts/Scene1.gd" type="Script" id=6] [sub_resource type="OccluderPolygon2D" id=1] polygon = PoolVector2Array( 8, 8, 0, 8, 0, 0, 8, 0 ) @@ -142,7 +141,7 @@ points = PoolVector2Array( 4, 4, 4, 0, 8, 0, 8, 8, 0, 8, 0, 4 ) [sub_resource type="TileSet" id=45] 0/name = "world_tileset.tres 0" -0/texture = ExtResource( 2 ) +0/texture = ExtResource( 1 ) 0/tex_offset = Vector2( 0, 0 ) 0/modulate = Color( 1, 1, 1, 1 ) 0/region = Rect2( 0, 0, 64, 56 ) @@ -297,7 +296,7 @@ points = PoolVector2Array( 4, 4, 4, 0, 8, 0, 8, 8, 0, 8, 0, 4 ) 0/z_index = 0 [node name="Scene2" type="Node2D"] -script = ExtResource( 4 ) +script = ExtResource( 6 ) [node name="YSort" type="YSort" parent="."] @@ -309,7 +308,7 @@ cell_y_sort = true format = 1 tile_data = PoolIntArray( -262152, 0, 0, -262151, 0, 0, -262150, 0, 0, -262149, 0, 0, -262148, 0, 0, -262147, 0, 0, -262146, 0, 0, -262145, 0, 0, -327680, 0, 0, -327679, 0, 0, -327678, 0, 0, -327677, 0, 0, -327676, 0, 0, -327675, 0, 0, -327674, 0, 0, -327673, 0, 0, -327672, 0, 0, -327671, 0, 0, -327670, 0, 0, -327669, 0, 0, -327668, 0, 0, -327667, 0, 0, -327666, 0, 0, -327665, 0, 0, -327664, 0, 0, -327663, 0, 0, -327662, 0, 0, -327661, 0, 0, -327660, 0, 0, -196616, 0, 0, -196615, 0, 0, -196614, 0, 0, -196613, 0, 0, -196612, 0, 0, -196611, 0, 0, -196610, 0, 0, -196609, 0, 0, -262144, 0, 0, -262143, 0, 0, -262142, 0, 0, -262141, 0, 0, -262140, 0, 0, -262139, 0, 0, -262138, 0, 0, -262137, 0, 0, -262136, 0, 0, -262135, 0, 0, -262134, 0, 0, -262133, 0, 0, -262132, 0, 0, -262131, 0, 0, -262130, 0, 0, -262129, 0, 0, -262128, 0, 0, -262127, 0, 0, -262126, 0, 0, -262125, 0, 0, -262124, 0, 0, -131080, 0, 0, -131079, 0, 0, -131078, 0, 0, -131077, 0, 0, -131076, 0, 0, -131075, 0, 0, -131074, 0, 0, -131073, 0, 0, -196608, 0, 0, -196607, 0, 0, -196606, 0, 0, -196605, 0, 0, -196604, 0, 0, -196603, 0, 0, -196602, 0, 0, -196601, 0, 0, -196600, 0, 0, -196599, 0, 0, -196598, 0, 0, -196597, 0, 0, -196596, 0, 0, -196595, 0, 0, -196594, 0, 0, -196593, 0, 0, -196592, 0, 0, -196591, 0, 0, -196590, 0, 0, -196589, 0, 0, -196588, 0, 0, -65544, 0, 0, -65543, 0, 0, -65542, 0, 0, -65541, 0, 0, -65540, 0, 0, -65539, 0, 0, -65538, 0, 0, -65537, 0, 0, -131072, 0, 0, -131071, 0, 0, -131070, 0, 0, -131069, 0, 0, -131068, 0, 0, -131067, 0, 0, -131066, 0, 0, -131065, 0, 0, -131064, 0, 0, -131063, 0, 0, -131062, 0, 0, -131061, 0, 0, -131060, 0, 0, -131059, 0, 0, -131058, 0, 0, -131057, 0, 0, -131056, 0, 0, -131055, 0, 0, -131054, 0, 0, -131053, 0, 0, -131052, 0, 0, -8, 0, 0, -7, 0, 0, -6, 0, 0, -5, 0, 0, -4, 0, 0, -3, 0, 0, -2, 0, 0, -1, 0, 0, -65536, 0, 0, -65535, 0, 0, -65534, 0, 0, -65533, 0, 0, -65532, 0, 0, -65531, 0, 0, -65530, 0, 0, -65529, 0, 0, -65528, 0, 0, -65527, 0, 0, -65526, 0, 0, -65525, 0, 0, -65524, 0, 0, -65523, 0, 0, -65522, 0, 0, -65521, 0, 0, -65520, 0, 0, -65519, 0, 0, -65518, 0, 0, -65517, 0, 0, -65516, 0, 0, 65528, 0, 0, 65529, 0, 0, 65530, 0, 0, 65531, 0, 0, 65532, 0, 0, 65533, 0, 0, 65534, 0, 0, 65535, 0, 0, 0, 0, 65540, 1, 0, 1, 2, 0, 1, 3, 0, 1, 4, 0, 1, 5, 0, 1, 6, 0, 1, 7, 0, 1, 8, 0, 1, 9, 0, 1, 10, 0, 1, 11, 0, 1, 12, 0, 65541, 13, 0, 0, 14, 0, 0, 15, 0, 0, 16, 0, 0, 17, 0, 0, 18, 0, 0, 19, 0, 0, 20, 0, 0, 131064, 0, 0, 131065, 0, 0, 131066, 0, 0, 131067, 0, 0, 131068, 0, 0, 131069, 0, 0, 131070, 0, 0, 131071, 0, 0, 65536, 0, 65539, 65548, 0, 65538, 65549, 0, 0, 65550, 0, 0, 65551, 0, 0, 65552, 0, 0, 65553, 0, 0, 65554, 0, 0, 65555, 0, 0, 65556, 0, 0, 196600, 0, 0, 196601, 0, 0, 196602, 0, 0, 196603, 0, 0, 196604, 0, 0, 196605, 0, 0, 196606, 0, 0, 196607, 0, 0, 131072, 0, 65539, 131084, 0, 65538, 131085, 0, 0, 131086, 0, 0, 131087, 0, 0, 131088, 0, 0, 131089, 0, 0, 131090, 0, 0, 131091, 0, 0, 131092, 0, 0, 262136, 0, 0, 262137, 0, 0, 262138, 0, 0, 262139, 0, 0, 262140, 0, 0, 262141, 0, 5, 262142, 0, 1, 262143, 0, 1, 196608, 0, 131075, 196620, 0, 2, 196621, 0, 0, 196622, 0, 0, 196623, 0, 0, 196624, 0, 0, 196625, 0, 0, 196626, 0, 0, 196627, 0, 0, 196628, 0, 0, 327672, 0, 0, 327673, 0, 0, 327674, 0, 0, 327675, 0, 0, 327676, 0, 0, 262156, 0, 65538, 262157, 0, 0, 262158, 0, 0, 262159, 0, 0, 262160, 0, 0, 262161, 0, 0, 262162, 0, 0, 262163, 0, 0, 262164, 0, 0, 393208, 0, 0, 393209, 0, 0, 393210, 0, 0, 393211, 0, 0, 393212, 0, 0, 393213, 0, 5, 393214, 0, 1, 393215, 0, 1, 327680, 0, 3, 327692, 0, 65538, 327693, 0, 0, 327694, 0, 0, 327695, 0, 0, 327696, 0, 0, 327697, 0, 0, 327698, 0, 0, 327699, 0, 0, 327700, 0, 0, 458744, 0, 0, 458745, 0, 0, 458746, 0, 0, 458747, 0, 0, 458748, 0, 0, 458749, 0, 0, 458750, 0, 0, 458751, 0, 0, 393216, 0, 3, 393228, 0, 65538, 393229, 0, 0, 393230, 0, 0, 393231, 0, 0, 393232, 0, 0, 393233, 0, 0, 393234, 0, 0, 393235, 0, 0, 393236, 0, 0, 524280, 0, 0, 524281, 0, 0, 524282, 0, 0, 524283, 0, 0, 524284, 0, 0, 524285, 0, 0, 524286, 0, 0, 524287, 0, 0, 458752, 0, 65539, 458764, 0, 65538, 458765, 0, 0, 458766, 0, 0, 458767, 0, 0, 458768, 0, 0, 458769, 0, 0, 458770, 0, 0, 458771, 0, 0, 458772, 0, 0, 589816, 0, 0, 589817, 0, 0, 589818, 0, 0, 589819, 0, 0, 589820, 0, 0, 589821, 0, 0, 589822, 0, 0, 589823, 0, 0, 524288, 0, 131076, 524289, 0, 1, 524290, 0, 1, 524291, 0, 1, 524292, 0, 1, 524293, 0, 1, 524294, 0, 1, 524295, 0, 1, 524296, 0, 1, 524297, 0, 1, 524298, 0, 1, 524299, 0, 1, 524300, 0, 131077, 524301, 0, 0, 524302, 0, 0, 524303, 0, 0, 524304, 0, 0, 524305, 0, 0, 524306, 0, 0, 524307, 0, 0, 524308, 0, 0, 655352, 0, 0, 655353, 0, 0, 655354, 0, 0, 655355, 0, 0, 655356, 0, 0, 655357, 0, 0, 655358, 0, 0, 655359, 0, 0, 589824, 0, 0, 589825, 0, 0, 589826, 0, 0, 589827, 0, 0, 589828, 0, 0, 589829, 0, 0, 589830, 0, 0, 589831, 0, 0, 589832, 0, 0, 589833, 0, 0, 589834, 0, 0, 589835, 0, 0, 589836, 0, 0, 589837, 0, 0, 589838, 0, 0, 589839, 0, 0, 589840, 0, 0, 589841, 0, 0, 589842, 0, 0, 589843, 0, 0, 589844, 0, 0, 720888, 0, 0, 720889, 0, 0, 720890, 0, 0, 720891, 0, 0, 720892, 0, 0, 720893, 0, 0, 720894, 0, 0, 720895, 0, 0, 655360, 0, 0, 655361, 0, 0, 655362, 0, 0, 655363, 0, 0, 655364, 0, 0, 655365, 0, 0, 655366, 0, 0, 655367, 0, 0, 655368, 0, 0, 655369, 0, 0, 655370, 0, 0, 655371, 0, 0, 655372, 0, 0, 655373, 0, 0, 655374, 0, 0, 655375, 0, 0, 655376, 0, 0, 655377, 0, 0, 655378, 0, 0, 655379, 0, 0, 655380, 0, 0, 786424, 0, 0, 786425, 0, 0, 786426, 0, 0, 786427, 0, 0, 786428, 0, 0, 786429, 0, 0, 786430, 0, 0, 786431, 0, 0, 720896, 0, 0, 720897, 0, 0, 720898, 0, 0, 720899, 0, 0, 720900, 0, 0, 720901, 0, 0, 720902, 0, 0, 720903, 0, 0, 720904, 0, 0, 720905, 0, 0, 720906, 0, 0, 720907, 0, 0, 720908, 0, 0, 720909, 0, 0, 720910, 0, 0, 720911, 0, 0, 720912, 0, 0, 720913, 0, 0, 720914, 0, 0, 720915, 0, 0, 720916, 0, 0, 851960, 0, 0, 851961, 0, 0, 851962, 0, 0, 851963, 0, 0, 851964, 0, 0, 851965, 0, 0, 851966, 0, 0, 851967, 0, 0, 786432, 0, 0, 786433, 0, 0, 786434, 0, 0, 786435, 0, 0, 786436, 0, 0, 786437, 0, 0, 786438, 0, 0, 786439, 0, 0, 786440, 0, 0, 786441, 0, 0, 786442, 0, 0, 786443, 0, 0, 786444, 0, 0, 786445, 0, 0, 786446, 0, 0, 786447, 0, 0, 786448, 0, 0, 786449, 0, 0, 786450, 0, 0, 786451, 0, 0, 786452, 0, 0 ) -[node name="PlayerCharacter" parent="YSort" instance=ExtResource( 1 )] +[node name="PlayerCharacter" parent="YSort" instance=ExtResource( 4 )] position = Vector2( 90.8539, 19.871 ) speed = 32 @@ -333,20 +332,10 @@ shadow_filter = 1 [node name="DwarfNPC" parent="YSort" groups=[ "Dialogue", -] instance=ExtResource( 3 )] +] instance=ExtResource( 2 )] position = Vector2( 17.3501, 55.8153 ) target = NodePath("../../../Scene2/YSort/PlayerCharacter") -[node name="Ladder" parent="YSort" instance=ExtResource( 6 )] +[node name="Ladder" parent="YSort" instance=ExtResource( 3 )] position = Vector2( 88, 48 ) target_scene_num = 1 - -[node name="CanvasLayer" type="CanvasLayer" parent="."] - -[node name="GUI" type="MarginContainer" parent="CanvasLayer"] -margin_right = 40.0 -margin_bottom = 40.0 -script = ExtResource( 7 ) -__meta__ = { -"_edit_use_anchors_": false -} diff --git a/scripts/Game.gd b/scripts/Game.gd new file mode 100644 index 0000000..e36fdfe --- /dev/null +++ b/scripts/Game.gd @@ -0,0 +1,42 @@ +extends Node +class_name Game +onready var gui: GUI = $CanvasLayer/GUI + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready(): + update_debug_gui() + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + +func update_debug_gui(): + var current_level = $Level.get_filename() + gui.set_debug_text("Level: %s" % current_level) + +func _on_Level_ready(): + yield(get_tree(), "idle_frame") + update_debug_gui() + +func change_level(num: int): + var new_scene = "res://scenes/Level" + String(num) + ".tscn" + + var cur_level = $Level + self.remove_child(cur_level) + cur_level.call_deferred("free") + + var new_level_res = load(new_scene) + var new_level: Node = new_level_res.instance() + + print("Changed level to " + new_scene) + var err = new_level.connect("ready", self, "_on_Level_ready") + if err: + push_error(err) + + self.add_child(new_level) + new_level.set_name("Level") diff --git a/scripts/Ladder.gd b/scripts/Ladder.gd index c21752b..9c566da 100644 --- a/scripts/Ladder.gd +++ b/scripts/Ladder.gd @@ -18,5 +18,5 @@ func _ready(): func _on_Ladder_body_entered(body): if body is PlayerCharacter: - var player: PlayerCharacter = body - player.change_level(target_scene_num) + var game: Game = get_tree().get_root().get_node("Game") + game.call_deferred("change_level", target_scene_num) diff --git a/scripts/PlayerCharacter.gd b/scripts/PlayerCharacter.gd index 1df100c..3cf4518 100644 --- a/scripts/PlayerCharacter.gd +++ b/scripts/PlayerCharacter.gd @@ -65,13 +65,3 @@ func _physics_process(_delta): dialogue_active_with = coll.collider run_dialogue(coll.collider) break - -func change_level(num: int): - var new_scene = "res://scenes/Scene" + String(num) + ".tscn" - var gui: GUI = get_node(gui_node) - var success = get_tree().change_scene(new_scene) - if success == OK: - gui.set_debug_text("Level: %s" % num) - print("Changed level to " + new_scene) - else: - push_error("Failed to change level to " + new_scene) |
