aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaure <paure@student.42lyon.fr>2022-01-11 20:48:06 +0100
committerPaure <paure@student.42lyon.fr>2022-01-11 20:48:06 +0100
commitff969a6dd485c94e0c1147219e0a485bc6563aee (patch)
treed4c11053ae189103cfa739fc2f9f123f654186f1
parentclang format blind letsgo ! (diff)
downloadthreshold-ff969a6dd485c94e0c1147219e0a485bc6563aee.tar.gz
threshold-ff969a6dd485c94e0c1147219e0a485bc6563aee.tar.bz2
threshold-ff969a6dd485c94e0c1147219e0a485bc6563aee.tar.xz
threshold-ff969a6dd485c94e0c1147219e0a485bc6563aee.tar.zst
threshold-ff969a6dd485c94e0c1147219e0a485bc6563aee.zip
fix player leaving the map
-rw-r--r--src/gameplay.cpp22
1 files changed, 17 insertions, 5 deletions
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)) {