Created
March 9, 2022 11:26
-
-
Save yknishidate/a3a184f10ed7f493e1940bc9f55afa40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <array> | |
class Particle | |
{ | |
public: | |
Particle() : m_frameLeft(0) {} | |
void init(double x, double y, int lifeTime) | |
{ | |
m_x = x; | |
m_y = y; | |
m_frameLeft = lifeTime; | |
} | |
bool inUse() const | |
{ | |
return m_frameLeft > 0; | |
} | |
void animate() | |
{ | |
if (!inUse()) | |
{ | |
return; | |
} | |
m_frameLeft--; | |
m_x += 1.0; | |
m_y += 1.0; | |
} | |
private: | |
int m_frameLeft; | |
double m_x; | |
double m_y; | |
}; | |
class ParticlePool | |
{ | |
public: | |
void create(double x, double y, int lifeTime) | |
{ | |
for (auto &particle : m_particles) | |
{ | |
if (!particle.inUse()) | |
{ | |
particle.init(x, y, lifeTime); | |
return; | |
} | |
} | |
} | |
void animate() | |
{ | |
for (auto &particle : m_particles) | |
{ | |
particle.animate(); | |
} | |
} | |
void showMemory() | |
{ | |
std::cout << "|"; | |
for (auto &particle : m_particles) | |
{ | |
if (particle.inUse()) | |
{ | |
std::cout << "*"; | |
} | |
else | |
{ | |
std::cout << " "; | |
} | |
} | |
std::cout << "|" << std::endl; | |
} | |
private: | |
static const int POOL_SIZE = 25; | |
std::array<Particle, POOL_SIZE> m_particles; | |
}; | |
int main() | |
{ | |
ParticlePool pool; | |
while (true) | |
{ | |
pool.create(0, 0, 25); | |
pool.animate(); | |
pool.showMemory(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment