Skip to content

Commit

Permalink
Dang it does (add slightly broken input)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale-Ko committed Sep 12, 2024
1 parent 1b0816b commit ac23970
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ namespace Snake

void Snake::Game::run()
{
UUID* keyDownUuid = this->eventManager->registerKeyDownListener([this](Snake::KeyStruct key) {
this->handleKey(key);
});

std::this_thread::sleep_for(std::chrono::milliseconds(1000));

while (this->running)
Expand All @@ -107,6 +111,29 @@ namespace Snake

std::this_thread::sleep_for(std::chrono::milliseconds(500));
}

this->eventManager->unregisterKeyDownListener(keyDownUuid);
}

void Snake::Game::handleKey(const Snake::KeyStruct key)
{
switch (key.code)
{
case Snake::Key::KEY_W:
this->headDirection = Snake::Direction::NORTH;
break;
case Snake::Key::KEY_S:
this->headDirection = Snake::Direction::SOUTH;
break;
case Snake::Key::KEY_D:
this->headDirection = Snake::Direction::EAST;
break;
case Snake::Key::KEY_A:
this->headDirection = Snake::Direction::WEST;
break;
default:
break;
}
}

void Snake::Game::start()
Expand Down Expand Up @@ -202,7 +229,7 @@ namespace Snake
this->headPosition.x -= 1;
}
break;
case NONE:
default:
printf("Error at %i %i\n", x, y);
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace Snake
void run();

void update();
void handleKey(const Snake::KeyStruct key);

void generateGrid();

Expand Down

0 comments on commit ac23970

Please sign in to comment.