Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced controls (inputs) #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/Config/Globals.cpp
Expand Up @@ -115,10 +115,16 @@ void Globals::init()
InputManager::bind("right", KEY_RIGHT);
InputManager::bind("up", KEY_UP);
InputManager::bind("down", KEY_DOWN);
InputManager::bind("upleft", '7');
InputManager::bind("upright", '9');
InputManager::bind("downleft", '1');
InputManager::bind("downright", '3');
InputManager::bind("clockwise", 'x');
InputManager::bind("counterclockwise", 'z');
InputManager::bind("pause", 'p');
InputManager::bind("help", 'h');
InputManager::bind("quit", 'q');


// Aww yeah, rev up dem colors
Globals::Theme::player_head = Colors::pair("green", "default", true);
Expand Down Expand Up @@ -230,6 +236,24 @@ void Globals::loadFile()
INI_GET(tmp, "input", "quit");
InputManager::bind("quit", InputManager::stringToKey(tmp));

INI_GET(tmp, "input", "upleft");
InputManager::bind("upleft", InputManager::stringToKey(tmp));

INI_GET(tmp, "input", "upright");
InputManager::bind("upright", InputManager::stringToKey(tmp));

INI_GET(tmp, "input", "downleft");
InputManager::bind("downleft", InputManager::stringToKey(tmp));

INI_GET(tmp, "input", "downright");
InputManager::bind("downright", InputManager::stringToKey(tmp));

INI_GET(tmp, "input", "clockwise");
InputManager::bind("clockwise", InputManager::stringToKey(tmp));

INI_GET(tmp, "input", "counterclockwise");
InputManager::bind("counterclockwise", InputManager::stringToKey(tmp));

// Board Size
int board_size = 2;
INI_GET(board_size, "game", "board_size");
Expand Down Expand Up @@ -337,6 +361,23 @@ void Globals::saveFile()
key = InputManager::keyToString(InputManager::getBind("quit"));
INI_SET("input", "quit", key);

key = InputManager::keyToString(InputManager::getBind("upleft"));
INI_SET("input", "upleft", key);

key = InputManager::keyToString(InputManager::getBind("upright"));
INI_SET("input", "upright", key);

key = InputManager::keyToString(InputManager::getBind("downleft"));
INI_SET("input", "downleft", key);

key = InputManager::keyToString(InputManager::getBind("downright"));
INI_SET("input", "downright", key);

key = InputManager::keyToString(InputManager::getBind("clockwise"));
INI_SET("input", "clockwise", key);

key = InputManager::keyToString(InputManager::getBind("counterclockwise"));
INI_SET("input", "counterclockwise", key);
// Board size
int board_size = Globals::Game::boardSizeToInt(Globals::Game::board_size);
INI_SET("game", "board_size", board_size);
Expand Down
26 changes: 19 additions & 7 deletions src/Display/WindowGameHelp.cpp
Expand Up @@ -8,7 +8,7 @@
WindowGameHelp::WindowGameHelp()
{
int width = 40;
int height = 17;
int height = 23;

int windowx = Layout::screenWidth/2 - width/2;
int windowy = Layout::screenHeight/2 - height/2;
Expand Down Expand Up @@ -73,39 +73,51 @@ void WindowGameHelp::run()
"Move down\n"
"Move left\n"
"Move right\n"
"Move up or left\n"
"Move up or right\n"
"Move down or left\n"
"Move down or right\n"
"Rotate clockwise\n"
"Rotate counterclockwise\n"
"Pause game\n"
"Quit anytime\n"
"Show help", '\n'),
1, 1,
2, 1,
EngineGlobals::Theme::hilite_text);

this->windows[0]->print(Utils::String::split(InputManager::keyToString(InputManager::getBind("up")) + "\n" +
InputManager::keyToString(InputManager::getBind("down")) + "\n" +
InputManager::keyToString(InputManager::getBind("left")) + "\n" +
InputManager::keyToString(InputManager::getBind("right")) + "\n" +
InputManager::keyToString(InputManager::getBind("upleft")) + "\n" +
InputManager::keyToString(InputManager::getBind("upright")) + "\n" +
InputManager::keyToString(InputManager::getBind("downleft")) + "\n" +
InputManager::keyToString(InputManager::getBind("downright")) + "\n" +
InputManager::keyToString(InputManager::getBind("clockwise")) + "\n" +
InputManager::keyToString(InputManager::getBind("counterclockwise")) + "\n" +
InputManager::keyToString(InputManager::getBind("pause")) + "\n" +
InputManager::keyToString(InputManager::getBind("quit")) + "\n" +
InputManager::keyToString(InputManager::getBind("help")), '\n'),
14, 1,
27, 1,
EngineGlobals::Theme::text);

