refactor code to be more decoupled

This commit is contained in:
Chris Cromer 2022-08-14 16:53:44 -04:00
parent 2a7b20ed54
commit 563cdade00
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
5 changed files with 40 additions and 23 deletions

View File

@ -137,18 +137,18 @@ func _on_input_validated(validated_player: Dictionary) -> void:
func _object_created(name: String, state: String, position: Vector2, velocity: Vector2) -> void: func _object_created(name: String, state: String, position: Vector2, velocity: Vector2) -> void:
if monitor_enabled and started: if monitor_enabled:
add_object(name, state, position, velocity) add_object(name, state, position, velocity)
func _object_updated(name: String, state: String, position: Vector2, velocity: Vector2) -> void: func _object_updated(name: String, state: String, position: Vector2, velocity: Vector2) -> void:
if monitor_enabled and started: if monitor_enabled:
remove_object(name) remove_object(name)
add_object(name, state, position, velocity) add_object(name, state, position, velocity)
func _object_removed(name: String) -> void: func _object_removed(name: String) -> void:
if monitor_enabled and started: if monitor_enabled:
remove_object(name) remove_object(name)

View File

@ -7,7 +7,6 @@
pause_mode = 2 pause_mode = 2
script = ExtResource( 1 ) script = ExtResource( 1 )
monitor_enabled = true monitor_enabled = true
use_development_url = true
[node name="MonitorGUI" parent="." instance=ExtResource( 3 )] [node name="MonitorGUI" parent="." instance=ExtResource( 3 )]
visible = false visible = false

View File

@ -46,20 +46,12 @@ void Main::_ready()
{ {
// Load monitor from pck // Load monitor from pck
Godot::print("Monitor pck found, loading..."); Godot::print("Monitor pck found, loading...");
Ref<PackedScene> monitor_scene = _resource_loader->load("res://monitor/Monitor.tscn"); load_monitor();
add_child(monitor_scene->instance());
auto monitor = get_node("Monitor");
monitor->connect("monitor_loaded", this, "_on_monitor_loaded");
get_tree()->set_pause(true);
} }
else if (_resource_loader->exists("res://monitor/Monitor.tscn")) else if (_resource_loader->exists("res://monitor/Monitor.tscn"))
{ {
// Load monitor from alai's pck // Load monitor from alai's pck
Ref<PackedScene> monitor_scene = _resource_loader->load("res://monitor/Monitor.tscn"); load_monitor();
add_child(monitor_scene->instance());
auto monitor = get_node("Monitor");
monitor->connect("monitor_loaded", this, "_on_monitor_loaded");
get_tree()->set_pause(true);
} }
else else
{ {
@ -95,15 +87,36 @@ void Main::_ready()
} }
} }
void Main::_on_monitor_loaded() { void Main::_on_monitor_loaded()
if (level != NULL) {
if (level != nullptr)
{
auto level_node = load_level();
connect("monitor_loaded", level_node->get_child(0)->find_node("Player", true, false), "_on_monitor_loaded");
emit_signal("monitor_loaded");
}
}
void Main::load_monitor()
{
Ref<PackedScene> monitor_scene = _resource_loader->load("res://monitor/Monitor.tscn");
add_child(monitor_scene->instance());
auto monitor = get_node("Monitor");
monitor->connect("monitor_loaded", this, "_on_monitor_loaded");
get_tree()->set_pause(true);
}
Node *Main::load_level()
{
if (level != nullptr)
{ {
auto path = level->get_path(); auto path = level->get_path();
Ref<PackedScene> level_scene = _resource_loader->load(path); auto loaded_level = level->instance();
auto loaded_level = level_scene->instance(); auto level_node = get_node("Level");
connect("monitor_loaded", loaded_level, "_on_monitor_loaded"); level_node->add_child(loaded_level);
get_node("Level")->add_child(loaded_level); return level_node;
} }
return nullptr;
} }
void Main::_physics_process(float delta) void Main::_physics_process(float delta)

View File

@ -218,6 +218,9 @@ namespace godot
int8_t get_launch_screen(); int8_t get_launch_screen();
void _on_monitor_loaded(); void _on_monitor_loaded();
void load_monitor();
Node *load_level();
}; };
} }
} }

View File

@ -81,7 +81,7 @@ void Player::_ready()
} }
void Player::_on_monitor_loaded() { void Player::_on_monitor_loaded() {
auto object_node = get_tree()->get_root()->get_node("Main")->find_node("Monitor", true); auto object_node = get_tree()->get_root()->find_node("Monitor", true, false);
if (object_node != nullptr) if (object_node != nullptr)
{ {
auto state = get_node("StateMachine")->get_child(0); auto state = get_node("StateMachine")->get_child(0);
@ -97,10 +97,12 @@ void Player::_on_monitor_loaded() {
WARN_PRINT("State not found!"); WARN_PRINT("State not found!");
} }
} }
#ifndef NDEBUG
else else
{ {
WARN_PRINT("Monitor not found!"); WARN_PRINT("Monitor not found!");
} }
#endif
} }
void Player::_physics_process(float delta) void Player::_physics_process(float delta)