diff --git a/include/map.h b/include/map.h index 49fff07..8c6dc0f 100644 --- a/include/map.h +++ b/include/map.h @@ -46,6 +46,10 @@ void loadMapByName(char *name); void loadMap(map_t map); +//load next map in stored maps (rotate through stored maps) +void rotateMapNext(); + + //return true when provided coordinate matches the position of a collision box bool checkCollides(map_t map, int x, int y); diff --git a/src/game.c b/src/game.c index 4f8933a..7c899e9 100644 --- a/src/game.c +++ b/src/game.c @@ -30,10 +30,6 @@ gameData_t game = { void gameInit() { LOGI("game: initializing game...\n"); - //----- snake ----- - // defines initial values of game.snake - snakeInit(); //TODO assign return value to game.snake? - //----- load map ----- //load default map if no map loaded yet if (!game.mapIsLoaded){ @@ -41,6 +37,10 @@ void gameInit() loadMapByName(config.defaultMapName); } + //----- snake ----- + // defines initial values of game.snake + snakeInit(); //TODO assign return value to game.snake? + //--- place initial food --- placeFood(); LOGI("game: placed initial food at x=%d, y=%d\n", game.foodX, game.foodY); diff --git a/src/input.c b/src/input.c index 832da9b..c1adfff 100644 --- a/src/input.c +++ b/src/input.c @@ -4,6 +4,7 @@ #include "game.h" #include "menu.h" #include "snake.h" +#include "map.h" @@ -18,16 +19,17 @@ void handleInput_runningState(SDL_Event event) { switch (event.key.keysym.sym) { - case SDLK_q: + case SDLK_q: // q: quit game.gameState = EXIT; break; - case SDLK_p: + case SDLK_p: // p: pause case SDLK_ESCAPE: game.gameState = PAUSED; showPauseScreen(); break; + //--- control snake direction --- case SDLK_UP: case SDLK_w: snakeSetDir(UP); @@ -48,14 +50,26 @@ void handleInput_runningState(SDL_Event event) snakeSetDir(RIGHT); break; + case SDLK_m: // m: cycle through maps + rotateMapNext(); + break; + + case SDLK_2: // 2: speed up game by increment + config.cycleDurationMs -= sqrt(config.cycleDurationMs) + 1; + if (config.cycleDurationMs < 20) + config.cycleDurationMs = 20; + break; + + case SDLK_1: // 1: slow down game by increment + config.cycleDurationMs += 50; + break; + default: LOGD("input: key %d is not handled in RUNNING mode\n", event.key.keysym.sym); } return; } - - //============================= //===== processInputEvent ===== //============================= diff --git a/src/map.c b/src/map.c index c2e943f..879c3c0 100644 --- a/src/map.c +++ b/src/map.c @@ -3,6 +3,8 @@ #include "game.h" #include "common.h" +int indexLoadedMap = 0; + //=========================== //==== renderGameToArray ==== //=========================== @@ -167,6 +169,20 @@ void loadMap(map_t map) } +//=========================== +//====== rotateMapNext ====== +//=========================== +//load next map in stored maps (rotate through stored maps) +void rotateMapNext(){ + if (indexLoadedMap >= storedMapsCount -1 ){ + indexLoadedMap = 0; + } else { + indexLoadedMap ++; + } + loadMap(*storedMaps[indexLoadedMap]); +} + + //=========================== //====== checkCollides ====== @@ -208,7 +224,7 @@ static const map_t map_default = { static const map_t map_empty = { .width = 20, - .height = 10, + .height = 15, .name = "empty", .collisions = {}, .collisionCount = 0, @@ -225,7 +241,7 @@ static const map_t map_intermediate = { .width = 15, .height = 15, .name = "intermediate", - .collisions = {{8, 9}, {8, 8}, {4, 5}, {0, 1}, {9, 9}, {7, 5}, {4, 0}, {3, 0}, {12, 11}, {14, 13}}, + .collisions = {{8, 9}, {8, 8}, {4, 6}, {0, 1}, {9, 9}, {7, 6}, {4, 0}, {3, 0}, {12, 11}, {14, 13}}, .collisionCount = 10, .portals = { {.posX = 5,