I'm having trouble wrapping my head around a method to compartmentalize my game control scheme within the game loop in an efficient way. Currently this is basically how my control works in bad pseudocode / JavaScript. I think this type of scheme is pretty standard for simple games..
Init() {
function onKeyDown( event ) {
switch( event.code ) {
case 'KeyA':
moveLeft = true;
case 'KeyD':
moveRight = true;
etc...
}
document.addEventListener( 'keydown', onKeyDown, false );
}
GameLoop() {
CheckControls();
MoveWorldObjects();
Render();
}
CheckControls() {
if ( moveLeft ) velocity.x -= 10;
if ( moveRight ) velocity.x += 10;
etc...
}
This method has worked fine for me so far, especially since the eventListening is abstracted, and relatively quick for what I need it to do. However, as my game gets bigger the controls are starting to look very disorganized and I'm starting to wonder if this is a good program flow for a larger game.
Another problem is the fact that there are multiple control schemes in the game, so different characters will use different keys to activate different skills. It made sense to me to try loading different schemes at run time from JSON files, but I can't wrap my head around how I would do event handling from JSON, because I can only store values/gameStates, which I am already doing. Adding a JSON layer to the controls doesn't seem like it would help me at this point.
Is there an approach to organizing game control that I should read about? Or is it a good idea to keep all of the control responses very close to the game loop and simply try to work around that limitation. Or perhaps there is a way to separate basic controls (like movement) from more advanced/less high-speed responses like pulling up a UI?
Thanks in advance for any help! -G