Sign in to follow this  
ms75214

Controllers

Recommended Posts

I have a spaceship, and I want to make it possible to control it using different mechanisms.  Like Keyboard, mouse, joystick.
 
I want to do something like this:
 
spaceship.setController(new KeyBoardController()).
 
 
Does anybody do things this way or am I on the wrong track?
 
Thanks.
 

Share this post


Link to post
Share on other sites

I think you're over thinking it.  I'd suggest doing something simpler to begin with, like have simple function that you call and check for a specific action.  Like, check for move forward. main fire key, alternate fire key, etc.  And, in that function, you can check the multiple input controllers for the input.  SO, you may have 'w' mapped to move forward, or left mouse button, or up on the joystick.  If any of those are true, the function returns true.  Like this:

 

enum etKeyAction
{
  KEY_MOVE_FOWARD,
  ...
  KEY_MAIN_FIRE,
  ...
};
 
bool IsKeyDown(etKeyAction keyAction)
{
  if (KeyboardDown(keyAction) {
    return true;
  }
  else if (MouseDown(keyAction) {
 ...
}

Share this post


Link to post
Share on other sites
I personally like the idea of having a controller interface and having multiple concrete implementations. I think it creates a flexible system that lets you add any number of different input types.

I think BeerNutts' solution is good for a simple game, because it is easier to lay down that implementation quickly, but I personally would go with a controller interface pattern as you have described above.

Share this post


Link to post
Share on other sites

I personally like the idea of having a controller interface and having multiple concrete implementations. I think it creates a flexible system that lets you add any number of different input types.

I agree it would be nice.

Share this post


Link to post
Share on other sites

I personally like the idea of having a controller interface and having multiple concrete implementations. I think it creates a flexible system that lets you add any number of different input types.

I think BeerNutts' solution is good for a simple game, because it is easier to lay down that implementation quickly, but I personally would go with a controller interface pattern as you have described above.

 

 

How does this look for a controller interface?

public interface Controller
{

    float getX();
    float getY();

    // returns whether the x and y positions are delta or absolute positions
    boolean isDelta();


}

Thanks.
 

Share this post


Link to post
Share on other sites

How do you plan on using this Controller interface?  Above you have "spaceship.setController(new KeyBoardController())", but that extremely limiting.  Will you be setting the controller for the spaceship every time it is created?  Why wouldn't you pass it a JoystickController, or a MouseController?  How will you handle that?

 

I still say you're over thinking this.  What can you gain from doing this versus calling a function to check if a particular key function is being pressed, or to call, say, GetMouseCoordinates(), or GetMouseMovement()?  If you don't like the polling method, then work out an event system, but I still don't see the gain from a Controller interface.

 

If you still want to go the Controller route, you're example needs work.  This is simply for finding coordinates of the mouse position, and is totally lacking for a generic "Controller" interface.  You should:

#1, remove isDelta().  You are writing the controllers, so you should have control over what it gives (absolute or changes only); otherwise, every call to controller might also need a call to isDelta().  Go ahead and make it one or the other.

#2, where are the button instances?  Not just mouse buttons, but any button your game might use (Fire button, move forward button, inventory button, etc.)?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this