-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartSystem.cpp
90 lines (77 loc) · 2.74 KB
/
PartSystem.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
#include "PartSystem.h"
PartSystem::PartSystem(sf::Sprite sprite, float dir, float dir_offset, float speed, float speed_offset, float acc, float acc_offset, float size, float size_offset, unsigned int life, unsigned int life_offset, usi gen_amount, usi gen_alarm, sf::FloatRect generator)
{
this->sprite = sprite;
this->sprite.setOrigin(sprite.getGlobalBounds().width / 2, sprite.getGlobalBounds().height / 2);
this->dir = dir;
this->dir_offset = dir_offset;
this->speed = speed;
this->speed_offset = speed_offset;
this->acc = acc;
this->acc_offset = acc_offset;
this->size = size;
this->size_offset = size_offset;
this->life = life;
this->life_offset = life_offset;
this->gen_amount = gen_amount;
this->gen_alarm = gen_alarm;
this->generator = generator;
this->alarm = 0;
alarm = 0;
}
PartSystem::PartSystem(sf::Sprite sprite, float dir, float dir_offset, float speed, float speed_offset, float size, float size_offset, unsigned int life, unsigned int life_offset, usi gen_amount, usi gen_alarm, sf::FloatRect generator)
:PartSystem(sprite, dir, dir_offset, speed, speed_offset, 0, 0, size, size_offset, life, life_offset, gen_amount, gen_alarm, generator)
{}
PartSystem::~PartSystem()
{
}
void PartSystem::step()
{
alarm++;
if (alarm == gen_alarm)
{
alarm = 0;
for (size_t i = 0; i < gen_amount; i++)
{
float p_x = Maths::random_range(generator.left, generator.left + generator.width);
float p_y = Maths::random_range(generator.top, generator.top + generator.height);
float p_dir = Maths::random_range(dir - dir_offset, dir + dir_offset);
float p_life = Maths::random_range(life - life_offset, life + life_offset);
float p_speed = Maths::random_range(speed - speed_offset, speed + speed_offset);
float p_acc = Maths::random_range(acc - acc_offset, acc + acc_offset);
float p_size = Maths::random_range(size - size_offset, size + size_offset);
particles.push_front(Particle(sprite, p_x, p_y, p_dir, p_speed, p_acc, p_size, p_life));
}
}
std::list<Particle>::iterator i = particles.begin();
while (i != particles.end())
{
if ((*i).step())
{
i = particles.erase(i);
}
else
{
i++;
}
}
}
#include <iostream>
void PartSystem::position_push(float x, float y, float force, float angle_min, float angle_max)
{
for (auto p : particles)
{
float dist = Maths::point_dist(x, y, p.getPosition().x, p.getPosition().y);
float applied_force = (dist > 1) ? (force / sqrt(dist)) : force;
if (applied_force < 0.1) { continue; }
float angle = Maths::point_angle(p.getPosition().x, p.getPosition().y, x, y);
if (angle <= angle_max && angle >= angle_min) { p.push(angle, force); }
}
}
void PartSystem::draw(sf::RenderTarget & target)
{
for (Particle& p : particles)
{
target.draw(p);
}
}