I'm quite a bit new to Object Oriented Programming. I have to implement an interface that should account for the different input types, ie. Joystick, mouse, xbox, etc.
A movement controller class will use the interface, and a player class will implement the movement class.
I'm having trouble putting these ideas into code. Well Mostly what should go inside the interface class.
Heres how I imagine it so far:
Class InputInterface
{
//What goes here?
}
class Movement : InputInterface
{
}
//Other classes that inherit InputInterface
class PlayerShip
{
public:
MovementController* movementController;
}
So I do know that most 'controller' classes (Joystick, 360 controller, etc), use functions that return either boolean or float values. So I may have data members in the interface class that can have their values set by member functions in the interface class.
The best thing with the strategy pattern is to keep things simple. The method that is relevant for your client object will go into the interface and expose only the information about the internal object that the client will need to know to call the method. You will construct the concrete object outside of the client object.
template<typename T>
class IInput
{
public:
virtual ~IInput() { } // if you want to clean up via this interface instead
// of the concrete object you will need a virtual destructor
virtual T Poll() = 0;
};
I've made the type generic, but you don't have to.
Your player object might look like this:
#include <memory>
class Player
{
std::shared_ptr<IInput<float>> velocityx;
float displacementx;
public:
Player(std::shared_ptr<IInput<float>> v)
:velocityx(v)
{
displacementx = 0;
}
void Update(float elapsedSeconds)
{
displacementx += elapsedSeconds * velocityx->Poll();
}
};
and be constructed like this (imagining MouseMover is your concrete class)
Player player(std::make_shared<MouseMover>());
or dynamically like this:
std::shared_ptr<Player> player = std::make_shared<Player>(std::make_shared<MouseMover>());