Interaction between entities
Consider I have some environment(let's name it world
) and some entities(let's name them bot开发者_如何学C
s), that belong to the world. I want bots to interact with each other(e.g. get a list of the bots located in the field of view of the bot). But if bots don't know about their environment, they also don't know about the others. So the question is - how does this interaction implemented in real world applications?
The solution I see is to give bots the pointer to the world, but I find it quite illogical, because each bot will have the same «rights» while interacting with other bots as the world has.
The solution I use at this moment is QT signals & slots conception. Each bot has a signal queryNeighbours(Bot *requester, QVector<Bot *> *result)
, and the world has a slot giveNeighbours(Bot *requester, QVector<Bot *> *result)
. So, then I connect bots' signals with the world's slot, and when a bot needs to get the neighbours, it just gets them like:
QVector<Bot *> bots;
emit queryBots(this, &bots);
However, I think there is some pattern, implementing what I need, so I wouldn't need QT.
Maybe you could use a class whose only aim is to handle the relations between bots and make that class available to both the world and the bots :)
精彩评论