Skip to content

Commit

Permalink
Rework Game initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Oct 7, 2024
1 parent 8e1606a commit 31a14a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
#include "Tank.h"
#include "Utils.h"

Game::Game()
Game::Game(std::list<Tank>& tanks, Map& map)
: status_{{Config::getInstance().getBoardWidth(), 0}},
randomGenerator_{Config::getRandomSeed()},
map_{Config::getInstance().getTileCount()}
tanks_{tanks},
map_{map}
{
}

void Game::init(std::iostream& level) { tanks_ = map_.loadMap(level); }

std::pair<bool, Direction> Game::inputActionsToDirection(
const std::set<InputAction>& actions)
{
Expand Down
8 changes: 3 additions & 5 deletions src/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ enum class Level : char;
class Game
{
public:
Game();

void init(std::iostream& level);
Game(std::list<Tank>& tanks, Map& map);

void moveBullets();

Expand Down Expand Up @@ -53,7 +51,7 @@ class Game
std::uniform_int_distribution<> distribution_{
std::uniform_int_distribution<>(0, 7)};

std::list<Tank> tanks_;
Map map_;
std::list<Tank>& tanks_;
Map& map_;
std::list<Bullet> bullets_;
};
14 changes: 9 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdlib>
#include <fstream>

#include "Config.h"
#include "Game.h"
#include "InputAction.h"
#include "Menu.h"
Expand Down Expand Up @@ -69,13 +70,16 @@ int main()
if (choice == UserChoice::EXIT)
return EXIT_SUCCESS;

Game game;
const Level level{menu.choiceToLevel(choice)};
if (auto [ok, levelContent]{Resources::getLevel(level)}; ok)
game.init(levelContent);
else
auto [ok, level]{Resources::getLevel(menu.choiceToLevel(choice))};
if (!ok)
return EXIT_FAILURE;

Map map{Config::getInstance().getTileCount()};
std::list<Tank> tanks{map.loadMap(level)};

Game game{tanks, map};
exit = play(std::move(game), screen, input);
}

return EXIT_SUCCESS;
}

0 comments on commit 31a14a2

Please sign in to comment.