Source code for the Player class of the platforming game "Celeste" released as open-source

General rule is keep things single-purpose, you should be able to describe everything a file does in 2-3 sentences. example, in an FPS, does the code you use to move your player have to be in the same file as the code that handles shooting? Probably not

For player movement, I like breaking things down into states. Often you'll do something like if (onGround) { /* code */ } else if (inAir) { /* code */ } else if (climbing) { /* code */ }. That can be moved to different classes, you have one class for when you're on the ground, one class for inAir, etc and provide a way to toggle state

So like if you're on the ground, you don't need to be applying gravity every update. So you'd do something like

update() {
     if (noGroundBelow) {
          enterState(State.INAIR);
     }
     if (playerPressesJump) {
          enterState(State.JUMP);
     }
}

This also saves an if/then mess, because if the player can't double jump, you just leave the "player press jump" key out of the InAir class

/r/gamedev Thread Parent Link - github.com