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!
|
* @brief Sample code to play with!
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <eventbus/EventBus.h>
|
#include <eventbus/EventBus.h>
|
||||||
#include <eventbus/EventCollector.h>
|
#include <eventbus/EventCollector.h>
|
||||||
|
|
||||||
namespace Event //Example namespace for events
|
namespace Event // Example namespace for events
|
||||||
{
|
{
|
||||||
|
struct Gold // Event that will be proceed when our gold changes
|
||||||
struct Gold { int value = 0; }; //Event that will be proceed when our gold changes
|
{
|
||||||
|
int value = 0;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class Monster
|
enum class Monster
|
||||||
{
|
{
|
||||||
Frog, Tux, Shark
|
Frog,
|
||||||
|
Tux,
|
||||||
|
Shark
|
||||||
};
|
};
|
||||||
|
|
||||||
class CharacterController // some kind of character controller
|
class Character
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CharacterController(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
Character(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||||
: _bus{eventBus}
|
: _bus{eventBus}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void kill(Monster monsterType)
|
void kill(Monster monsterType)
|
||||||
{
|
{
|
||||||
if (Monster::Frog == monsterType)
|
if(Monster::Frog == monsterType)
|
||||||
{
|
{
|
||||||
_gold += 1;
|
_gold += 1;
|
||||||
}
|
}
|
||||||
else if (Monster::Tux == monsterType)
|
else if(Monster::Tux == monsterType)
|
||||||
{
|
{
|
||||||
_gold += 100;
|
_gold += 100;
|
||||||
}
|
}
|
||||||
else if (Monster::Shark == monsterType)
|
else if(Monster::Shark == monsterType)
|
||||||
{
|
{
|
||||||
_gold += 25;
|
_gold += 25;
|
||||||
}
|
}
|
||||||
@ -48,7 +51,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _gold = 0;//Controller stores how much gold we have
|
int _gold = 0;
|
||||||
std::shared_ptr<Dexode::EventBus> _bus;
|
std::shared_ptr<Dexode::EventBus> _bus;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -56,21 +59,20 @@ class UIWallet
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UIWallet(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
UIWallet(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||||
: _listener{eventBus}
|
: _listener{eventBus}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void onDraw()//Example "draw" of UI
|
void onDraw() // Example "draw" of UI
|
||||||
{
|
{
|
||||||
std::cout << "Gold:" << _gold << std::endl;
|
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)
|
_listener.listen<Event::Gold>(
|
||||||
{
|
[this](const auto& event) { _gold = std::to_string(event.value); });
|
||||||
_gold = std::to_string(event.value);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onExit()
|
void onExit()
|
||||||
@ -83,23 +85,17 @@ private:
|
|||||||
Dexode::EventCollector _listener;
|
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:
|
public:
|
||||||
ShopButton(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
ShopButton(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||||
: _listener{eventBus}
|
: _listener{eventBus}
|
||||||
{
|
{
|
||||||
}
|
// We can use lambda or bind your choice
|
||||||
|
_listener.listen<Event::Gold>(
|
||||||
void onEnter()
|
std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1));
|
||||||
{
|
// Also we use RAII idiom to handle unlisten
|
||||||
//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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isEnabled() const
|
bool isEnabled() const
|
||||||
@ -114,32 +110,36 @@ private:
|
|||||||
void onGoldUpdated(const Event::Gold& event)
|
void onGoldUpdated(const Event::Gold& event)
|
||||||
{
|
{
|
||||||
_isEnabled = event.value > 0;
|
_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[])
|
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
|
UIWallet wallet{eventBus}; // UIWallet doesn't know anything about character
|
||||||
ShopButton shopButton{eventBus};//ShopButton doesn't know anything about character
|
// or even who store gold
|
||||||
|
ShopButton shopButton{eventBus}; // ShopButton doesn't know anything about character
|
||||||
{
|
{
|
||||||
wallet.onEnter();
|
wallet.onEnter();
|
||||||
shopButton.onEnter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
characterController.kill(Monster::Tux);
|
wallet.onDraw();
|
||||||
|
{
|
||||||
|
characterController.kill(Monster::Tux);
|
||||||
|
}
|
||||||
wallet.onDraw();
|
wallet.onDraw();
|
||||||
|
|
||||||
//It is easy to test UI eg.
|
// It is easy to test UI eg.
|
||||||
eventBus->notify(Event::Gold{1});
|
eventBus->notify(Event::Gold{1});
|
||||||
assert(shopButton.isEnabled() == true);
|
assert(shopButton.isEnabled() == true);
|
||||||
eventBus->notify(Event::Gold{0});
|
eventBus->notify(Event::Gold{0});
|
||||||
assert(shopButton.isEnabled() == false);
|
assert(shopButton.isEnabled() == false);
|
||||||
|
|
||||||
|
wallet.onExit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user