From 4c105fc3dfd961e046fde80625440c21175b14ce Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 8 Nov 2016 20:34:04 +0100 Subject: [PATCH] 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. --- src/ingamescene.cpp | 12 ++++++++---- src/worldviewport.cpp | 13 ++++++++++++- src/worldviewport.hpp | 2 ++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/ingamescene.cpp b/src/ingamescene.cpp index f98a90f..8b88b12 100644 --- a/src/ingamescene.cpp +++ b/src/ingamescene.cpp @@ -69,6 +69,8 @@ namespace curry { #if !defined(NDEBUG) std::cout << "World size set to " << world_size << '\n'; #endif + + m_local_data->viewport.allow_overscan(false); } void IngameScene::on_update (float parDeltaT) { @@ -80,14 +82,16 @@ namespace curry { auto& viewport = m_local_data->viewport; const float speed = parDeltaT * speed_per_sec; + vec2f offset(0.0f); if (cloonel::IsPressed(input_bag(), ActionRight)) - viewport.set_position(viewport.position() + vec2f(speed, 0)); + offset.x() += speed; if (cloonel::IsPressed(input_bag(), ActionLeft)) - viewport.set_position(viewport.position() - vec2f(speed, 0)); + offset.x() -= speed; if (cloonel::IsPressed(input_bag(), ActionUp)) - viewport.set_position(viewport.position() - vec2f(0, speed)); + offset.y() -= speed; 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(world.tile_size())); const auto tilecount(m_local_data->worldtiles.width_height() / world.tile_size()); diff --git a/src/worldviewport.cpp b/src/worldviewport.cpp index 36e077c..6d29dcc 100644 --- a/src/worldviewport.cpp +++ b/src/worldviewport.cpp @@ -46,10 +46,21 @@ namespace curry { } 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(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 { return world()->tile(parIndex + starting_index(*this)); } + + void WorldViewport::allow_overscan (bool parAllow) { + m_allow_overscan = parAllow; + } } //namespace curry diff --git a/src/worldviewport.hpp b/src/worldviewport.hpp index 46f25cd..a7eaafe 100644 --- a/src/worldviewport.hpp +++ b/src/worldviewport.hpp @@ -16,6 +16,7 @@ namespace curry { const vec2f& position() const; const vec2f& size() const; const TileIndex* tile (const vec2us& parIndex) const; + void allow_overscan (bool parAllow); void set_position (const vec2f& parPos); @@ -26,5 +27,6 @@ namespace curry { vec2f m_position; vec2f m_size; WorldGrid* m_world; + bool m_allow_overscan; }; } //namespace curry