I’m looking for a way or general best practice advices for decoupling the architecture of my game, in the example below the input from the current game state workflow / entity behavior. While I’m all in for certain game development (or general) patterns such as an event- or component-driven design, I’m failing to understand how a decoupled architecture can work in a case where e.g. an entity actually needs to know a little bit more.
Take for example this little (bad) example:
// some component attached to an entity
void Update(long elapsedTime) {
var keyboardState = _keyboardDevice.State;
var gameState = _gameStateStack.CurrentState;
if(keyboardState.IsPressed(Keys.Enter) {
if(gameState.GetType == typeof(MenuState))
((MenuState)gameState).ExitMenu();
} else {
parent.Jump();
}
}
}
The requirements are pretty simple: If we’re in the game menu, pressing enter closes the menu. However if the menu is not active, pressing enter causes the entity to jump. If I’m going to decouple the menu and the entity component using an event-driven approach…
// menu state
class MenuState : IHandle<KeyCommand>() {
void Handle(KeyCommand keyCommand) {
if(keyCommand.Key == Keys.Enter && keyCommand.IsPressed)
...
}
}
// entity component
class EntityControllerComponent : IHandle<KeyCommand>() {
...
}
… the concerns are clearly separated. But what happens now if I press the ENTER key? Who decides which part of the game is allowed to actually receive and handle the event?