-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunk.cpp
213 lines (192 loc) · 4.67 KB
/
Chunk.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "Chunk.h"
Chunk::Chunk(unsigned int e_type, Cblocklist &e_list) : m_blocklist(e_list)
{
m_map_w = MAP_W;
m_map_h = MAP_H;
unsigned int x, y;
for (x = 0; x < m_map_w; x++)
{
for (y = 0; y < m_map_h; y++)
{
Cblock TempBlock;
TempBlock = m_blocklist.GetBlock(e_type);
// On alloue dynamiquement des portions non contigues de memoire
// pour contenir les blocks.
// On range des pointeurs vers les blocks declarés dans un tableau.
m_CblockTable[x][y] = new Cblock(TempBlock);
// m_CblockTable est un tableau bidimensionnels de pointeurs vers
// des objets Cblock.
}
}
}
// Destructeur
Chunk::~Chunk()
{
unsigned int x, y;
for (x = 0; x < m_map_w; x++)
{
for (y = 0; y < m_map_h; y++)
{
// On libère l'éspace aloué dynamiquement dans le constructeur.
delete m_CblockTable[x][y];
}
}
}
//// Methodes
// Getters
Cblock* Chunk::GetBlock(unsigned int x, unsigned int y)
{
return m_CblockTable[x][y];
}
// Getters speciaux
unsigned int Chunk::getW()
{
return m_map_w;
}
unsigned int Chunk::getH()
{
return m_map_h;
}
// Autres methodes
void Chunk::randomize()
{
srand((unsigned)time(0));
unsigned int x, y;
for (x = 0; x < m_map_w; x++)
{
for (y = 0; y < m_map_h; y++)
{
// int randomIndex = rand()%3 ;
unsigned int randomIndex = Randomizer::Random(1, 4);
m_CblockTable[x][y]->copyFrom(*m_blocklist[randomIndex]);
}
}
}
void Chunk::sequence()
{
unsigned int x, y;
for (x = 0; x < m_map_w; x++)
{
int variance = randStrate(10,80,10);
if (x != 0)
{
m_strate.push_back(variance += m_strate[x - 1]);
}
else
{
m_strate.push_back(variance);
}
}
for (x = 0; x < m_map_w; x++)
{
for (y = 90; y < m_map_h; y++)
{
if ((y + m_strate[x]) < m_map_h)
{
m_CblockTable[x][y + m_strate[x]]->copyFrom(*m_blocklist[1]);
if ((m_CblockTable[x][y + m_strate[x]-1]->getType() == 0))
{
m_CblockTable[x][y + m_strate[x]]->setGrassy(true);
}
}
}
for (y = 125; y < m_map_h; y++)
{
if ((y + m_strate[x]) < m_map_h)
{
m_CblockTable[x][y + m_strate[x]]->copyFrom(*m_blocklist[2]);
}
}
for (y = 150; y < m_map_h; y++)
{
if ((y + m_strate[x]) < m_map_h)
{
m_CblockTable[x][y + m_strate[x]]->copyFrom(*m_blocklist[3]);
}
}
}
}
void Chunk::fillWith(unsigned int e_type)
{
unsigned int x, y;
for (x = 0; x < m_map_w; x++)
{
for (y = 0; y < m_map_h; y++)
{
m_CblockTable[x][y]->copyFrom(*m_blocklist[e_type]);
}
}
}
void Chunk::rockBottom(unsigned int e_type)
{
unsigned int x;
for (x = 0; x < m_map_w; x++)
{
m_CblockTable[x][m_map_h-1]->copyFrom(*m_blocklist[e_type]);
}
}
// Affectation des blocs
bool Chunk::affectBlockSpAt(int x, int y)
{
bool value = true;
if (x > MAP_W || y > MAP_H)
value = false;
else
{
m_CblockTable[x][y]->spDecrease();
m_CblockTable[x][y]->update();
}
return value;
}
bool Chunk::affectBlockSpAt(float x, float y)
{
bool value = true;
x = x/BLOCK_W;
y = y/BLOCK_H;
if (x > MAP_W || y > MAP_H)
value = false;
else
{
m_CblockTable[(int) x][(int) y]->spDecrease();
m_CblockTable[(int) x][(int) y]->update();
}
return value;
}
bool Chunk::affectBlockSpAt(float x, float y, int val)
{
bool value = true;
x = x/BLOCK_W;
y = y/BLOCK_H;
if (x > MAP_W || y > MAP_H)
value = false;
else
{
m_CblockTable[(int) ceil(x)][(int) ceil(y)]->setSp(val);
m_CblockTable[(int) x][(int) y]->update();
}
return value;
}
void Chunk::changeBlockTypeAt(int x, int y, unsigned int e_type)
{
if (x > MAP_W || y > MAP_H)
return;
else
{
m_CblockTable[x][y]->setType(e_type);
m_CblockTable[x][y]->setSp(1);
m_CblockTable[x][y]->update();
}
}
void Chunk::changeBlockTypeAt(float x, int y, unsigned int e_type)
{
x = x/BLOCK_W;
y = y/BLOCK_H;
if (x > MAP_W || y > MAP_H)
return;
else
{
m_CblockTable[(int) x][(int) y]->setType(e_type);
m_CblockTable[(int) x][(int) y]->setSp(1);
m_CblockTable[(int) x][(int) y]->update();
}
}