Автор Борислав Станимиров
Реалните случаи са по-тежки: Обекти в игра
...най-сетне
flying_creature
two_legged_creature
monster_ai
animated_model
directx_rendering
monster_ai
с player_control
и ставаме шофьор на драконdirectx_rendering
с opengl_rendering
- имаме игра за Линуксflying_creature
- (двукрак) гущерfire_breather
- става страшноEntity-component system
Entity
class game_entity
{
control* m_control;
physical_data* m_physical_data;
rendering* rendering;
vector<type*> m_types; // ??
...
};
game_entity dragon;
dragon.set_control(new monster_ai);
dragon.set_physical_data(new animated_model("dragon.model"));
dragon.add_type(new flying_creature);
...
dragon.set_ai(new player_control);
Component
class component
{
entity* self;
};
class monster_ai : public control, public component
{
void simulation_tick() override
{
...
self->get_physical_data()->move_to(enemy);
self->get_physical_data()->animate("flying"); // ??
}
};
Entity-component система на стероиди
Boost.Mixin.Дракон
object dragon; // just an empty object
mutate(dragon)
.add<flying_creature>().add<two_legged_creature>()
.add<monster_ai>().add<animated_model>().add<directx_rendering>();
set_model(dragon, "dragon.model"); // handled by animated_model
...
mutate(dragon).remove<monster_ai>().add<player_control>();
Boost.Mixin Миксин
class monster_ai
{
void simulate_tick()
{
...
move_to(bm_this, enemy);
animate(bm_this, "flying");
}
};
bm_this = self =
обектът, от който миксинът е част
BOOST_DECLARE_MIXIN(monster_ai);
BOOST_DEFINE_MIXIN(monster_ai, simulate_tick_msg & get_info_msg);
BOOST_MIXIN_MESSAGE_0(void, simulate_tick);
BOOST_MIXIN_MESSAGE_1(void, move_to, object*, target);
BOOST_MIXIN_MESSAGE_2(rt, foo, arg1_t, arg1, arg2_t, arg2);
BOOST_MIXIN_DEFINE_MESSAGE(simulate_tick);
BOOST_MIXIN_DEFINE_MESSAGE(move_to);
BOOST_MIXIN_DEFINE_MESSAGE(foo);
BOOST_MIXIN_MULTICAST_MESSAGE_1(void, get_info, ostream&, out);
...
get_info(obj, cout);
get_rendering_elements
... & foo_msg & priority(5, bar_msg));
mutate(monster).add<berserk_ai>();
... // do berserk stuff
mutate(monster).remove<berserk_ai>();
... // do regular stuff
РТФМ @ ibob.github.io/boost.mixin/
Документацията не е съвсем пълна :(
Boost.Mixin е тук: github.com/iboB/boost.mixin
Презентацията е лицензирана с Creative Commons Признание 3.0