Skip to content

Commit 159c5a6

Browse files
committed
Merge branch 'master' into dice_operations
2 parents 30e4a89 + d6429b2 commit 159c5a6

File tree

9 files changed

+106
-10
lines changed

9 files changed

+106
-10
lines changed

include/GameBoard.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class GameBoard {
6666

6767

6868
const std::shared_ptr<Road> getRoad(Coordinate start, Coordinate end) const;
69-
69+
const std::vector<std::shared_ptr<Road>>& getRoads(Coordinate loc) const;
7070

7171
int FindLongestRoad(const Player & owner) const;
7272

@@ -90,6 +90,8 @@ class GameBoard {
9090
void accept(GameVisitor& visitor);
9191

9292
bool operator==(const GameBoard& other) const;
93+
94+
const std::vector<std::unique_ptr<Player>>& getPlayers() const;
9395

9496
bool testRollChecking(int* rolls);
9597

include/GameController.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
#define GAME_CONTROLLER_H
33

44
class GameBoard;
5+
class ClickCoordinateEvent;
56

67
class GameController {
78
private:
8-
GameBoard& board;
9+
GameBoard& model;
910

10-
GameController(const GameController& o) : board(o.board) {} //deleted
11+
GameController(const GameController& o) : model(o.model) {} //deleted
1112
GameController& operator=(const GameController& o) { return *this; } //deleted
1213
public:
1314
GameController(GameBoard&);
1415
~GameController();
16+
17+
void handleEvent(const ClickCoordinateEvent&);
1518
};
1619

1720
#endif

include/GameView.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "SDL2/SDL.h"
55

66
#include "GameVisitor.h"
7+
#include "Util.h"
78

89
class GameBoard;
910
class GameController;
@@ -42,4 +43,16 @@ class DrawingGameVisitor : public GameVisitor {
4243
virtual void visit(DevelopmentCard&);
4344
};
4445

46+
class ClickCoordinateEvent {
47+
private:
48+
Coordinate clicked;
49+
public:
50+
ClickCoordinateEvent(const Coordinate& clicked);
51+
ClickCoordinateEvent(const ClickCoordinateEvent&);
52+
~ClickCoordinateEvent();
53+
ClickCoordinateEvent& operator=(const ClickCoordinateEvent&);
54+
55+
Coordinate getCoordinate() const;
56+
};
57+
4558
#endif

include/Renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void renderBoard(const GameBoard& board, const Player& perspective);
1313
GLuint loadImageAsTexture(const std::string& name);
1414

1515
std::pair<float, float> coordToScreen(const Coordinate& coord);
16+
Coordinate screenToCoord(const std::pair<float, float>&);
1617

1718
void vertexPair(const Coordinate& coord);
1819

src/GameBoard.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ bool GameBoard::roadExists(Coordinate start, Coordinate end) const {
288288
return bool(getRoad(start, end)); // shared_ptr can convert to bool
289289
}
290290

291-
292291
/**
293292
* Checks to make sure the road being placed at a valid point according to the rules
294293
*/
@@ -397,6 +396,14 @@ const std::shared_ptr<Road> GameBoard::getRoad(Coordinate start, Coordinate end)
397396
return NULL;
398397
}
399398

399+
const std::vector<std::shared_ptr<Road>>& GameBoard::getRoads(Coordinate coord) const {
400+
static const std::vector<std::shared_ptr<Road>> empty;
401+
if(roads.find(coord) != roads.end()) {
402+
return roads.find(coord)->second;
403+
}
404+
return empty;
405+
}
406+
400407
/**
401408
* Parent function for the find longest road traversal. Note that longest path is NP-Hard, so there is no simple algorithm for this.
402409
*/
@@ -586,3 +593,7 @@ bool GameBoard::isValidBoard() const {
586593
}
587594
return true;
588595
}
596+
597+
const std::vector<std::unique_ptr<Player>>& GameBoard::getPlayers() const {
598+
return players;
599+
}

src/GameController.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
#include "GameController.h"
22

3+
#include <iostream>
4+
35
#include "GameBoard.h"
6+
#include "GameView.h"
47

