mirror of
https://github.com/gelldur/EventBus.git
synced 2024-12-27 12:21:02 +08:00
Update sample
This commit is contained in:
parent
11a146bb91
commit
1e7500607b
@ -2,45 +2,48 @@
|
||||
* @brief Sample code to play with!
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <eventbus/EventBus.h>
|
||||
#include <eventbus/EventCollector.h>
|
||||
|
||||
namespace Event //Example namespace for events
|
||||
namespace Event // Example namespace for events
|
||||
{
|
||||
|
||||
struct Gold { int value = 0; }; //Event that will be proceed when our gold changes
|
||||
|
||||
struct Gold // Event that will be proceed when our gold changes
|
||||
{
|
||||
int value = 0;
|
||||
};
|
||||
}
|
||||
|
||||
enum class Monster
|
||||
{
|
||||
Frog, Tux, Shark
|
||||
Frog,
|
||||
Tux,
|
||||
Shark
|
||||
};
|
||||
|
||||
class CharacterController // some kind of character controller
|
||||
class Character
|
||||
{
|
||||
public:
|
||||
CharacterController(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||
Character(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||
: _bus{eventBus}
|
||||
{
|
||||
}
|
||||
|
||||
void kill(Monster monsterType)
|
||||
{
|
||||
if (Monster::Frog == monsterType)
|
||||
if(Monster::Frog == monsterType)
|
||||
{
|
||||
_gold += 1;
|
||||
}
|
||||
else if (Monster::Tux == monsterType)
|
||||
else if(Monster::Tux == monsterType)
|
||||
{
|
||||
_gold += 100;
|
||||
}
|
||||
else if (Monster::Shark == monsterType)
|
||||
else if(Monster::Shark == monsterType)
|
||||
{
|
||||
_gold += 25;
|
||||
}
|
||||
@ -48,7 +51,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
int _gold = 0;//Controller stores how much gold we have
|
||||
int _gold = 0;
|
||||
std::shared_ptr<Dexode::EventBus> _bus;
|
||||
};
|
||||
|
||||
@ -60,17 +63,16 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
void onDraw()//Example "draw" of UI
|
||||
void onDraw() // Example "draw" of UI
|
||||
{
|
||||
std::cout << "Gold:" << _gold << std::endl;
|
||||
}
|
||||
|
||||
void onEnter()// We could also do such things in ctor and dtor but a lot of UI has something like this
|
||||
void onEnter() // We could also do such things in ctor and dtor but a lot of UI has something
|
||||
// like this
|
||||
{
|
||||
_listener.listen<Event::Gold>([this](const auto& event)
|
||||
{
|
||||
_gold = std::to_string(event.value);
|
||||
});
|
||||
_listener.listen<Event::Gold>(
|
||||
[this](const auto& event) { _gold = std::to_string(event.value); });
|
||||
}
|
||||
|
||||
void onExit()
|
||||
@ -83,23 +85,17 @@ private:
|
||||
Dexode::EventCollector _listener;
|
||||
};
|
||||
|
||||
class ShopButton // Shop button is only enabled when we have some gold (odd decision but for sample good :P)
|
||||
class ShopButton // Shop button is only enabled when we have some gold (odd decision but for sample
|
||||
// good :P)
|
||||
{
|
||||
public:
|
||||
ShopButton(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||
: _listener{eventBus}
|
||||
{
|
||||
}
|
||||
|
||||
void onEnter()
|
||||
{
|
||||
//We can use lambda or bind your choice
|
||||
_listener.listen<Event::Gold>(std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void onExit()
|
||||
{
|
||||
_listener.unlistenAll();//unlistenAll is also called when listener is destroyed
|
||||
// We can use lambda or bind your choice
|
||||
_listener.listen<Event::Gold>(
|
||||
std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1));
|
||||
// Also we use RAII idiom to handle unlisten
|
||||
}
|
||||
|
||||
bool isEnabled() const
|
||||
@ -114,32 +110,36 @@ private:
|
||||
void onGoldUpdated(const Event::Gold& event)
|
||||
{
|
||||
_isEnabled = event.value > 0;
|
||||
std::cout << "Shop button is:" << _isEnabled << std::endl;//some kind of logs
|
||||
std::cout << "Shop button is:" << _isEnabled << std::endl; // some kind of logs
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
std::shared_ptr<Dexode::EventBus> eventBus = std::shared_ptr<Dexode::EventBus>(new Dexode::EventBus{});
|
||||
std::shared_ptr<Dexode::EventBus> eventBus = std::make_shared<Dexode::EventBus>();
|
||||
|
||||
CharacterController characterController{eventBus};
|
||||
Character characterController{eventBus};
|
||||
|
||||
UIWallet wallet{eventBus};//UIWallet doesn't know anything about character or even who store gold
|
||||
ShopButton shopButton{eventBus};//ShopButton doesn't know anything about character
|
||||
UIWallet wallet{eventBus}; // UIWallet doesn't know anything about character
|
||||
// or even who store gold
|
||||
ShopButton shopButton{eventBus}; // ShopButton doesn't know anything about character
|
||||
{
|
||||
wallet.onEnter();
|
||||
shopButton.onEnter();
|
||||
}
|
||||
|
||||
wallet.onDraw();
|
||||
{
|
||||
characterController.kill(Monster::Tux);
|
||||
|
||||
}
|
||||
wallet.onDraw();
|
||||
|
||||
//It is easy to test UI eg.
|
||||
// It is easy to test UI eg.
|
||||
eventBus->notify(Event::Gold{1});
|
||||
assert(shopButton.isEnabled() == true);
|
||||
eventBus->notify(Event::Gold{0});
|
||||
assert(shopButton.isEnabled() == false);
|
||||
|
||||
wallet.onExit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user