Skip to content

Commit 0621d88

Browse files
author
ankit21
committed
Merge branch 'master' into player_turn
Conflicts: src/GameController.cpp
2 parents 7cac269 + 26b1eee commit 0621d88

28 files changed

+1469
-180
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ warsofcatan
2121
*.wocs
2222

2323
documentation/*
24+
/Debug

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>warsofcatan</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export EXECUTABLE := warsofcatan
77
ALLFILES := $(wildcard $(SRC_HOME)/*) $(wildcard $(INCL_HOME)/*)
88
export CXX := g++
99
export LD := g++
10-
export CXXFLAGS := -g -I$(INCL_HOME) -std=c++0x -Wall
11-
export LDFLAGS := -L/usr/local/lib -lSDL2 -lSDL2_ttf -lGL -lGLU
10+
export CXXFLAGS := -g -I$(INCL_HOME) -std=c++0x -I/usr/include/SDL2 -I/usr/local/include/SDL2 -Wall
11+
export LDFLAGS := -L/usr/local/lib -lSDL2 -lSDL2_ttf -lGL -lGLU -Wl,-R/usr/local/lib
1212

1313
.PHONY: all
1414
all: $(EXECUTABLE)
@@ -25,4 +25,4 @@ tests: $(EXECUTABLE)
2525
.PHONY: clean
2626
clean:
2727
rm -f $(EXECUTABLE)
28-
rm -f obj/*.o
28+
rm -f obj/*.o

include/City.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
* Upgraded from a Settlement. Exists on the board, and receives two resources from adjacent tiles when their number is rolled.
88
*/
99
class City : public CornerPiece {
10-
private:
10+
private:
1111

12-
public:
13-
City(GameBoard& board, Coordinate location, Player& owner);
14-
City(City&) = delete;
15-
~City();
16-
City(CornerPiece& sett);
17-
City& operator=(City&) = delete;
18-
19-
virtual void accept(GameVisitor&);
20-
virtual bool operator==(const GamePiece& piece) const;
12+
public:
13+
City(GameBoard& board, Coordinate location, Player& owner);
14+
City(City&) = delete;
15+
~City();
16+
City(CornerPiece& sett);
17+
City& operator=(City&) = delete;
2118

22-
int getResourceModifier();
23-
int getVictoryPoints();
19+
virtual void accept(GameVisitor&);
20+
virtual bool operator==(const GamePiece& piece) const;
21+
22+
int getResourceModifier();
23+
int getVictoryPoints();
2424
};
2525

2626
#endif

include/CornerPiece.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ class CornerPiece : public GamePiece {
2020
Player& getOwner();
2121
const Player& getOwner() const;
2222

23+
virtual void accept(GameVisitor& visitor)=0;
24+
2325
virtual int getResourceModifier();
2426

2527
virtual int getVictoryPoints();
28+
29+
virtual bool operator==(const GamePiece&) const;
2630
};
2731

2832
#endif

include/GameBoard.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "Settlement.h"
1717
#include "tinyxml2.h"
1818
#include "Road.h"
19-
19+
#include "GameDice.h"
2020

2121
class GameVisitor;
2222