5-
GameController::GameController(GameBoard& board) : board(board) {
8+
GameController::GameController(GameBoard& model) : model(model) {
69

710
}
811

912
GameController::~GameController() {
1013

1114
}
1215

16+
void GameController::handleEvent(const ClickCoordinateEvent& event) {
17+
//std::cout << "user clicked at " << event.getCoordinate().first << ", " << event.getCoordinate().second << std::endl;
18+
if(model.getRoads(event.getCoordinate()).size() > 0) {
19+
model.PlaceSettlement(event.getCoordinate(), *model.getPlayers()[0]);
20+
} else {
21+
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
22+
for(auto& adjacent : adjacentCoordDiffs) {
23+
Coordinate start {event.getCoordinate().first + adjacent.first, event.getCoordinate().second + adjacent.second};
24+
if(model.getRoads(start).size() > 0) {
25+
model.PlaceRoad(start, event.getCoordinate(), *model.getPlayers()[0]);
26+
break;
27+
}
28+
}
29+
}
30+
}
31+

src/GameView.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "GameBoard.h"
99
#include "GameController.h"
1010
#include "Renderer.h"
11+
#include "City.h"
1112

1213
using std::make_pair;
1314
using std::pair;
@@ -34,6 +35,8 @@ void GameView::render() {
3435
bool GameView::acceptInput(SDL_Event& event) {
3536
if(event.type == SDL_QUIT) {
3637
return false;
38+
} else if(event.type == SDL_MOUSEBUTTONUP) {
39+
controller.handleEvent(ClickCoordinateEvent(screenToCoord({(float) event.button.x / 900.f, 1.f - (float) event.button.y / 800.f})));
3740
}
3841
return true;
3942
}
@@ -98,7 +101,18 @@ void DrawingGameVisitor::visit(Settlement& settlement) {
98101
}
99102

100103
void DrawingGameVisitor::visit(City& city) {
104+
static const auto cityRadius = 0.03;
101105

106+
auto centerScreenPos = coordToScreen(city.getLocation());
107+
108+
glBindTexture(GL_TEXTURE_2D, 0);
109+
glColor3d(0., 0., 0.);
110+
glBegin(GL_QUADS);
111+
glVertex2d(centerScreenPos.first + cityRadius, centerScreenPos.second + cityRadius);
112+
glVertex2d(centerScreenPos.first + cityRadius, centerScreenPos.second - cityRadius);
113+
glVertex2d(centerScreenPos.first - cityRadius, centerScreenPos.second - cityRadius);
114+
glVertex2d(centerScreenPos.first - cityRadius, centerScreenPos.second + cityRadius);
115+
glEnd();
102116
}
103117

104118
void DrawingGameVisitor::visit(Player& player) {
@@ -180,3 +194,24 @@ void DrawingGameVisitor::visit(ResourceTile& tile) {
180194
void DrawingGameVisitor::visit(DevelopmentCard& card) {
181195

182196
}
197+
198+
ClickCoordinateEvent::ClickCoordinateEvent(const Coordinate& clicked) : clicked(clicked) {
199+
200+
}
201+
202+
ClickCoordinateEvent::ClickCoordinateEvent(const ClickCoordinateEvent& event) : clicked(event.clicked) {
203+
204+
}
205+
206+
ClickCoordinateEvent::~ClickCoordinateEvent() {
207+
208+
}
209+
210+
ClickCoordinateEvent& ClickCoordinateEvent::operator=(const ClickCoordinateEvent& event) {
211+
clicked = event.clicked;
212+
return *this;
213+
}
214+
215+
Coordinate ClickCoordinateEvent::getCoordinate() const {
216+
return clicked;
217+
}

src/Renderer.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ pair<float, float> coordToScreen(const Coordinate& coord) {
4747
using std::sin;
4848
using std::cos;
4949
// TODO not magic numbers
50-
float scale = 0.1f;
51-
float angle = M_PI / 3.f;
52-
float x = .5f + (scale * coord.first) + (scale * coord.second) * cos(angle);
53-
float y = .5f + (scale * coord.second) * sin(angle);
54-
return std::make_pair(x - 0.25f, y - 0.4f);
50+
static const float scale = 0.1f;
51+
static const float angle = M_PI / 3.f;
52+
float x = .25f + (scale * coord.first) + ((scale * coord.second) * cos(angle));
53+
float y = .1f + (scale * coord.second) * sin(angle);
54+
return std::make_pair(x, y);
55+
}
56+
57+
Coordinate screenToCoord(const pair<float, float>& screen) {
58+
static const float scale = 0.1;
59+
static const float angle = M_PI / 3.f;
60+
Coordinate ret;
61+
float y_approx = (screen.second - 0.1f) / std::sin(angle) / scale;
62+
ret.second = std::round(y_approx);
63+
ret.first = std::round((screen.first - 0.2f) / scale - (screen.second - 0.1f) / scale / std::sin(angle) * std::cos(angle)) - 1;
64+
return ret;
5565
}
5666

5767
void vertexPair(const Coordinate& coord) {

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ int main(int argc, char *argv[]) {
7575
model.PlaceRoad(Coordinate{0, 0}, Coordinate{1, 0}, firstPlayer);
7676
model.PlaceRoad(Coordinate{1, 0}, Coordinate{1, 1}, firstPlayer);
7777
model.PlaceRoad(Coordinate{1, 1}, Coordinate{0, 2}, firstPlayer);
78+
model.PlaceSettlement(Coordinate{0, 2}, firstPlayer);
79+
model.UpgradeSettlement(Coordinate{0, 2});
7880

7981
bool running = true;
8082
while(running) {

0 commit comments

Comments
 (0)