Jump to content
  • Advertisement
Sign in to follow this  
ms75214

Controllers

This topic is 1850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!