Don't move the viewport if it would generate gaps in the map.

In other words, don't let the player see past the end of the world.
This commit is contained in:
King_DuckZ 2016-11-08 20:34:04 +01:00
parent 5d231206ef
commit 4c105fc3df
3 changed files with 22 additions and 5 deletions

View file

@ -69,6 +69,8 @@ namespace curry {
#if !defined(NDEBUG) #if !defined(NDEBUG)
std::cout << "World size set to " << world_size << '\n'; std::cout << "World size set to " << world_size << '\n';
#endif #endif
m_local_data->viewport.allow_overscan(false);
} }
void IngameScene::on_update (float parDeltaT) { void IngameScene::on_update (float parDeltaT) {
@ -80,14 +82,16 @@ namespace curry {
auto& viewport = m_local_data->viewport; auto& viewport = m_local_data->viewport;
const float speed = parDeltaT * speed_per_sec; const float speed = parDeltaT * speed_per_sec;
vec2f offset(0.0f);
if (cloonel::IsPressed(input_bag(), ActionRight)) if (cloonel::IsPressed(input_bag(), ActionRight))
viewport.set_position(viewport.position() + vec2f(speed, 0)); offset.x() += speed;
if (cloonel::IsPressed(input_bag(), ActionLeft)) if (cloonel::IsPressed(input_bag(), ActionLeft))
viewport.set_position(viewport.position() - vec2f(speed, 0)); offset.x() -= speed;
if (cloonel::IsPressed(input_bag(), ActionUp)) if (cloonel::IsPressed(input_bag(), ActionUp))
viewport.set_position(viewport.position() - vec2f(0, speed)); offset.y() -= speed;
if (cloonel::IsPressed(input_bag(), ActionDown)) if (cloonel::IsPressed(input_bag(), ActionDown))
viewport.set_position(viewport.position() + vec2f(0, speed)); offset.y() += speed;
viewport.set_position(viewport.position() + offset);
const auto tilesize(static_cast<vec2f>(world.tile_size())); const auto tilesize(static_cast<vec2f>(world.tile_size()));
const auto tilecount(m_local_data->worldtiles.width_height() / world.tile_size()); const auto tilecount(m_local_data->worldtiles.width_height() / world.tile_size());

View file

@ -46,10 +46,21 @@ namespace curry {
} }
void WorldViewport::set_position (const vec2f& parPos) { void WorldViewport::set_position (const vec2f& parPos) {
m_position = parPos; //TODO: assert validity if (m_allow_overscan) {
m_position = parPos;
}
else {
const auto max_offs = vector_cast<vec2f>(world_size_pixel(*m_world)) - size();
m_position.x() = std::min(std::max(parPos.x(), 0.0f), max_offs.x());
m_position.y() = std::min(std::max(parPos.y(), 0.0f), max_offs.y());
}
} }
const TileIndex* WorldViewport::tile (const vec2us& parIndex) const { const TileIndex* WorldViewport::tile (const vec2us& parIndex) const {
return world()->tile(parIndex + starting_index(*this)); return world()->tile(parIndex + starting_index(*this));
} }
void WorldViewport::allow_overscan (bool parAllow) {
m_allow_overscan = parAllow;
}
} //namespace curry } //namespace curry

View file

@ -16,6 +16,7 @@ namespace curry {
const vec2f& position() const; const vec2f& position() const;
const vec2f& size() const; const vec2f& size() const;
const TileIndex* tile (const vec2us& parIndex) const; const TileIndex* tile (const vec2us& parIndex) const;
void allow_overscan (bool parAllow);
void set_position (const vec2f& parPos); void set_position (const vec2f& parPos);
@ -26,5 +27,6 @@ namespace curry {
vec2f m_position; vec2f m_position;
vec2f m_size; vec2f m_size;
WorldGrid* m_world; WorldGrid* m_world;
bool m_allow_overscan;
}; };
} //namespace curry } //namespace curry