add event bus #31

Merged
cromer merged 1 commits from feature/event_bus into develop 2022-08-27 18:16:29 -04:00
8 changed files with 95 additions and 21 deletions

8
godot/Event.gdns Normal file
View File

@ -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 )

View File

@ -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"] = ""

View File

@ -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

View File

@ -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

20
src/Event.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "Event.h"
void alai::Event::_register_methods()
{
godot::register_signal<Event>("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<Event>("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<Event>("object_removed", "name", GODOT_VARIANT_TYPE_STRING);
}
alai::Event::Event()
{
}
alai::Event::~Event()
{
}
void alai::Event::_init()
{
}

47
src/Event.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef ALAI_EVENT_H
#define ALAI_EVENT_H
#include <Godot.hpp>
#include <Node.hpp>
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

View File

@ -1,5 +1,6 @@
#include <Godot.hpp>
#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<alai::Event>();
register_class<StateMachine>();
register_class<State>();
register_class<main::Main>();

View File

@ -1,5 +1,7 @@
#include "player/Player.h"
#include "Event.h"
#include <Camera2D.hpp>
#include <TileMap.hpp>
#include <VisibilityNotifier2D.hpp>
@ -27,9 +29,6 @@ void Player::_register_methods()
register_property<Player, float>("gravity", &Player::set_gravity, &Player::get_gravity, player::gravity);
register_property<Player, float>("run_speed", &Player::set_run_speed, &Player::get_run_speed, player::run_speed);
register_property<Player, bool>("double_jump", &Player::set_double_jump, &Player::get_double_jump, player::double_jump);
register_signal<Player>("object_created", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2);
register_signal<Player>("object_updated", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2);
register_signal<Player>("object_removed", "name", GODOT_VARIANT_TYPE_STRING);
}
Player::Player()
@ -81,29 +80,17 @@ 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)
{
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);
auto event = get_node<alai::Event>("/root/Event");
event->emit_signal("object_created", this->get_name(), state->get_name(), get_global_position(), velocity);
}
else
{
WARN_PRINT("State not found!");
}
}
#ifndef NDEBUG
else
{
WARN_PRINT("Monitor 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<alai::Event>("/root/Event");
event->emit_signal("object_updated", this->get_name(), state->get_name(), get_global_position(), velocity);
}
else
{