Viktor Kirilov
C++ dev
struct hostile_ai
{
void act()
{
auto guys = find_good_guys();
move_to(guys);
attack(guys);
}
...
};
struct player_controlled
{
void act()
{
auto key = read_keyboad();
if(key == left)
move_left();
}
...
};
struct flying_creature
{
void move_to(target t)
{
flap_wings();
my_pos += norm(t.pos() - my_pos) * my_speed;
}
...
};
struct component { object* self; };
struct Icontrol { virtual void act() = 0; }
struct Imobility { virtual void move_to(target) = 0; }
struct object {
Icontrol* control;
Imobility* mobility;
};
struct hostile_ai : public Icontrol, public component {
void act() override { self->mobility->move_to(good_guy); }
};
object dragon; // time to compose
dragon.mobility = new flying_creature;
dragon.control = new hostile_ai;
dragon.control->act();
dragon.control = new player_controlled; // mutating the control
// compose
object dragon;
mutate(dragon)
.add<hostile_ai>()
.add<flying_creature>();
act(dragon); // calls act() from hostile_ai - no uniform call syntax :(
// mutate - change control from AI to player
mutate(dragon)
.remove<hostile_ai>()
.add<player_controlled>();
act(dragon); // the same call continues to work but does something else
struct hostile_ai {
void act() {
auto guys = find_good_guys();
move_to(dm_this, guys); // dm_this == self - the object we are part of
attack(guys);
}
};
By Viktor Kirilov
A lightning talk about DynaMix for CppCon 2017