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
| #pragma once
#include "map.hpp"
#include "components.hpp"
#include "systems.hpp"
#include "random.hpp"
using random = effolkronium::random_static;
class particle
{
using index_t = std::size_t;
private:
render& rnd_s;
dynamics& dyn_s;
ecs::map<index_t, Life> life_;
std::vector<index_t> removed_;
std::size_t number_;
index_t generate_id()
{
static index_t id{0};
return id++;
}
public:
particle(render & rs, dynamics & ds): rnd_s{rs}, dyn_s{ds}, number_{0} {}
void create()
{
create(generate_id());
}
void create(index_t const & id)
{
dyn_s.add(id, Position{800.0f, 800.0f});
dyn_s.add(id, random_vel(-250.0f, 250.0f));
rnd_s.add(id, Shape{sf::CircleShape(1.0f), sf::Color(222.0f, 193.0f, 30.0f), 1.0f, true});
life_.push(id, random::get(1.5f, 3.5f));
}
Velocity random_vel(float const & min, float const & max)
{
float x = random::get(-250.0f, 250.0f);
float y = random::get(-250.0f, .0f);
return Velocity{x, y};
}
std::size_t get_number()
{
return life_.size();
}
void destroy(index_t const & id)
{
dyn_s.destroy(id);
rnd_s.destroy(id);
life_.pop(id);
}
void clean()
{
for(auto i : removed_)
{
destroy(i);
}
}
void update(float const & tf)
{
if(life_.size() < 64000)
{
for(int i{0}; i != 50; ++i)
{
this->create();
}
}
for(auto it{life_.begin()}; it != life_.end(); ++it)
{
it->ttl_ -= tf;
if(it->ttl_ <= .0f)
{
removed_.push_back(it->id_);
}
}
//clean();
for(auto i : removed_)
{
destroy(i);
this->create(i);
}
removed_.clear();
}
}; |
Partager