I saw raymath.h contained a Clamp() method, so rather than doing a multi-condition check that discarded all movement if going out of bounds, it instead just acts like a cage and lets you carry on moving along any unconstrained walls or edge
I also didn’t want this cluttering my core loop directly, so refactored it out to a separate method that can be used generically for all Characters to keep them in bounds if desired. I did still place it immediately after knight.tick(); though, but it’s now just a call to keepCharacterInWorldBounds(knight);
void keepCharacterInWorldBounds(Character &character) {
Vector2 mapSize { MAP_TILESIZE * MAP_X * MAP_SCALE, MAP_TILESIZE * MAP_Y * MAP_SCALE };
auto worldPosition = character.getWorldPosition();
auto x = Clamp(worldPosition.x, 0.f, mapSize.x - SCREEN_WIDTH);
auto y = Clamp(worldPosition.y, 0.f, mapSize.y - SCREEN_HEIGHT);
character.setWorldPosition((int)x, (int)y);
}
I just preferred this approach to having either a complicated logic or halting all movement if any one axis/direction was blocked.
Edit: Changed my code from a pointer instance to a stack scoped instance. Still knocking the rust (no pun) off my old knowledge of C/C++.