|
1 | 1 | #include "GameController.h" |
2 | 2 |
|
3 | 3 | #include <iostream> |
| 4 | +#include <functional> |
4 | 5 |
|
5 | 6 | #include "GameBoard.h" |
6 | 7 | #include "GameView.h" |
7 | 8 | #include "Renderer.h" |
8 | 9 |
|
9 | 10 | GameController::GameController(GameBoard& model, GameView& view) : model(model), view(view), placingRoads(false), placingCities(false) ,lastCoordClick(-100, -100) { |
10 | | - view.addElement(makeViewButtonColor([&](ScreenCoordinate coord) { |
11 | | - placingRoads = true; |
12 | | - placingCities = false; |
13 | | - return true; |
14 | | - }, {{0, 0}, {0.1, 0.1}}, std::make_tuple(1.f, 0.f, 0.f))); |
15 | | - view.addElement(makeViewButtonColor([&](ScreenCoordinate coord) { |
16 | | - placingRoads = false; |
17 | | - placingCities = true; |
18 | | - return true; |
19 | | - }, {{0, 0.1}, {0.1, 0.2}}, std::make_tuple(0.f, 1.0f, 0.f))); |
20 | | - view.addElement(makeViewButton([this](ScreenCoordinate coord) { this->handleEvent(ClickCoordinateEvent(screenToCoord(coord))); return true; }, {{0, 0}, {1, 1}})); |
| 11 | + using namespace std::placeholders; |
| 12 | + |
| 13 | + view.addElement(makeViewButtonColor(std::bind(&GameController::handleRoadButtonEvent, this, _1), {{0, 0}, {0.1, 0.1}}, std::make_tuple(1.f, 0.f, 0.f))); |
| 14 | + view.addElement(makeViewButtonColor(std::bind(&GameController::handleSettlementButtonEvent, this, _1), {{0, 0.1}, {0.1, 0.2}}, std::make_tuple(0.f, 1.0f, 0.f))); |
| 15 | + view.addElement(makeViewButton(std::bind(&GameController::handleBoardEvent, this, _1), {{0, 0}, {1, 1}})); |
21 | 16 | } |
22 | 17 |
|
23 | 18 | GameController::~GameController() { |
24 | 19 |
|
25 | 20 | } |
26 | 21 |
|
27 | | -void GameController::handleEvent(const ClickCoordinateEvent& event) { |
28 | | - //std::cout << "user clicked at " << event.getCoordinate().first << ", " << event.getCoordinate().second << std::endl; |
29 | | - /* |
30 | | - if(model.getRoads(event.getCoordinate()).size() > 0) { |
31 | | - model.PlaceSettlement(event.getCoordinate(), *model.getPlayers()[0]); |
32 | | - } else { |
33 | | - static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)}; |
34 | | - for(auto& adjacent : adjacentCoordDiffs) { |
35 | | - Coordinate start {event.getCoordinate().first + adjacent.first, event.getCoordinate().second + adjacent.second}; |
36 | | - if(model.getRoads(start).size() > 0) { |
37 | | - model.PlaceRoad(start, event.getCoordinate(), *model.getPlayers()[0]); |
38 | | - break; |
39 | | - } |
40 | | - } |
41 | | - }*/ |
| 22 | +bool GameController::handleBoardEvent(ScreenCoordinate screenCoord) { |
| 23 | + auto coord = screenToCoord(screenCoord); |
42 | 24 | if(placingRoads) { |
43 | 25 | if(lastCoordClick.first == -100 && lastCoordClick.second == -100) { |
44 | | - lastCoordClick = event.getCoordinate(); |
| 26 | + lastCoordClick = coord; |
45 | 27 | } else { |
46 | | - model.PlaceRoad(lastCoordClick, event.getCoordinate(), *model.getPlayers()[0]); |
| 28 | + model.PlaceRoad(lastCoordClick, coord, *model.getPlayers()[0]); |
47 | 29 | lastCoordClick = {-100, -100}; |
48 | 30 | } |
49 | 31 | } else if(placingCities) { |
50 | | - model.PlaceSettlement(event.getCoordinate(), *model.getPlayers()[0]); |
| 32 | + model.PlaceSettlement(coord, *model.getPlayers()[0]); |
51 | 33 | } |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +bool GameController::handleRoadButtonEvent(ScreenCoordinate coord) { |
| 38 | + placingRoads = true; |
| 39 | + placingCities = false; |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +bool GameController::handleSettlementButtonEvent(ScreenCoordinate coord) { |
| 44 | + placingRoads = false; |
| 45 | + placingCities = true; |
| 46 | + return true; |
52 | 47 | } |
53 | 48 |
|
0 commit comments