-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathAnim.cpp
More file actions
99 lines (80 loc) · 2.12 KB
/
PathAnim.cpp
File metadata and controls
99 lines (80 loc) · 2.12 KB
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
#include "PathAnim.hpp"
EdgeAnim::EdgeAnim(const sf::Vector2f& point1, const sf::Vector2f& point2, float distBetweenParticle, float particleRadius) :
point1(point1 - sf::Vector2f(particleRadius, particleRadius)), point2(point2 - sf::Vector2f(particleRadius, particleRadius))
{
dist = std::sqrtf(std::powf((point2.x - point1.x), 2) + std::powf((point2.y - point1.y), 2));
float segments = dist / (distBetweenParticle + 2 * particleRadius);
step.x = (point2.x - point1.x) / segments;
step.y = (point2.y - point1.y) / segments;
sf::Vector2f pos = this->point1;
for (int i = 1; i < segments; i++)
{
sf::CircleShape circle;
circle.setRadius(particleRadius);
circle.setFillColor(sf::Color::White);
circle.setOutlineThickness(2);
circle.setOutlineColor(sf::Color(0, 51, 102));
circle.setPosition(pos);
mList.push_back(circle);
pos += step;
}
step.x /= 20;
step.y /= 20;
}
void EdgeAnim::update()
{
for(sf::CircleShape& circle : mList)
{
circle.setPosition(circle.getPosition() + step);
}
sf::CircleShape circle = mList.back();
float dist2 = std::sqrtf(std::powf((circle.getPosition().x - point1.x), 2) + std::powf((circle.getPosition().y - point1.y), 2));
if (dist2 >= dist)
{
mList.pop_back();
circle.setPosition(point1);
mList.push_front(circle);
}
}
void EdgeAnim::draw(sf::RenderWindow& window) const
{
for (const sf::CircleShape& circle : mList)
{
window.draw(circle);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
PathAnim::PathAnim()
{
}
PathAnim::PathAnim(std::list<std::pair<sf::Vector2f, sf::Vector2f>> edges)
{
for (std::pair<sf::Vector2f, sf::Vector2f>& edge : edges)
{
edgesAnim.push_back(EdgeAnim(edge.first, edge.second, 16, 8));
}
}
void PathAnim::draw(sf::RenderWindow& window) const
{
for (const EdgeAnim& e : edgesAnim)
{
e.draw(window);
}
}
void PathAnim::update()
{
float time = clock.getElapsedTime().asMilliseconds();
while (time > 10)
{
clock.restart();
for (EdgeAnim& e : edgesAnim)
{
e.update();
}
time -= 10;
}
}
std::list<EdgeAnim>& PathAnim::getList()
{
return this->edgesAnim;
}