-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossRoadGame.h
176 lines (143 loc) · 4.13 KB
/
CrossRoadGame.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <vector>
#include <stdlib.h>
#include <Windows.h>
#include <glut.h>
#include <string>
#include <iostream>
#include <math.h>
using namespace std;
using std::string;
/* struct definitions for holding drawing data */
typedef struct { // to hold x and y coordinates of agent,car,truck and coin
GLint x;
GLint y;
} Point2D;
typedef struct { // to hold color of drawing components
GLint r;
GLint g;
GLint b;
} Color;
typedef struct { // to define cars
Point2D start;
Point2D end;
Color color;
int velocity;
int lineNumber;
} Car;
typedef struct { // to define trucks
Point2D start;
Point2D end;
Color color;
int velocity;
int lineNumber;
} Truck;
typedef struct { // to define lanes
int lineNumber;
char direction;
Point2D start;
Point2D end;
Color color;
} Lane;
typedef struct { // to define sidewalks
Point2D start;
Point2D end;
Color color;
} SideWalk;
typedef struct { // to define agent
Point2D leftVertex;
Point2D rightVertex;
Point2D upVertex;
char direction; // U for UP and D for DOWN
Color color;
} Agent;
typedef struct { // to define coins
GLint radius;
Point2D center;
Color color;
} Coin;
typedef struct { // for collision detection
GLint x;
GLint y;
GLint width;
GLint height;
} Rect;
/* globals */
GLsizei width = 520, height = 700; /* initial window size */
const double PI = 3.141592653589793238463;
int score = 0;
int time = 0;
/* variables for holding game states s*/
GLboolean isPaused = false;
GLboolean isGameOver = false;
GLboolean isGameWon = false;
GLboolean isOneStepMode = false;
/* periods for timer functions */
GLint randomVehicleGeneratorPeriod = 100;
GLint updateVehiclePeriod = 20;
GLint randomCoinGeneratorPeriod = 1000;
/* constants for drawing components' size*/
const GLint SIDEWALK_WIDTH = 40;
const GLint ROAD_WIDTH = 80;
const GLint LANE_LENGTH = 20;
const GLint GAP_BETWEEN_LANES_HORIZONTALLY = 15;
const GLint GAP_BETWEEN_LANES_VERTICALLY = 20;
const GLint SCOREBOARD_SIZE = 64;
const GLint gameWindowHeight = height - SCOREBOARD_SIZE;
const GLint AGENT_WIDTH = 10;
const GLint AGENT_HEIGHT = 20;
const GLint CAR_HALF_SIZE = (GAP_BETWEEN_LANES_VERTICALLY - GAP_BETWEEN_LANES_VERTICALLY / 5) / 2;
const GLint TRUCK_HALF_SIZE = (GAP_BETWEEN_LANES_VERTICALLY - GAP_BETWEEN_LANES_VERTICALLY / 5) / 2;
const GLint NUMBER_OF_SIDEWALKS = 6;
const GLint NUMBER_OF_ROADS = 5;
const GLint NUMBER_OF_LINES_PER_ROAD = 4;
const GLint TOTAL_NUMBER_OF_LINES = NUMBER_OF_ROADS * NUMBER_OF_LINES_PER_ROAD;
const GLint NUMBER_OF_LANES_PER_LINE = 15;
const GLint GAP_FROM_WINDOW = 5;
/* vector definitions for keeping multiple objects of same type */
vector<SideWalk> sideWalks; // holds sidewalks
vector<Lane> lanes; // holds lanes
vector<Car> carVector; // holds generated cars
vector<Truck> truckVector; // holds generated trucks
vector<Coin> coinVector; // holds generated coins
vector<int> keyboardHistory; // holds keyboard history for taking last key input when program is in the one step mode(right click)
Agent agent;
/* function prototypes */
void fillSideWalksVector();
void fillLanesVector();
vector<Car> getCarsFromGivenLineNumber(int lineNumber);
vector<Truck> getTrucksFromGivenLineNumber(int lineNumber);
vector<Lane> getLanesFromGivenLineNumber(int lineNumber);
int getHeightOfGivenLineNumber(int lineNumber);
char getDirectionOfLine(int lineNumber);
GLboolean isThereAnyCarOrTruckInThatLineEdges(GLint y, GLint lineNumber);
void randomVehicleGenerator(int id);
void randomCoinGenerator(int id);
void updateVehicleLocation(int id);
void agentInit();
void gameOver();
void gameRestart();
void turnAgentDown();
void turnAgentUp();
void agentMoveUp();
void agentMoveDown();
void agentMoveLeft();
void agentMoveRight();
void myReshape(GLsizei w, GLsizei h);
void myinit(void);
void myKeyboard(unsigned char key, int x, int y);
void myKeyboardSpecial(int key, int x, int y);
void myMouse(int btn, int state, int x, int y);
void myDisplay(void);
void drawSidewalks();
void drawLanes();
void drawCars();
void drawTrucks();
void drawAgent();
void drawCoins();
void drawScoreBoard();
void checkCollisions();
int getLineNumberOfAgent();
void drawGameOver();
void timeCounter(int id);
void drawWonTheGame();
void gameInfo();