Game design

Hello everyone! I’m here once again to ask you something! ::slight_smile:

I’m going to try to implement ALL the things you guys been suggesting me. Starting from GAME STATES.

So I took what ra4king and zngga told me here and try to implement it.

This is what I have so far.

|- engine
||- GameEngine: Creates a JFrame and adds a canvas in it. Has the main loop |(while (isRunning)), inside it, there’s the time handling, the update and the |render methods.
|
|- entities
||- Entity: Base class for every object.
||- All other entities that extends Entity.
|
|- main
||- Main: A simple main method that turns on the GameEngine and runs the |run() method.
|
|- states
||- ScreenState: Base class for evey state in the game, has update(), ||render(), enterState(), leaveState() and mouse and keyboard handling.
||
||- Other states are GameMapViewState, GameMenuState and GamePlayState

Is this ok? Am I on the right track?
How do I implement the KeyInputHandler? And if I want to add a mouse input handler?

Things to do:

  • Change between states (100%)
  • Support mouse and keyboard input (100%)
  • Fix time managment (ticks, fps, anything else) (70%)
  • Add animation (0%)
  • Support spritesheets (0%)
  • Add sounds & music (10%)
  • Separate core engine from game engine (45%)

Edit: 6/4/12: Modified the OP to add TODO things and made the “design tree”
Edit: 9/4/12: Added things to the TODO list, plus % done and colors :slight_smile:

Two points from me:

  • sleep(10) is not that good (precision is platform dependent, not reliable). I think there is a verbose thread somewhere here about timing
  • I wouldn’t expose attributes of class Entity as public attributes

Mouse and keyboard input ?
Just call addMouseListener() or addKeyListener on your UI component. But be careful about threading issues. Listeners will be called from the AWT thread.

Thanks for your reply :smiley:

  • I’ll look later about timing, right now I’m working just on design, but thanks, I’ll keep it in mind (and noted on my to-do list).
  • Class Entity was very quick made by copy pasting things, I will make it better soon!

So, every game state should have a mouse and keyboard listener?
And about threading, I’ll keep it very simple for now :slight_smile:

Thanks again! :smiley:

You could add one pair of listeners once and forward events to the current state. Otherwise you would rather have to attach and detach listeners on every state change.

That’s good. Nevertheless you have to be aware of the issues of Swing not being threadsafe, how to update the UI, about visibility of variables and locking maybe.

Awesome looking code, couple thoughts from me too:

  • You don’t have to get the screen size and do all that math to center the window, a simple call to frame.setLocationRelativeTo(null) does just that.
  • Use this code to force Windows to use the high resolution timer so sleep(10) would become more accurate, like 65K mentioned.
  • To get events, just create an inner class in GameEngine that implements KeyListener, MouseListener, and MouseMotionListener. This way, you can just propagate the events to the currentState’s appropriate methods.