-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameWorld.cpp
167 lines (146 loc) · 4.04 KB
/
GameWorld.cpp
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
//GameWorld.cpp
//Jordan Baxter
#include <iostream>
#include "GameWorld.h"
#include "Ball.h"
#include "Brick.h"
#include "ofMain.h"
GameWorld::GameWorld() {
this->gamefont.load("Paradox.ttf", ofGetWidth() / 40, true, true);
this->gameState = PLAY;
this->lives = 3;
this->level = 1;
this->score = 0;
}
void GameWorld::fetchLevelLayout(string file1, string file2, string file3) {
cout << "Fetching level layouts. \n";
ofBuffer level = ofBufferFromFile(file1);
cout << level.getText();
vector<string> lines;
for (auto line: level.getLines()) {
lines.push_back(line);
cout << line << "\n";
}
for (int i = 0; i < lines.size(); ++i) {
for (int j = 0; j < lines.at(i).size(); ++j) {
this->levelLayout1[i][j] = lines.at(i).at(j);
cout << levelLayout1[i][j];
}
cout << "\n";
}
lines.clear();
level = ofBufferFromFile(file2);
for (auto line : level.getLines()) {
lines.push_back(line);
}
for (int i = 0; i < lines.size(); ++i) {
for (int j = 0; j < lines.at(i).size(); ++j) {
this->levelLayout2[i][j] = lines.at(i).at(j);
cout << levelLayout2[i][j];
}
cout << "\n";
}
lines.clear();
level = ofBufferFromFile(file3);
for (auto line : level.getLines()) {
lines.push_back(line);
}
for (int i = 0; i < lines.size(); ++i) {
for (int j = 0; j < lines.at(i).size(); ++j) {
this->levelLayout3[i][j] = lines.at(i).at(j);
cout << levelLayout3[i][j];
}
cout << "\n";
}
cout << "Done. \n";
}
void GameWorld::nextLevel() {
this->level += 1;
}
void GameWorld::changeState(enum Game_State gs) {
this->gameState = gs;
}
void GameWorld::draw() {
if (this->gameState == PLAY) {
gamefont.drawString("LEVEL: " + ofToString(this->level), ofGetWidth() * 1 / 10, ofGetHeight() / 20);
gamefont.drawString("LIVES: " + ofToString(this->lives), ofGetWidth() * 3 / 10, ofGetHeight() / 20);
gamefont.drawString("SCORE: " + ofToString(this->score), ofGetWidth() * 5 / 10, ofGetHeight() / 20);
}
else if (this->gameState == WIN) {
string winString = "YOU WIN ! \n CLICK ANYWHERE TO PLAY AGAIN ! \n SCORE: " + ofToString(this->score);
gamefont.drawString(winString, ofGetWidth() / 2 - gamefont.stringWidth(winString) / 2, ofGetHeight() / 2 - gamefont.stringHeight(winString) / 2);
}
else if (this->gameState == LOSE) {
string loseString = "YOU LOSE ! \n CLICK ANYWHERE TO TRY AGAIN. \n SCORE: " + ofToString(this->score);
gamefont.drawString(loseString, ofGetWidth() / 2 - gamefont.stringWidth(loseString) / 2, ofGetHeight() / 2 - gamefont.stringHeight(loseString));
}
}
void GameWorld::resize(Brick* brick, Paddle* paddle, Ball* ball) {
//brick->resize();
//ball->resize();
//paddle->resize();
}
void GameWorld::loseLife() {
this->lives -= 1;
}
enum Game_State GameWorld::getState() {
return this->gameState;
}
char GameWorld::getLayout(int i, int j) {
if (this->level == 1) {
return this->levelLayout1[i][j];
}
else if (this->level == 2) {
return this->levelLayout2[i][j];
}
else if (this->level == 3) {
return this->levelLayout3[i][j];
}
else {
return ' ';
}
}
bool GameWorld::noBricks(vector<Brick*> &bricks) {
bool noBricks = true;
for (int i = 0; i < bricks.size(); ++i) {
if (bricks[i]->getExists()) {
noBricks = false;
break;
}
}
return noBricks;
}
bool GameWorld::noLives() {
return this->lives == 0;
}
void GameWorld::generateBricks(vector<Brick*> &bricks) {
bricks.erase(bricks.begin(), bricks.end());
cout << "Generating Bricks...";
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 12; ++j) {
bricks.push_back(new Brick(ofGetHeight() * ((i + 3) / 25.0), ofGetWidth() * (j / 12.0), this->getLayout(i,j)));
}
}
cout << "Done." << "\n";
}
void GameWorld::generateBalls(vector<Ball*> &balls) {
cout << "Generating balls...";
for (int i = 0; i < 5; ++i) {
balls.push_back(new Ball());
}
cout << "Done \n";
}
void GameWorld::scoreUp(int x) {
this->score += x;
}
int GameWorld::getLevel() {
return this->level;
}
void GameWorld::reset(vector <Brick*> &bricks, Ball* ball) {
this->score = 0;
this->lives = 3;
this->gameState = PLAY;
this->level = 1;
this->generateBricks(bricks);
ball->spawn();
}