Use event bus for coins

This commit is contained in:
Martin Araneda 2022-08-27 18:32:33 -04:00
parent 8b53f74929
commit 705ed4e897
3 changed files with 9 additions and 9 deletions

View File

@ -5,6 +5,9 @@ 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_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_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); godot::register_signal<Event>("object_removed", "name", GODOT_VARIANT_TYPE_STRING);
godot::register_signal<Event>("coin_collected", "amount", GODOT_VARIANT_TYPE_INT);
godot::register_signal<Event>("player_died");
} }
alai::Event::Event() alai::Event::Event()

View File

@ -1,5 +1,6 @@
#include "coin/CoinCollected.h" #include "coin/CoinCollected.h"
#include <AnimationPlayer.hpp> #include <AnimationPlayer.hpp>
#include "Event.h"
using namespace godot; using namespace godot;
@ -8,7 +9,6 @@ void CoinCollected::_register_methods()
register_method("_state_enter", &CoinCollected::_state_enter); register_method("_state_enter", &CoinCollected::_state_enter);
register_method("_state_exit", &CoinCollected::_state_exit); register_method("_state_exit", &CoinCollected::_state_exit);
register_method("_on_animation_finished", &CoinCollected::_on_animation_finished); register_method("_on_animation_finished", &CoinCollected::_on_animation_finished);
register_signal<CoinCollected>("coin_collected", "amount", GODOT_VARIANT_TYPE_INT);
} }
CoinCollected::CoinCollected() CoinCollected::CoinCollected()
@ -43,7 +43,8 @@ void CoinCollected::_state_exit()
void CoinCollected::_on_animation_finished(String anim_name) void CoinCollected::_on_animation_finished(String anim_name)
{ {
emit_signal("coin_collected", 1); auto event = get_node<alai::Event>("/root/Event");
event->emit_signal("coin_collected", 1);
this->get_parent()->queue_free(); this->get_parent()->queue_free();

View File

@ -1,6 +1,7 @@
#include "coin/CoinCounter.h" #include "coin/CoinCounter.h"
#include <String.hpp> #include <String.hpp>
#include "coin/CoinCollected.h" #include "coin/CoinCollected.h"
#include "Event.h"
using namespace godot; using namespace godot;
void CoinCounter::_register_methods() void CoinCounter::_register_methods()
@ -35,13 +36,8 @@ void CoinCounter::_on_coin_collected(int amount)
void CoinCounter::_ready() void CoinCounter::_ready()
{ {
set_text("0"); set_text("0");
auto coins_node = get_node("../../Coins"); auto event = get_node<alai::Event>("/root/Event");
auto children_count = coins_node->get_child_count(); event->connect("coin_collected", this, "_on_coin_collected");
for(int64_t i = 0; i < children_count;i++)
{
auto child = coins_node->get_child(i);
child->get_node("StateMachine/CoinCollected")->connect("coin_collected",this,"_on_coin_collected");
}
} }