Center viewport on the character when possible.

This commit is contained in:
King_DuckZ 2017-01-27 19:39:47 +00:00
parent 4715552b44
commit 7a0d2c3267
3 changed files with 12 additions and 16 deletions

View file

@ -22,25 +22,20 @@
namespace curry {
namespace {
vec2f get_safe_width_height (const Texture& parTex) {
return (parTex.texture() ?
vector_cast<vec2f>(parTex.width_height())
:
vec2f(0.0f)
);
}
} //unnamed namespace
Character::Character (const WorldSizeNotifiable::DeferredRegister& parDeferredRegister) :
WorldSizeNotifiable(parDeferredRegister),
m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f))
m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f)),
m_width_height(0.0f)
{
}
void Character::load (const char* parTexturePath, cloonel::SDLMain& parSDLMain) {
const auto old_wh = get_safe_width_height(m_texture);
const auto old_wh = m_width_height;
m_texture.load(parTexturePath, parSDLMain);
const auto new_wh = vector_cast<vec2f>(m_texture.width_height());
m_width_height = vector_cast<vec2f>(m_texture.width_height());
const auto& new_wh = m_width_height;
if (m_position.get_min() != m_position.get_max())
m_position.change_minmax(m_position.get_min(), m_position.get_max() + old_wh - new_wh);
}
@ -57,8 +52,8 @@ namespace curry {
return m_position.get();
}
vec2us Character::width_height() const {
return get_safe_width_height(m_texture);
const vec2f& Character::width_height() const {
return m_width_height;
}
Texture& Character::texture() {
@ -68,14 +63,14 @@ namespace curry {
Rect<float> Character::destination_rect (const vec2f& parWorldPos) const {
return Rect<float>(
m_position.get() - parWorldPos,
m_position.get() - parWorldPos + vector_cast<vec2f>(width_height())
m_position.get() - parWorldPos + width_height()
);
}
Rect<float> Character::source_rect() const {
return Rect<float>(
vec2f(0.0f),
vector_cast<vec2f>(width_height())
width_height()
);
}

View file

@ -39,7 +39,7 @@ namespace curry {
void unload();
const vec2f& position() const;
void set_position (const vec2f& parPos);
vec2us width_height() const;
const vec2f& width_height() const;
Texture& texture();
Rect<float> destination_rect (const vec2f& parWorldPos) const;
Rect<float> source_rect() const;
@ -48,5 +48,6 @@ namespace curry {
private:
ConstrainedPosition<vec2f> m_position;
Texture m_texture;
vec2f m_width_height;
};
} //namespace curry

View file

@ -114,8 +114,8 @@ namespace curry {
offset.y() -= speed;
if (cloonel::IsPressed(input_bag(), ActionDown))
offset.y() += speed;
viewport.set_position(viewport.position() + offset);
m_local_data->character.set_position(m_local_data->character.position() + offset);
viewport.set_position(m_local_data->character.position() - (viewport.size() - m_local_data->character.width_height()) / 2.0f);
const auto tilesize(static_cast<vec2f>(world.tile_size()));
const auto tilecount(m_local_data->worldtiles.width_height() / world.tile_size());