Skip to content

Commit 2fcef68

Browse files
committed
Added buttons to control road and settlement placement
1 parent 35f28e9 commit 2fcef68

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

include/GameController.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef GAME_CONTROLLER_H
22
#define GAME_CONTROLLER_H
33

4+
#include "Util.h"
5+
46
class GameBoard;
57
class ClickCoordinateEvent;
68
class GameView;
@@ -10,6 +12,10 @@ class GameController {
1012
GameBoard& model;
1113
GameView& view;
1214

15+
bool placingRoads;
16+
bool placingCities;
17+
Coordinate lastCoordClick;
18+
1319
GameController(const GameController& o) : model(o.model), view(o.view) {} //deleted
1420
GameController& operator=(const GameController& o) { return *this; } //deleted
1521
public:

include/GameView.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class ViewButtonColor : public ViewButton<Fn> {
114114
ViewButtonColor(const ViewButtonColor& vb) : ViewElement(vb) {} //deleted
115115
ViewButtonColor& operator=(const ViewButtonColor& vb) { return *this; }
116116
public:
117-
ViewButtonColor(Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) : ViewElement(rect, action), color(color) {}
117+
ViewButtonColor(Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) : ViewButton<Fn>(action, rect), color(color) {}
118118
virtual ~ViewButtonColor() {}
119119

120120
virtual void render() {
@@ -123,10 +123,10 @@ class ViewButtonColor : public ViewButton<Fn> {
123123
auto topLeft = ViewElement::getRect().first;
124124
auto bottomRight = ViewElement::getRect().second;
125125
glBegin(GL_QUADS);
126-
glVertex3f(topLeft.first, topLeft.second);
127-
glVertex3f(bottomRight.first, topLeft.second);
128-
glVertex3f(bottomRight.first, bottomRight.second);
129-
glVertex3f(topLeft.first, bottomRight.second);
126+
glVertex2f(topLeft.first, topLeft.second);
127+
glVertex2f(bottomRight.first, topLeft.second);
128+
glVertex2f(bottomRight.first, bottomRight.second);
129+
glVertex2f(topLeft.first, bottomRight.second);
130130
glEnd();
131131
}
132132
};

src/GameController.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
#include "GameView.h"
77
#include "Renderer.h"
88

9-
GameController::GameController(GameBoard& model, GameView& view) : model(model), view(view) {
9+
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+
std::cout << __PRETTY_FUNCTION__ << std::endl;
14+
return true;
15+
}, {{0, 0}, {0.1, 0.1}}, std::make_tuple(1.f, 0.f, 0.f)));
16+
view.addElement(makeViewButtonColor([&](ScreenCoordinate coord) {
17+
placingRoads = false;
18+
placingCities = true;
19+
std::cout << __PRETTY_FUNCTION__ << std::endl;
20+
return true;
21+
}, {{0, 0.1}, {0.1, 0.2}}, std::make_tuple(0.f, 1.0f, 0.f)));
1022
view.addElement(makeViewButton([this](ScreenCoordinate coord) { this->handleEvent(ClickCoordinateEvent(screenToCoord(coord))); return true; }, {{0, 0}, {1, 1}}));
1123
}
1224

@@ -16,6 +28,7 @@ GameController::~GameController() {
1628

1729
void GameController::handleEvent(const ClickCoordinateEvent& event) {
1830
//std::cout << "user clicked at " << event.getCoordinate().first << ", " << event.getCoordinate().second << std::endl;
31+
/*
1932
if(model.getRoads(event.getCoordinate()).size() > 0) {
2033
model.PlaceSettlement(event.getCoordinate(), *model.getPlayers()[0]);
2134
} else {
@@ -27,6 +40,17 @@ void GameController::handleEvent(const ClickCoordinateEvent& event) {
2740
break;
2841
}
2942
}
43+
}*/
44+
std::cout << placingRoads << " " << placingCities << std::endl;
45+
if(placingRoads) {
46+
if(lastCoordClick.first == -100 && lastCoordClick.second == -100) {
47+
lastCoordClick = event.getCoordinate();
48+
} else {
49+
model.PlaceRoad(lastCoordClick, event.getCoordinate(), *model.getPlayers()[0]);
50+
lastCoordClick = {-100, -100};
51+
}
52+
} else if(placingCities) {
53+
model.PlaceSettlement(event.getCoordinate(), *model.getPlayers()[0]);
3054
}
3155
}
3256

0 commit comments

Comments
 (0)