|
|
|
@ -6,13 +6,21 @@ namespace pengine {
@@ -6,13 +6,21 @@ namespace pengine {
|
|
|
|
|
Uint8 part_colours[][3] = { |
|
|
|
|
{255,255,255}, // BASIC
|
|
|
|
|
{252,250,148}, // SAND
|
|
|
|
|
{0,0,255} // SAND
|
|
|
|
|
{70,90,255} // WATER
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
std::string part_names[] = { |
|
|
|
|
"default", |
|
|
|
|
"Sand" |
|
|
|
|
"Water", |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Particle *get_new_particle(ParticleID p) { |
|
|
|
|
switch (p) { |
|
|
|
|
case ParticleID::SAND: |
|
|
|
|
return new Sand(); |
|
|
|
|
case ParticleID::WATER: |
|
|
|
|
return new Water(); |
|
|
|
|
case ParticleID::BASIC: |
|
|
|
|
return new BasicParticle(); |
|
|
|
|
} |
|
|
|
@ -59,7 +67,7 @@ namespace pengine {
@@ -59,7 +67,7 @@ namespace pengine {
|
|
|
|
|
xsize = xs; |
|
|
|
|
ysize = ys; |
|
|
|
|
|
|
|
|
|
grid = new Particle *[xs * ys] {};
|
|
|
|
|
grid = new Particle *[xs * ys] {0};
|
|
|
|
|
pixels = new Uint32[xs * ys];
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -77,14 +85,19 @@ namespace pengine {
@@ -77,14 +85,19 @@ namespace pengine {
|
|
|
|
|
{ |
|
|
|
|
static bool updated = false; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < xsize * ysize; i++) { |
|
|
|
|
if (grid[i] != nullptr) { |
|
|
|
|
if (grid[i]->updated != updated) { |
|
|
|
|
grid[i]->updated = updated; |
|
|
|
|
grid[i]->update(this, i % xsize, i / xsize); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < xsize; i++) { |
|
|
|
|
for (int j = 0; j < ysize; j++) { |
|
|
|
|
int p = j * xsize + i; |
|
|
|
|
if (grid[p] != nullptr) { |
|
|
|
|
if (grid[p]->updated != updated) { |
|
|
|
|
grid[p]->updated = updated; |
|
|
|
|
grid[p]->update(this, i, j); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updated = !updated; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -155,16 +168,60 @@ namespace pengine {
@@ -155,16 +168,60 @@ namespace pengine {
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Grid::swap_particle(int xa, int ya, int xb, int yb) { |
|
|
|
|
|
|
|
|
|
if (!inside_grid(xb, yb)) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!inside_grid(xa, ya)) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((get_particle(xa,ya) != nullptr) || (get_particle(xb,yb) != nullptr)) { |
|
|
|
|
Particle *temp = grid[xb + yb * xsize]; |
|
|
|
|
grid[xb + yb * xsize] = grid[xa + ya * xsize]; |
|
|
|
|
grid[xa + ya * xsize] = temp; |
|
|
|
|
return true; |
|
|
|
|
}
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Sand::update(Grid * const g, int x, int y) { |
|
|
|
|
if (g->move_particle(x, y, x, y+1)) { |
|
|
|
|
Particle *p; |
|
|
|
|
p = g->get_particle(x, y+1); |
|
|
|
|
if (p) { |
|
|
|
|
if (p->density < density) { |
|
|
|
|
g->swap_particle(x, y, x, y+1); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
g->swap_particle(x, y, x, y+1); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (g->move_particle(x, y, x-1, y+1)) { |
|
|
|
|
|
|
|
|
|
p = g->get_particle(x-1, y+1); |
|
|
|
|
if (p) { |
|
|
|
|
if (p->density < density) { |
|
|
|
|
g->swap_particle(x, y, x-1, y+1); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
g->swap_particle(x, y, x-1, y+1); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (g->move_particle(x, y, x+1, y+1)) { |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = g->get_particle(x+1, y+1); |
|
|
|
|
if (p) { |
|
|
|
|
if (p->density < density) { |
|
|
|
|
g->swap_particle(x, y, x-1, y+1); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
g->swap_particle(x, y, x+1, y+1); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BasicParticle::BasicParticle() : Particle(ParticleID::BASIC) {} |
|
|
|
@ -178,6 +235,27 @@ namespace pengine {
@@ -178,6 +235,27 @@ namespace pengine {
|
|
|
|
|
return PixelColour {part_colours[((int)this->id)]}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string BasicParticle::get_name() { |
|
|
|
|
return part_names[((int)this->id)]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Water::update(Grid * const g, int x, int y) { |
|
|
|
|
if (g->move_particle(x, y, x, y+1)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (g->move_particle(x, y, x-1, y+1)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (g->move_particle(x, y, x+1, y+1)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (g->move_particle(x, y, x+1, y)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (g->move_particle(x, y, x-1, y)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|