@@ -29,6 +29,9 @@ class GameBoard {
2929

3030
std::map<Coordinate, std::unique_ptr<ResourceTile>> resources;
3131

32+
GameDice dice;
33+
34+
3235

3336
std::map<Coordinate, std::vector<std::shared_ptr<Road>>> roads;
3437

@@ -95,13 +98,14 @@ class GameBoard {
9598

9699
void PlaceSettlement(Coordinate location, Player& Owner);
97100
void UpgradeSettlement(Coordinate location);
98-
//void PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
101+
void UpgradeToWonder(Coordinate location);
99102

100103

101104
bool buyRoad(Coordinate start, Coordinate end, Player& Owner);
102105

103106
//void PlaceSettlement(Coordinate location, Player& Owner);
104107
void PlaceCity(Coordinate location, Player& Owner);
108+
void PlaceWonder(Coordinate location, Player& Owner);
105109
bool PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
106110

107111
void accept(GameVisitor& visitor);

include/GameController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
#include "Util.h"
55

6+
#include <array>
7+
68
class GameBoard;
79
class ClickCoordinateEvent;
810
class GameView;
11+
class Player;
912

1013
/**
1114
* Takes interpreted Catan events from the View and calls the appropriate functions on the model to changee the state
@@ -30,6 +33,8 @@ class GameController {
3033
bool handleBoardEvent(ScreenCoordinate);
3134
bool handleRoadButtonEvent(ScreenCoordinate);
3235
bool handleSettlementButtonEvent(ScreenCoordinate);
36+
bool handlePlayerClick(ScreenCoordinate, Player&);
37+
bool handleTradeOffer(ScreenCoordinate, Player& initiating, std::array<int, 5>, Player& receiving, std::array<int, 5>);
3338
};
3439

3540
#endif

include/GameDice.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef GAMEDICE_H
2+
#define GAMEDICE_H
3+
4+
5+
6+
7+
8+
class GameVisitor;
9+
10+
class GameDice {
11+
private:
12+
int first;
13+
int second;
14+
15+
16+
public:
17+
int getFirst();
18+
int getSecond();
19+
bool shown;
20+
void setFirst(int newFirst);
21+
void setSecond(int newSecond);
22+
virtual void accept(GameVisitor& visitor);
23+
24+
25+
26+
};
27+
28+
#endif

include/GameView.h

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
#include <vector>
55
#include <memory>
6+
#include <array>
67

78
#include "SDL2/SDL.h"
89
#include "SDL2/SDL_opengl.h"
910
#include "GL/gl.h"
1011

1112
#include "GameVisitor.h"
13+
#include "Renderer.h"
1214
#include "Util.h"
1315

1416
class GameBoard;
@@ -45,10 +47,10 @@ class GameView {
4547
private:
4648
GameBoard& model;
4749

48-
std::vector<std::unique_ptr<ViewElement>> viewElements;
50+
std::map<int, std::unique_ptr<ViewElement>> viewElements;
4951

50-
GameView(const GameView& o) : model(o.model) {} //deleted
51-
GameView& operator=(const GameView& o) { return *this; } //deleted
52+
GameView(const GameView& o) = delete;
53+
GameView& operator=(const GameView& o) = delete;
5254
public:
5355
GameView(GameBoard&);
5456
~GameView();
@@ -57,6 +59,11 @@ class GameView {
5759
bool acceptInput(SDL_Event& event);
5860

5961
void addElement(std::unique_ptr<ViewElement>);
62+
void addElement(int priority, std::unique_ptr<ViewElement>);
63+
64+
std::unique_ptr<ViewElement> removeElement(int priority);
65+
std::unique_ptr<ViewElement> removeElement(const ViewElement*);
66+
std::unique_ptr<ViewElement> removeElement(const ViewElement&);
6067
};
6168

6269
/**
@@ -66,8 +73,8 @@ class DrawingGameVisitor : public GameVisitor {
6673
private:
6774
GameView& view;
6875

69-
DrawingGameVisitor(const DrawingGameVisitor& o) : view(o.view) {} //deleted
70-
DrawingGameVisitor& operator=(const DrawingGameVisitor& o) { return *this; } //deleted
76+
DrawingGameVisitor(const DrawingGameVisitor& o) = delete;
77+
DrawingGameVisitor& operator=(const DrawingGameVisitor& o) = delete;
7178
public:
7279
DrawingGameVisitor(GameView& view);
7380
~DrawingGameVisitor();
@@ -79,27 +86,26 @@ class DrawingGameVisitor : public GameVisitor {
7986
virtual void visit(Player&);
8087
virtual void visit(ResourceTile&);
8188
virtual void visit(DevelopmentCard&);
89+
virtual void visit(GameDice&);
90+
virtual void visit(Wonder&);
8291
};
8392

8493
/**
8594
* A view element that is invisible and calls a callback function when it is clicked.
8695
*/
87-
template<class Fn>
8896
class ViewButton : public ViewElement {
8997
private:
90-
Fn action;
98+
std::function<bool(ScreenCoordinate)> action;
9199

92-
ViewButton(const ViewButton& vb) : ViewElement(vb) {} //deleted
93-
ViewButton& operator=(const ViewButton&) { return *this; } //deleted
100+
ViewButton(const ViewButton& vb) = delete;
101+
ViewButton& operator=(const ViewButton&) = delete;
94102
protected:
95-
virtual bool clicked(ScreenCoordinate coord) {
96-
return action(coord);
97-
}
103+
virtual bool clicked(ScreenCoordinate coord);
98104
public:
99-
ViewButton(Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
100-
virtual ~ViewButton() {}
105+
ViewButton(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect);
106+
virtual ~ViewButton();
101107

102-
virtual void render() {}
108+
virtual void render();
103109
};
104110

105111
/**
@@ -111,35 +117,23 @@ class ViewButton : public ViewElement {
111117
*/
112118
template<class Fn>
113119
std::unique_ptr<ViewElement> makeViewButton(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
114-
return std::unique_ptr<ViewElement>(new ViewButton<Fn>(fn, rect));
120+
return std::unique_ptr<ViewElement>(new ViewButton(fn, rect));
115121
}
116122

117123
/**
118124
* A view element drawn as a solid color that has a callback function that is called when it is clicked.
119125
*/
120-
template<class Fn>
121-
class ViewButtonColor : public ViewButton<Fn> {
126+
class ViewButtonColor : public ViewButton {
122127
private:
123128
std::tuple<float, float, float> color;
124129

125-
ViewButtonColor(const ViewButtonColor& vb) : ViewElement(vb) {} //deleted
126-
ViewButtonColor& operator=(const ViewButtonColor& vb) { return *this; }
130+
ViewButtonColor(const ViewButtonColor& vb) = delete;
131+
ViewButtonColor& operator=(const ViewButtonColor& vb) = delete;
127132
public:
128-
ViewButtonColor(Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) : ViewButton<Fn>(action, rect), color(color) {}
129-
virtual ~ViewButtonColor() {}
130-
131-
virtual void render() {
132-
glBindTexture(GL_TEXTURE_2D, 0);
133-
glColor3f(std::get<0>(color), std::get<1>(color), std::get<2>(color));
134-
auto topLeft = ViewElement::getRect().first;
135-
auto bottomRight = ViewElement::getRect().second;
136-
glBegin(GL_QUADS);
137-
glVertex2f(topLeft.first, topLeft.second);
138-
glVertex2f(bottomRight.first, topLeft.second);
139-
glVertex2f(bottomRight.first, bottomRight.second);
140-
glVertex2f(topLeft.first, bottomRight.second);
141-
glEnd();
142-
}
133+
ViewButtonColor(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color);
134+
virtual ~ViewButtonColor();
135+
136+
virtual void render();
143137
};
144138

145139
/**
@@ -152,7 +146,60 @@ class ViewButtonColor : public ViewButton<Fn> {
152146
*/
153147
template<class Fn>
154148
std::unique_ptr<ViewElement> makeViewButtonColor(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) {
155-
return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn>(fn, rect, color));
149+
return std::unique_ptr<ViewElement>(new ViewButtonColor(fn, rect, color));
156150
}
157151

158-
#endif
152+
/**
153+
* A view element drawn as some text on the screen that has a callback function when it is clicked.
154+
*/
155+
class ViewButtonText : public ViewButton {
156+
private:
157+
GLuint texture;
158+
159+
ViewButtonText(const ViewButtonText& vb) = delete;
160+
ViewButtonText& operator=(const ViewButtonText& vb) = delete;
161+
public:
162+
ViewButtonText(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text);
163+
virtual ~ViewButtonText();
164+
165+
void setText(const std::string& font, int fontSize, const std::string& text);
166+
167+
virtual void render();
168+
};
169+
170+
/**
171+
* Constructs a ViewButtonText using the same parameters as the ViewButtonText. Exists because template inference exists only
172+
* for functions, not classes.
173+
* @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
174+
* @param rect The location on screen to draw to and receive clicks from.
175+
* @param font The path to the font to use to draw the text.
176+
* @param fontSize The font size of the text.
177+
* @param text The text to render.
178+
*/
179+
template<class Fn>
180+
std::unique_ptr<ViewElement> makeViewButtonText(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text) {
181+
return std::unique_ptr<ViewElement>(new ViewButtonText(fn, rect, font, fontSize, text));
182+
}
183+
184+
class TradingView : public ViewElement {
185+
private:
186+
Player& initiating;
187+
Player& receiving;
188+
189+
ViewButtonText trade;
190+
ViewButtonText cancel;
191+
192+
std::array<int, 5> offer;
193+
194+
TradingView(TradingView& o) = delete;
195+
TradingView& operator=(TradingView& o) = delete;
196+
protected:
197+
virtual bool clicked(ScreenCoordinate coord);
198+
public:
199+
TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> offer);
200+
virtual ~TradingView();
201+
202+
void render();
203+
};
204+
205+
#endif

include/GameVisitor.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Road;
88
class City;
99
class Player;
1010
class DevelopmentCard;
11+
class GameDice;
12+
class Wonder;
1113

1214
/**
1315
* A class to be extended with callbacks to handle the different classes in the model.
@@ -28,6 +30,8 @@ class GameVisitor {
2830
virtual void visit(Player&) = 0;
2931
virtual void visit(ResourceTile&) = 0;
3032
virtual void visit(DevelopmentCard&) = 0;
33+
virtual void visit(GameDice&) = 0;
34+
virtual void visit(Wonder&) = 0;
3135
};
3236

33-
#endif
37+
#endif

0 commit comments

Comments
 (0)