Skip to content

Commit

Permalink
Decrease resolution and fix chunks becoming 0 as a result
Browse files Browse the repository at this point in the history
  • Loading branch information
Kojirion committed Mar 22, 2016
1 parent e635a52 commit f9c57cb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
9 changes: 2 additions & 7 deletions TrackChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ float TrackChunk::left() const{
return m_points.front().x;
}

float TrackChunk::right() const{
return m_points.back().x;
const sf::Vector2f &TrackChunk::right() const{
return m_points.back();
}

float TrackChunk::top() const
Expand All @@ -119,11 +119,6 @@ float TrackChunk::bottom() const
})->y;
}

float TrackChunk::rightHeight() const
{
return m_points.back().y;
}

void TrackChunk::draw(sf::RenderTarget &target, sf::RenderStates states) const{
m_vertexArray.clear();
for (const auto& point : m_points)
Expand Down
3 changes: 1 addition & 2 deletions TrackChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ class TrackChunk : public sf::Drawable {
bool collidesWith(const sf::Shape& shape) const;
sf::Vector2f getStart() const;
float left() const;
float right() const;
const sf::Vector2f& right() const;
float top() const;
float bottom() const;
float rightHeight() const;

private:
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
Expand Down
32 changes: 19 additions & 13 deletions Tracks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@ Tracks::Tracks(){
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() < left;
return chunk.right().x < left;
});

while (right > m_right){
auto haveSpace = thor::random(0,1);
if (m_chunks.empty())
addNewChunk();

if (haveSpace){
m_right += distancePossible();
}else{
auto newChunkRight = thor::random(m_right+screenWidth/2.f, m_right + screenWidth);
m_chunks.push_back(TrackChunk(m_right,
newChunkRight,
m_chunks.back().rightHeight()));
m_right = m_chunks.back().right();
}
while (right > m_right.x){
auto haveSpace = thor::random(0,1);
if (haveSpace)
m_right.x += distancePossible();
else
addNewChunk();
}
}

Expand All @@ -40,7 +46,7 @@ void Tracks::draw(sf::RenderTarget &target, sf::RenderStates states) const{

Tracks::ChunkIterator Tracks::chunkAt(float x) const {
return boost::find_if(m_chunks, [x](const auto& chunk){
return chunk.left()<=x && x<=chunk.right();
return chunk.left()<=x && x<=chunk.right().x;
});
}

Expand Down
3 changes: 2 additions & 1 deletion Tracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Tracks : public sf::Drawable
private:
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
float distancePossible() const;
void addNewChunk();

Chunks m_chunks;
float m_right;
sf::Vector2f m_right;
};
4 changes: 2 additions & 2 deletions Window.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once

const int screenWidth = 1400;
const int screenHeight = 1000;
const int screenWidth = 800;
const int screenHeight = 600;

0 comments on commit f9c57cb

Please sign in to comment.