From ff969a6dd485c94e0c1147219e0a485bc6563aee Mon Sep 17 00:00:00 2001 From: Paure Date: Tue, 11 Jan 2022 20:48:06 +0100 Subject: fix player leaving the map --- src/gameplay.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 54270e3..cedc084 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -208,24 +208,36 @@ int Game::tick() const { } int Game::getKeys() const { - auto oldX = 0, oldY = 0; // get position before processing keys to check + auto oldX = 0, oldY = 0, speed = 7; // get position before processing keys to check // for player movement in threshold mode oldX = player->posX; oldY = player->posY; if (IsKeyDown(KEY_W)) { player->posX += 0; - player->posY += -7; + if ((player->posY + speed) < 0) + player->posY = 0; + else + player->posY += -speed; } if (IsKeyDown(KEY_S)) { player->posX += 0; - player->posY += 7; + if ((player->posY + speed) > SCREENHEIGHT) + player->posY = SCREENHEIGHT; + else + player->posY += speed; } if (IsKeyDown(KEY_A)) { - player->posX += -7; + if ((player->posX - speed) < 0) // avoid leaving the map + player->posX = 0; + else + player->posX += -speed; player->posY += 0; } if (IsKeyDown(KEY_D)) { - player->posX += 7; + if ((player->posX + speed) > SCREENWIDTH) + player->posX = SCREENWIDTH; + else + player->posX += speed; player->posY += 0; } if (player->fury >= 5 && IsKeyDown(KEY_E)) { -- cgit v1.2.3