Add map rotate and speed change via keys (1, 2 , m)
This commit is contained in:
parent
9afeb85635
commit
cc4f8f169f
@ -46,6 +46,10 @@ void loadMapByName(char *name);
|
|||||||
void loadMap(map_t map);
|
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
|
//return true when provided coordinate matches the position of a collision box
|
||||||
bool checkCollides(map_t map, int x, int y);
|
bool checkCollides(map_t map, int x, int y);
|
||||||
|
|
||||||
|
@ -30,10 +30,6 @@ gameData_t game = {
|
|||||||
void gameInit()
|
void gameInit()
|
||||||
{
|
{
|
||||||
LOGI("game: initializing game...\n");
|
LOGI("game: initializing game...\n");
|
||||||
//----- snake -----
|
|
||||||
// defines initial values of game.snake
|
|
||||||
snakeInit(); //TODO assign return value to game.snake?
|
|
||||||
|
|
||||||
//----- load map -----
|
//----- load map -----
|
||||||
//load default map if no map loaded yet
|
//load default map if no map loaded yet
|
||||||
if (!game.mapIsLoaded){
|
if (!game.mapIsLoaded){
|
||||||
@ -41,6 +37,10 @@ void gameInit()
|
|||||||
loadMapByName(config.defaultMapName);
|
loadMapByName(config.defaultMapName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//----- snake -----
|
||||||
|
// defines initial values of game.snake
|
||||||
|
snakeInit(); //TODO assign return value to game.snake?
|
||||||
|
|
||||||
//--- place initial food ---
|
//--- place initial food ---
|
||||||
placeFood();
|
placeFood();
|
||||||
LOGI("game: placed initial food at x=%d, y=%d\n", game.foodX, game.foodY);
|
LOGI("game: placed initial food at x=%d, y=%d\n", game.foodX, game.foodY);
|
||||||
|
22
src/input.c
22
src/input.c
@ -4,6 +4,7 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "menu.h"
|
#include "menu.h"
|
||||||
#include "snake.h"
|
#include "snake.h"
|
||||||
|
#include "map.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -18,16 +19,17 @@ void handleInput_runningState(SDL_Event event)
|
|||||||
{
|
{
|
||||||
switch (event.key.keysym.sym)
|
switch (event.key.keysym.sym)
|
||||||
{
|
{
|
||||||
case SDLK_q:
|
case SDLK_q: // q: quit
|
||||||
game.gameState = EXIT;
|
game.gameState = EXIT;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDLK_p:
|
case SDLK_p: // p: pause
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
game.gameState = PAUSED;
|
game.gameState = PAUSED;
|
||||||
showPauseScreen();
|
showPauseScreen();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
//--- control snake direction ---
|
||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
snakeSetDir(UP);
|
snakeSetDir(UP);
|
||||||
@ -48,14 +50,26 @@ void handleInput_runningState(SDL_Event event)
|
|||||||
snakeSetDir(RIGHT);
|
snakeSetDir(RIGHT);
|
||||||
break;
|
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:
|
default:
|
||||||
LOGD("input: key %d is not handled in RUNNING mode\n", event.key.keysym.sym);
|
LOGD("input: key %d is not handled in RUNNING mode\n", event.key.keysym.sym);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//=============================
|
//=============================
|
||||||
//===== processInputEvent =====
|
//===== processInputEvent =====
|
||||||
//=============================
|
//=============================
|
||||||
|
20
src/map.c
20
src/map.c
@ -3,6 +3,8 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
int indexLoadedMap = 0;
|
||||||
|
|
||||||
//===========================
|
//===========================
|
||||||
//==== renderGameToArray ====
|
//==== 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 ======
|
//====== checkCollides ======
|
||||||
@ -208,7 +224,7 @@ static const map_t map_default = {
|
|||||||
|
|
||||||
static const map_t map_empty = {
|
static const map_t map_empty = {
|
||||||
.width = 20,
|
.width = 20,
|
||||||
.height = 10,
|
.height = 15,
|
||||||
.name = "empty",
|
.name = "empty",
|
||||||
.collisions = {},
|
.collisions = {},
|
||||||
.collisionCount = 0,
|
.collisionCount = 0,
|
||||||
@ -225,7 +241,7 @@ static const map_t map_intermediate = {
|
|||||||
.width = 15,
|
.width = 15,
|
||||||
.height = 15,
|
.height = 15,
|
||||||
.name = "intermediate",
|
.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,
|
.collisionCount = 10,
|
||||||
.portals = {
|
.portals = {
|
||||||
{.posX = 5,
|
{.posX = 5,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user