-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
198 lines (173 loc) · 4.22 KB
/
game.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <cmath>
#include <iostream>
#include <sstream>
#include "entities.hpp"
#include "game.hpp"
#include "global.hpp"
using namespace sf;
using namespace std;
const int AREA_SIZE = WIDTH * 2;
const int AREA_DIFF = WIDTH * 1;
const float FILL_STEP = 32;
const float PART1_LENGTH = 500;
const float PART2_LENGTH = 5000;
const float PART3_LENGTH = 10000;
Game::Game()
{
entities.reserve(16);
actors.reserve(16);
Entity *welcomeSign = new Decal(Decal::WELCOME_SIGN);
welcomeSign->setPosition(-200, 100);
entities.push_back(welcomeSign);
Entity *welcomeSign2 = new Decal(Decal::WELCOME_SIGN2);
welcomeSign2->setPosition(200, 100);
entities.push_back(welcomeSign2);
monster = new Monster(this);
start = end = false;
updateGameArea();
}
void Game::update(float deltaTime)
{
for (int i = 0; i < entities.size(); i++) {
if (entities[i]) entities[i]->update(deltaTime);
entities[i]->collide(player);
}
// special entities
if (!end) player.update(deltaTime);
if (start) {
monster->collide(player);
monster->update(deltaTime);
}
// test if it time to update the game area
Vector2f playerPos = player.getPosition();
if ((abs(playerPos.x - areaCenter.x) > AREA_DIFF) || (abs(playerPos.y - areaCenter.y) > AREA_DIFF)) {
updateGameArea();
}
// monster activation
if (!start && (player.travelAmount > PART2_LENGTH || player.travelAmount < -200)) {
begin();
}
}
void Game::draw()
{
for (int i = 0; i < entities.size(); i++) {
entities[i]->draw();
}
// special entities
if (!end) player.draw();
if (start) monster->draw();
const View &view = window->getView();
float originX = view.getCenter().x - WIDTH / 2;
float originY = view.getCenter().y - HEIGHT / 2;
Text text;
text.setFont(defaultFont);
text.setCharacterSize(32);
text.setColor(Color::Black);
ostringstream textStream;
textStream.str(string());
textStream << "Speed: ";
textStream << (int) player.speed / 10;
text.setString(textStream.str());
text.setPosition(originX, originY);
window->draw(text);
textStream.str(string());
textStream << "Distance: ";
textStream << (int) player.travelAmount / 100;
text.setString(textStream.str());
text.setPosition(originX, originY + 40);
window->draw(text);
}
// remove any entities outside the game area
void Game::collect()
{
for (int i = entities.size() - 1; i > 0; i--) {
if (!gameArea.intersects(entities[i]->getBounds())) {
delete entities[i];
entities.erase(entities.begin() + i);
}
}
}
void Game::updateGameArea()
{
cerr << "updateGameArea()" << endl;
Vector2f playerPos = player.getPosition();
FloatRect newArea(playerPos.x - AREA_SIZE, playerPos.y - AREA_SIZE, AREA_SIZE * 2, AREA_SIZE * 2);
for (float x = 0; x < newArea.width; x += FILL_STEP) {
float xx = newArea.left + x;
for (float y = 0; y < newArea.height; y += FILL_STEP) {
float yy = newArea.top + y;
if (!gameArea.contains(xx, yy)) {
fillAt(xx, yy);
}
}
}
gameArea = newArea;
areaCenter = playerPos;
collect();
}
void Game::fillAt(float x, float y)
{
int invDensity;
// difficulty
if (y < PART1_LENGTH) {
return;
} else if (y < PART2_LENGTH) {
invDensity = 40;
} else if (y < PART3_LENGTH) {
invDensity = 20;
} else {
invDensity = 10;
}
if (rand() % invDensity == 1) {
Entity *entity;
switch (rand() % 9) {
case 0:
{
Decal::Type type;
int n = rand() % 100;
if (n < 10) type = Decal::BLOOD;
else if (n < 50) type = Decal::FALL;
else if (n < 80) type = Decal::TRACKS;
else if (n < 90) type = Decal::WARNING_SIGN1;
else if (n < 100) type = Decal::WARNING_SIGN2;
// skip signs
if (y < PART2_LENGTH) {
if (type >= Decal::WARNING_SIGN1) type = Decal::TRACKS;
}
// skip blood
if (y < PART3_LENGTH) {
if (type == Decal::BLOOD) type = Decal::FALL;
}
entity = new Decal(type);
}
break;
case 1:
case 2:
case 3:
entity = new Tree();
break;
case 4:
case 5:
entity = new Rock();
break;
case 6:
entity = new Jump();
break;
case 7:
case 8:
entity = new Dune();
}
entity->setPosition(x + rand() % 10, y + rand() % 10);
entities.push_back(entity);
}
}
void Game::begin()
{
Vector2f pos = player.getPosition();
monster->setPosition(pos.x - 500, pos.y - 500);
start = true;
}
void Game::finish()
{
end = true;
}