this->windows[0]->print("Menu controls:\n",
0, 9,
0, 15,
EngineGlobals::Theme::hilite_text);

this->windows[0]->print(Utils::String::split("First item\n"
"Last item", '\n'),
1, 10,
2, 16,
EngineGlobals::Theme::hilite_text);

this->windows[0]->print(Utils::String::split("page up\n"
"page down", '\n'),
14, 10,
27, 16,
EngineGlobals::Theme::text);

this->windows[0]->print(Utils::String::split(" Settings and scores are stored at:\n"
" `~/.local/share/nsnake/`", '\n'),
0, 13,
0, 19,
EngineGlobals::Theme::text);
}
//
Expand Down
24 changes: 24 additions & 0 deletions src/Entities/Game.cpp
Expand Up @@ -216,6 +216,30 @@ void Game::handleInput()
{
this->player->move(Player::DOWN);
}
else if (InputManager::isPressed("upleft"))
{
this->player->move(Player::UPLEFT);
}
else if (InputManager::isPressed("upright"))
{
this->player->move(Player::UPRIGHT);
}
else if (InputManager::isPressed("downleft"))
{
this->player->move(Player::DOWNLEFT);
}
else if (InputManager::isPressed("downright"))
{
this->player->move(Player::DOWNRIGHT);
}
else if (InputManager::isPressed("clockwise"))
{
this->player->move(Player::CLOCKWISE);
}
else if (InputManager::isPressed("counterclockwise"))
{
this->player->move(Player::COUNTERCLOCKWISE);
}
}
void Game::update()
{
Expand Down
83 changes: 82 additions & 1 deletion src/Entities/Player.cpp
Expand Up @@ -38,7 +38,88 @@ void Player::moveTo(int x, int y)
}
void Player::move(Direction dir)
{
this->nextDirection = dir;
// we don't want UPLEFT, UPRIGHT, DOWNLEFT, DOWNRIGHT, CLOCKWISE and COUNTERCLOCKWISE to be actual directions that the snake can move, since it would require more coding when updating the board.
// so we're basically translating these directions to UP, DOWN, LEFT or RIGHT depending on the currentDirection
switch(dir)
{
case Player::UPLEFT:
if (this->currentDirection == Player::UP || this->currentDirection == Player::DOWN)
{
this->nextDirection = Player::LEFT;
}
else if (this->currentDirection == Player::LEFT || this->currentDirection == Player::RIGHT)
{
this->nextDirection = Player::UP;
}
break;
case Player::UPRIGHT:
if (this->currentDirection == Player::UP || this->currentDirection == Player::DOWN)
{
this->nextDirection = Player::RIGHT;
}
else if (this->currentDirection == Player::LEFT || this->currentDirection == Player::RIGHT)
{
this->nextDirection = Player::UP;
}
break;
case Player::DOWNLEFT:
if (this->currentDirection == Player::UP || this->currentDirection == Player::DOWN)
{
this->nextDirection = Player::LEFT;
}
else if (this->currentDirection == Player::LEFT || this->currentDirection == Player::RIGHT)
{
this->nextDirection = Player::DOWN;
}
break;
case Player::DOWNRIGHT:
if (this->currentDirection == Player::UP || this->currentDirection == Player::DOWN)
{
this->nextDirection = Player::RIGHT;
}
else if (this->currentDirection == Player::LEFT || this->currentDirection == Player::RIGHT)
{
this->nextDirection = Player::DOWN;
}
break;
case Player::CLOCKWISE:
switch (this->currentDirection)
{
case Player::UP:
this->nextDirection = Player::RIGHT;
break;
case Player::RIGHT:
this->nextDirection = Player::DOWN;
break;
case Player::DOWN:
this->nextDirection = Player::LEFT;
break;
case Player::LEFT:
this->nextDirection = Player::UP;
break;
}
break;
case Player::COUNTERCLOCKWISE:
switch (this->currentDirection)
{
case Player::UP:
this->nextDirection = Player::LEFT;
break;
case Player::RIGHT:
this->nextDirection = Player::UP;
break;
case Player::DOWN:
this->nextDirection = Player::RIGHT;
break;
case Player::LEFT:
this->nextDirection = Player::DOWN;
break;
}
break;
default:
this->nextDirection = dir;
}

}
void Player::kill()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Player.hpp
Expand Up @@ -25,7 +25,7 @@ class Player
public:
enum Direction
{
UP, DOWN, LEFT, RIGHT
UP, DOWN, LEFT, RIGHT, UPLEFT, UPRIGHT, DOWNLEFT, DOWNRIGHT, CLOCKWISE, COUNTERCLOCKWISE
};

