Added Level singleton to manage the (un)loading of levels and entrypoints
This commit is contained in:
parent
642e98b44e
commit
71eae88a33
7 changed files with 80 additions and 22 deletions
57
core/globals/levels.gd
Normal file
57
core/globals/levels.gd
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
extends Node
|
||||||
|
|
||||||
|
class Entrypoint extends Object:
|
||||||
|
var scene_name: String
|
||||||
|
var player_position: Vector2
|
||||||
|
var reset_physics: bool
|
||||||
|
func _init(
|
||||||
|
scene_name_: String,
|
||||||
|
player_position_: Vector2,
|
||||||
|
reset_physics: bool
|
||||||
|
) -> void:
|
||||||
|
self.scene_name = scene_name_
|
||||||
|
self.player_position = player_position_
|
||||||
|
self.reset_physics = reset_physics
|
||||||
|
|
||||||
|
const SCENES = {
|
||||||
|
"intro": "uid://c6w7lrydi43ts"
|
||||||
|
}
|
||||||
|
|
||||||
|
var ENTRYPOINTS = {
|
||||||
|
"intro_start": Entrypoint.new("intro", Vector2(0, 0), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
# load that stuff
|
||||||
|
|
||||||
|
var level_root: Node
|
||||||
|
var player: Node2D
|
||||||
|
|
||||||
|
func load_scene(scn_name: String) -> bool: # returns true on success
|
||||||
|
if level_root == null:
|
||||||
|
push_error("Can't load level, level_root is not registered yet.")
|
||||||
|
return false
|
||||||
|
if not scn_name in SCENES:
|
||||||
|
push_error("Level " + scn_name + " doesn't exist.")
|
||||||
|
return false
|
||||||
|
unload_scene()
|
||||||
|
var scn = load(SCENES[scn_name])
|
||||||
|
level_root.add_child(scn.instantiate())
|
||||||
|
return true
|
||||||
|
|
||||||
|
func unload_scene():
|
||||||
|
for c in level_root.get_children():
|
||||||
|
c.queue_free()
|
||||||
|
|
||||||
|
func load_entrypoint(ep_name: String) -> bool: # returns true on success
|
||||||
|
if not ep_name in ENTRYPOINTS:
|
||||||
|
push_error("Entrypoint " + ep_name + " doesn't exist.")
|
||||||
|
return false
|
||||||
|
if player == null:
|
||||||
|
push_error("Can't load entrypoint, player is not registered yet.")
|
||||||
|
return false
|
||||||
|
var e: Entrypoint = ENTRYPOINTS[ep_name]
|
||||||
|
if load_scene(e.scene_name):
|
||||||
|
player.position = e.player_position
|
||||||
|
return true
|
||||||
|
else:
|
||||||
|
return false
|
3
levels/intro.tscn
Normal file
3
levels/intro.tscn
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[gd_scene format=3 uid="uid://c6w7lrydi43ts"]
|
||||||
|
|
||||||
|
[node name="Intro" type="Node2D"]
|
|
@ -1,19 +0,0 @@
|
||||||
[gd_scene load_steps=3 format=3 uid="uid://ptqto8ctsp4u"]
|
|
||||||
|
|
||||||
[ext_resource type="PackedScene" uid="uid://ebb4pfxklatj" path="res://player/player.tscn" id="1_h1tob"]
|
|
||||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/dynamic_polygon.tscn" id="2_83sm2"]
|
|
||||||
|
|
||||||
[node name="WakingUp" type="Node2D"]
|
|
||||||
|
|
||||||
[node name="Player" parent="." instance=ExtResource("1_h1tob")]
|
|
||||||
position = Vector2(400, 224)
|
|
||||||
|
|
||||||
[node name="StaticBody2D" type="StaticBody2D" parent="."]
|
|
||||||
|
|
||||||
[node name="DynamicPolygon" parent="StaticBody2D" instance=ExtResource("2_83sm2")]
|
|
||||||
polygon = PackedVector2Array(296, 288, 296, 368, 536, 368, 536, 336, 776, 336, 776, 400, 104, 400, 104, 360, 184, 360, 184, 304, 248, 304, 248, 288)
|
|
||||||
color = Color(0.235294, 0.607843, 0.643137, 1)
|
|
||||||
|
|
||||||
[node name="DynamicPolygon2" parent="StaticBody2D" instance=ExtResource("2_83sm2")]
|
|
||||||
polygon = PackedVector2Array(520, 96, 520, 280, 552, 280, 552, 32, 632, 32, 632, -8, 520, -8)
|
|
||||||
color = Color(0.236278, 0.606088, 0.644729, 1)
|
|
6
main.gd
Normal file
6
main.gd
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
extends Node2D
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
Levels.level_root = $LevelRoot
|
||||||
|
Levels.player = $Player
|
||||||
|
Levels.load_entrypoint("intro_start")
|
10
main.tscn
10
main.tscn
|
@ -1,9 +1,10 @@
|
||||||
[gd_scene load_steps=7 format=3 uid="uid://c13h0uf5fgx60"]
|
[gd_scene load_steps=8 format=3 uid="uid://c13h0uf5fgx60"]
|
||||||
|
|
||||||
[ext_resource type="PackedScene" uid="uid://ptqto8ctsp4u" path="res://levels/waking_up.tscn" id="1_3swad"]
|
[ext_resource type="Script" path="res://main.gd" id="1_m407c"]
|
||||||
[ext_resource type="Shader" path="res://core/shaders/screen_effects.gdshader" id="2_ha2mu"]
|
[ext_resource type="Shader" path="res://core/shaders/screen_effects.gdshader" id="2_ha2mu"]
|
||||||
[ext_resource type="Shader" path="res://core/shaders/screen_effects_2.gdshader" id="3_lm730"]
|
[ext_resource type="Shader" path="res://core/shaders/screen_effects_2.gdshader" id="3_lm730"]
|
||||||
[ext_resource type="Texture2D" uid="uid://bwlgypt4xjocj" path="res://core/shaders/vignette_mask.tres" id="4_wc582"]
|
[ext_resource type="Texture2D" uid="uid://bwlgypt4xjocj" path="res://core/shaders/vignette_mask.tres" id="4_wc582"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://ebb4pfxklatj" path="res://player/player.tscn" id="5_0agpg"]
|
||||||
|
|
||||||
[sub_resource type="ShaderMaterial" id="ShaderMaterial_myg35"]
|
[sub_resource type="ShaderMaterial" id="ShaderMaterial_myg35"]
|
||||||
shader = ExtResource("2_ha2mu")
|
shader = ExtResource("2_ha2mu")
|
||||||
|
@ -13,8 +14,9 @@ shader = ExtResource("3_lm730")
|
||||||
shader_parameter/vignette_mask = ExtResource("4_wc582")
|
shader_parameter/vignette_mask = ExtResource("4_wc582")
|
||||||
|
|
||||||
[node name="Main" type="Node2D"]
|
[node name="Main" type="Node2D"]
|
||||||
|
script = ExtResource("1_m407c")
|
||||||
|
|
||||||
[node name="WakingUp" parent="." instance=ExtResource("1_3swad")]
|
[node name="LevelRoot" type="Node2D" parent="."]
|
||||||
|
|
||||||
[node name="ScreenShader" type="Node2D" parent="."]
|
[node name="ScreenShader" type="Node2D" parent="."]
|
||||||
|
|
||||||
|
@ -41,3 +43,5 @@ anchor_right = 1.0
|
||||||
anchor_bottom = 1.0
|
anchor_bottom = 1.0
|
||||||
grow_horizontal = 2
|
grow_horizontal = 2
|
||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
|
|
||||||
|
[node name="Player" parent="." instance=ExtResource("5_0agpg")]
|
||||||
|
|
|
@ -34,3 +34,6 @@ func _physics_process(delta: float) -> void:
|
||||||
velocity.y = jump_velocity
|
velocity.y = jump_velocity
|
||||||
jumps += 1
|
jumps += 1
|
||||||
move_and_slide()
|
move_and_slide()
|
||||||
|
|
||||||
|
func reset_physics():
|
||||||
|
velocity = Vector2.ZERO
|
||||||
|
|
|
@ -16,6 +16,10 @@ config/features=PackedStringArray("4.3", "Forward Plus")
|
||||||
boot_splash/bg_color=Color(0, 0, 0, 1)
|
boot_splash/bg_color=Color(0, 0, 0, 1)
|
||||||
config/icon="res://icon.svg"
|
config/icon="res://icon.svg"
|
||||||
|
|
||||||
|
[autoload]
|
||||||
|
|
||||||
|
Levels="*res://core/globals/levels.gd"
|
||||||
|
|
||||||
[debug]
|
[debug]
|
||||||
|
|
||||||
settings/stdout/print_fps=true
|
settings/stdout/print_fps=true
|
||||||
|
|
Reference in a new issue