-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tracks.cpp
94 lines (79 loc) · 2.46 KB
/
Tracks.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
91
92
93
94
#include "Tracks.hpp"
#include <SFML/Graphics/RenderTarget.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <boost/range/algorithm/max_element.hpp>
#include <boost/range/algorithm/min_element.hpp>
#include <boost/range/algorithm_ext/erase.hpp>
#include <boost/algorithm/cxx11/any_of.hpp>
#include "Window.hpp"
#include <Thor/Math/Random.hpp>
Tracks::Tracks(){
m_chunks.push_back(TrackChunk(0.f, screenWidth, screenHeight/2.f));
m_right = m_chunks.back().right();
}
void Tracks::addNewChunk()
{
auto newChunkRight = thor::random(m_right.x+screenWidth/2.f, m_right.x + screenWidth);
m_chunks.push_back(TrackChunk(m_right.x,
newChunkRight,
m_right.y));
m_right = m_chunks.back().right();
}
void Tracks::update(float left, float right){
boost::remove_erase_if(m_chunks, [left](const auto& chunk){
return chunk.right().x < left;
});
if (m_chunks.empty())
addNewChunk();
while (right > m_right.x){
auto haveSpace = thor::random(0,1);
if (haveSpace)
m_right.x += distancePossible();
else
addNewChunk();
}
}
void Tracks::draw(sf::RenderTarget &target, sf::RenderStates states) const{
for (const auto& chunk : m_chunks)
target.draw(chunk);
}
Tracks::ChunkIterator Tracks::chunkAt(float x) const {
return boost::find_if(m_chunks, [x](const auto& chunk){
return chunk.left()<=x && x<=chunk.right().x;
});
}
Tracks::ChunkIterator Tracks::end() const
{
return m_chunks.cend();
}
float Tracks::top() const
{
return boost::min_element(m_chunks, [](const auto& lhs, const auto& rhs){
return lhs.top() < rhs.top();
})->top();
}
float Tracks::bottom() const
{
return boost::max_element(m_chunks, [](const auto& lhs, const auto& rhs){
return lhs.bottom() < rhs.bottom();
})->bottom();
}
float Tracks::distancePossible() const
{
//TODO: calculate this analytically
return thor::random(50, 500);
}
sf::Vector2f Tracks::getDirection(ChunkIterator chunk, float x) const{
return chunk->getDirection(x);
}
sf::Vector2f Tracks::getPointAt(ChunkIterator chunk, float x) const{
return chunk->getPointAt(x);
}
bool Tracks::collidesWith(const sf::Shape &shape) const{
return boost::algorithm::any_of(m_chunks, [&shape](const auto& chunk){
return chunk.collidesWith(shape);
});
}
sf::Vector2f Tracks::getStart() const{
return m_chunks.front().getStart();
}