From 3eb3d8765af625188841b5fc3c028575c6d0fc09 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 27 Aug 2022 18:10:36 -0400 Subject: [PATCH] add event bus --- godot/Event.gdns | 8 +++++++ godot/monitor/Monitor.gd | 4 ++++ godot/monitor/Monitor.tscn | 1 + godot/project.godot | 4 ++++ src/Event.cpp | 20 ++++++++++++++++ src/Event.h | 47 ++++++++++++++++++++++++++++++++++++++ src/godot.cpp | 2 ++ src/player/Player.cpp | 30 ++++++++---------------- 8 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 godot/Event.gdns create mode 100644 src/Event.cpp create mode 100644 src/Event.h diff --git a/godot/Event.gdns b/godot/Event.gdns new file mode 100644 index 0000000..5668ff6 --- /dev/null +++ b/godot/Event.gdns @@ -0,0 +1,8 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://gdnative/alai.tres" type="GDNativeLibrary" id=1] + +[resource] +resource_name = "Event" +class_name = "Event" +library = ExtResource( 1 ) diff --git a/godot/monitor/Monitor.gd b/godot/monitor/Monitor.gd index 004c1f9..a9ff7d7 100644 --- a/godot/monitor/Monitor.gd +++ b/godot/monitor/Monitor.gd @@ -50,6 +50,10 @@ var game: Dictionary = {} func _ready() -> void: + Event.connect("object_created", self, "_object_created") + Event.connect("object_updated", self, "_object_updated") + Event.connect("object_removed", self, "_object_removed") + game_version = get_parent().game_version player["rut"] = "" diff --git a/godot/monitor/Monitor.tscn b/godot/monitor/Monitor.tscn index 58f135d..b987a56 100644 --- a/godot/monitor/Monitor.tscn +++ b/godot/monitor/Monitor.tscn @@ -6,6 +6,7 @@ [node name="Monitor" type="Node"] pause_mode = 2 script = ExtResource( 1 ) +monitor_enabled = true [node name="MonitorGUI" parent="." instance=ExtResource( 3 )] visible = false diff --git a/godot/project.godot b/godot/project.godot index b01c12e..3a913d7 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -15,6 +15,10 @@ config/description="This game is for testing an Artificial Intelligence." run/main_scene="res://Main.tscn" config/icon="res://icon.png" +[autoload] + +Event="*res://Event.gdns" + [display] window/size/width=512 diff --git a/src/Event.cpp b/src/Event.cpp new file mode 100644 index 0000000..3e9e186 --- /dev/null +++ b/src/Event.cpp @@ -0,0 +1,20 @@ +#include "Event.h" + +void alai::Event::_register_methods() +{ + godot::register_signal("object_created", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2); + godot::register_signal("object_updated", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2); + godot::register_signal("object_removed", "name", GODOT_VARIANT_TYPE_STRING); +} + +alai::Event::Event() +{ +} + +alai::Event::~Event() +{ +} + +void alai::Event::_init() +{ +} diff --git a/src/Event.h b/src/Event.h new file mode 100644 index 0000000..87607c6 --- /dev/null +++ b/src/Event.h @@ -0,0 +1,47 @@ +#ifndef ALAI_EVENT_H +#define ALAI_EVENT_H + +#include +#include + +namespace alai +{ + /** + * @brief This class provides an event bus for the project. + * + * @details This class should be an auto-loaded singleton for the project. + * To use this use Event.connect() and Event.emit_signal() to have global signals. + */ + class Event : public godot::Node + { + GODOT_CLASS(Event, godot::Node) + public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ + static void _register_methods(); + + /** + * @brief Construct a new Event object. + * + */ + Event(); + + /** + * @brief Destroy the Event object. + * + */ + ~Event(); + + /** + * @brief Initialize the class from Godot. + * + * @details This method is called just once when the Godot engine connects to the instance of the class. + */ + void _init(); + }; +} + +#endif diff --git a/src/godot.cpp b/src/godot.cpp index 1c56d09..80bfa16 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -1,5 +1,6 @@ #include +#include "Event.h" #include "state_machine/StateMachine.h" #include "state_machine/State.h" #include "Main.h" @@ -40,6 +41,7 @@ extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_opt extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) { Godot::nativescript_init(handle); + register_class(); register_class(); register_class(); register_class(); diff --git a/src/player/Player.cpp b/src/player/Player.cpp index 868156b..17da42d 100644 --- a/src/player/Player.cpp +++ b/src/player/Player.cpp @@ -1,5 +1,7 @@ #include "player/Player.h" +#include "Event.h" + #include #include #include @@ -27,9 +29,6 @@ void Player::_register_methods() register_property("gravity", &Player::set_gravity, &Player::get_gravity, player::gravity); register_property("run_speed", &Player::set_run_speed, &Player::get_run_speed, player::run_speed); register_property("double_jump", &Player::set_double_jump, &Player::get_double_jump, player::double_jump); - register_signal("object_created", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2); - register_signal("object_updated", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2); - register_signal("object_removed", "name", GODOT_VARIANT_TYPE_STRING); } Player::Player() @@ -81,28 +80,16 @@ void Player::_ready() } void Player::_on_monitor_loaded() { - auto object_node = get_tree()->get_root()->find_node("Monitor", true, false); - if (object_node != nullptr) + auto state = get_node("StateMachine")->get_child(0); + if (state != nullptr) { - auto state = get_node("StateMachine")->get_child(0); - if (state != nullptr) - { - connect("object_created", object_node, "_object_created"); - connect("object_updated", object_node, "_object_updated"); - connect("object_removed", object_node, "_object_removed"); - emit_signal("object_created", this->get_name(), state->get_name(), get_global_position(), velocity); - } - else - { - WARN_PRINT("State not found!"); - } + auto event = get_node("/root/Event"); + event->emit_signal("object_created", this->get_name(), state->get_name(), get_global_position(), velocity); } -#ifndef NDEBUG else { - WARN_PRINT("Monitor not found!"); + WARN_PRINT("State not found!"); } -#endif } void Player::_physics_process(float delta) @@ -193,7 +180,8 @@ void Player::_physics_process(float delta) auto state = get_node("StateMachine")->get_child(0); if (state != nullptr) { - emit_signal("object_updated", this->get_name(), state->get_name(), get_global_position(), velocity); + auto event = get_node("/root/Event"); + event->emit_signal("object_updated", this->get_name(), state->get_name(), get_global_position(), velocity); } else { -- 2.30.2