I'm writing a game where the main character is a ghost, and therefor can possess just about everything. The problem is, I'm not sure how to efficiently & effectively handle the changing actions of the player.
My idea was to have the game controller have a pointer to the current Entity, and a map of keys to actions. Every entity that is possess-able will have a component called OnPossess, which will just be a wrapper around a function pointer to some global function for that type of entity. When that entity gets "possessed", the OnPossess function is called and the controllers key-to-action map is updated.
Something like this:
class Controller
{
public:
Entity* controlled_entity_;
std::map<char, std::function<void(Entity*)> > action_map_;
void OnInput(char input);
};
struct Component : public OnPossess
{
std::function<void(Controller*)> OnPossess;
}
namespace Character_A
{
void OnPossess(Controller* controller) {
controller.action_map_ = Character_A_Map;
}
std::map<char, std::function<void(Entity*)> > Character_A_Map = { std::make_pair('l',MoveLeft),
std::make_pair('r', MoveRight)
};
void MoveLeft(Entity* entity) {
//Move left stuff
}
void MoveRight(Entity* entity) {
//Move right stuff
}
} //End namespace Character_A
However, I feel as though this is not the way to go. Having every type of entities available actions sitting out in a global namespace with a function pointer map pointing to those functions just doesn't feel right. I had also thought about having a component that held the map of actions, with the actions just being lambdas, but then I have a bunch of copies of the same map lying around in memory.
Would there be any alternative solutions?