From 97d84ef36ab46d754f9f9791c17ed00b926223d5 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 16 Apr 2022 17:20:45 -0400 Subject: [PATCH] make level dynamic from main --- godot/Main.tscn | 5 ++--- src/Main.cpp | 16 ++++++++++++++++ src/Main.h | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/godot/Main.tscn b/godot/Main.tscn index 84d446d..0b5ed2e 100644 --- a/godot/Main.tscn +++ b/godot/Main.tscn @@ -1,9 +1,8 @@ [gd_scene load_steps=3 format=2] [ext_resource path="res://Main.gdns" type="Script" id=1] -[ext_resource path="res://levels/Level2.tscn" type="PackedScene" id=2] +[ext_resource path="res://levels/Prototype.tscn" type="PackedScene" id=2] [node name="Main" type="Node"] script = ExtResource( 1 ) - -[node name="Level2" parent="." instance=ExtResource( 2 )] +level = ExtResource( 2 ) diff --git a/src/Main.cpp b/src/Main.cpp index 387ff6e..4f91c0c 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -9,6 +9,7 @@ void Main::_register_methods() { register_method("_ready", &Main::_ready); register_method("_physics_process", &Main::_physics_process); + register_property>("level", &Main::set_level, &Main::get_level, NULL, GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, String("PackedScene")); register_property("full_screen", &Main::set_full_screen, &Main::get_full_screen, main::full_screen); register_property("window_size", &Main::set_window_size, &Main::get_window_size, main::window_size); register_property("launch_screen", &Main::set_launch_screen, &Main::get_launch_screen, main::launch_screen); @@ -45,6 +46,11 @@ void Main::_ready() _os->get_screen_position(get_launch_screen()) + _os->get_screen_size() * 0.5 - _os->get_window_size() * 0.5 ); } + + if (level != NULL) + { + add_child(level->instance()); + } } void Main::_physics_process(float delta) @@ -55,6 +61,16 @@ void Main::_physics_process(float delta) } } +void Main::set_level(Ref level) +{ + this->level = level; +} + +Ref Main::get_level() +{ + return this->level; +} + void Main::set_full_screen(bool full_screen) { this->full_screen = full_screen; diff --git a/src/Main.h b/src/Main.h index 4d86783..d89c5c9 100644 --- a/src/Main.h +++ b/src/Main.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include /** * @brief This is the godot namespace for all the code included in the library. @@ -57,6 +59,11 @@ namespace godot */ Input *_input; + /** + * @brief The first level to load + * + */ + Ref level; /** * @brief If the window is full screen or not. * @@ -116,6 +123,20 @@ namespace godot */ void _physics_process(float delta); + /** + * @brief Set the level object. + * + * @param[in] level The new level to load when starting. + */ + void set_level(Ref level); + + /** + * @brief Get the level object. + * + * @return Ref The level scene to load. + */ + Ref get_level(); + /** * @brief Set the full screen object. *