Player(int x, int y);
Expand Down
43 changes: 43 additions & 0 deletions src/States/GameStateMainMenu.cpp
Expand Up @@ -52,6 +52,12 @@ enum NamesToEasilyIdentifyTheMenuItemsInsteadOfRawNumbers
CONTROLS_KEY_RIGHT,
CONTROLS_KEY_UP,
CONTROLS_KEY_DOWN,
CONTROLS_KEY_UPLEFT,
CONTROLS_KEY_UPRIGHT,
CONTROLS_KEY_DOWNLEFT,
CONTROLS_KEY_DOWNRIGHT,
CONTROLS_KEY_CLOCKWISE,
CONTROLS_KEY_COUNTERCLOCKWISE,
CONTROLS_KEY_PAUSE,
CONTROLS_KEY_HELP,
CONTROLS_KEY_QUIT,
Expand Down Expand Up @@ -205,9 +211,16 @@ void GameStateMainMenu::update()
case CONTROLS_KEY_RIGHT: key = "right"; break;
case CONTROLS_KEY_UP: key = "up"; break;
case CONTROLS_KEY_DOWN: key = "down"; break;
case CONTROLS_KEY_UPLEFT: key = "upleft"; break;
case CONTROLS_KEY_UPRIGHT: key = "upright"; break;
case CONTROLS_KEY_DOWNLEFT: key = "downleft"; break;
case CONTROLS_KEY_DOWNRIGHT: key = "downright"; break;
case CONTROLS_KEY_CLOCKWISE: key = "clockwise"; break;
case CONTROLS_KEY_COUNTERCLOCKWISE: key = "counterclockwise"; break;
case CONTROLS_KEY_PAUSE: key = "pause"; break;
case CONTROLS_KEY_HELP: key = "help"; break;
case CONTROLS_KEY_QUIT: key = "quit"; break;


case CONTROLS_DEFAULT:
{
Expand All @@ -216,6 +229,12 @@ void GameStateMainMenu::update()
InputManager::bind("right", KEY_RIGHT);
InputManager::bind("up", KEY_UP);
InputManager::bind("down", KEY_DOWN);
InputManager::bind("upleft", '7');
InputManager::bind("upright", '9');
InputManager::bind("downleft", '1');
InputManager::bind("downright", '3');
InputManager::bind("clockwise", 'x');
InputManager::bind("counterclockwise", 'z');
InputManager::bind("pause", 'p');
InputManager::bind("help", 'h');
InputManager::bind("quit", 'q');
Expand Down Expand Up @@ -552,6 +571,30 @@ void GameStateMainMenu::createControlsMenu()
label = new MenuItemLabel("Key right", CONTROLS_KEY_RIGHT, str);
menuControls->add(label);

str = InputManager::keyToString(InputManager::getBind("upleft"));
label = new MenuItemLabel("Key up/left", CONTROLS_KEY_UPLEFT, str);
menuControls->add(label);

str = InputManager::keyToString(InputManager::getBind("upright"));
label = new MenuItemLabel("Key up/right", CONTROLS_KEY_UPRIGHT, str);
menuControls->add(label);

str = InputManager::keyToString(InputManager::getBind("downleft"));
label = new MenuItemLabel("Key down/left", CONTROLS_KEY_DOWNLEFT, str);
menuControls->add(label);

str = InputManager::keyToString(InputManager::getBind("downright"));
label = new MenuItemLabel("Key down/right", CONTROLS_KEY_DOWNRIGHT, str);
menuControls->add(label);

str = InputManager::keyToString(InputManager::getBind("clockwise"));
label = new MenuItemLabel("Key clockwise", CONTROLS_KEY_CLOCKWISE, str);
menuControls->add(label);

str = InputManager::keyToString(InputManager::getBind("counterclockwise"));
label = new MenuItemLabel("Key counterclockwise", CONTROLS_KEY_COUNTERCLOCKWISE, str);
menuControls->add(label);

str = InputManager::keyToString(InputManager::getBind("pause"));
label = new MenuItemLabel("Key pause", CONTROLS_KEY_PAUSE, str);
menuControls->add(label);
Expand Down