0
\$\begingroup\$

I am working on a java 2d game. Keeping things short, I'd like to improve my current input handling. Basically the player can move up/down/left/right with WASD, perform an attack with mouse left click and shoot with mouse right click.

Following this article I implemented my own finite state machine like:

interface State {
 State transition(Input input, double deltatime)

 void update(Input input, Entity e, double deltatime)
}

where Entity is player (I'm using some variant of entity-component-system, but its not relevant here). Then a state would look like this:

class WalkState implements State {
   State transition(Input input, double deltatime) {
     if (input.isShooting()) 
         return new ShootState()
     if (input.isAttacking())
         return new AttackState()
     if (!input.isMoving())
         return new IdleState()
     return null;
   }

   void update(Input input, Entity e, double deltatime) {
     if (input.isUp()) 
        //move up
     if (input.isDown())
        //move down
     ...
   }
}

The code that updates the player every tick looks like this:

if player is blocked executing animation
    return; // do not react to new input
var currState = player.getState()
newState = currState.transition(input, deltatime);
if (newState != null) 
    player.setState(newState)
player.getState.update(input, player, deltatime)
// update animation by getting a string description from the state

My doubts are that maybe using a finite state machine for this is not optimal and that the input taking class reimains made of boolean fields to be checked. I know of command pattern but I thought states gave me more flexibility. For instance I could insert timers into some state to trigger extra actions. Any suggestion is appreciated

\$\endgroup\$
2
  • 4
    \$\begingroup\$ What do you want to improve about the system? You mention that you're concerned it's "not optimal", but optimality requires context. Most input systems aren't performance bottlenecks, so it's reasonable to prioritize code clarity & maintainability over the small added expense of the FSM. What are you specifically do you seek to optimize? Also, the command pattern can be used with the state pattern - it doesn't need to be one or the other. The command pattern decouples keypresses from method calls (allowing easier rebinding) whereas the state pattern manages the outcomes. \$\endgroup\$ Commented May 1, 2023 at 13:39
  • 3
    \$\begingroup\$ I generally advise against questions asking "would this work? / is this good?" because there's a hidden ending to it: "is this good for my game's needs?" - of the group of us, you are the one who has access to your game and knows its design intimately, as well as the development experience of working with it. You are the person best positioned to assess how well you find this solution is meeting your needs. So: in what way does this fall short of your needs or hopes for it? What would be different in a "better" version? \$\endgroup\$ Commented May 1, 2023 at 15:48

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.