Skip to content

Commit 113eb9c

Browse files
committed
Introduce Rectf::grown(Vector) for growing rectangle by different amounts in X and Y direction / use method in Climbable
1 parent e5ad93f commit 113eb9c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/math/rectf.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ class Rectf final
147147
return glm::distance(v1, v2);
148148
}
149149

150+
/**
151+
* Returns a new Rectf that is grown / shrunk by the specified amount
152+
* @param border The amount of pixels that the Rectangle should be
153+
* grown (for positive values) or shrunk (for negative values)
154+
*/
150155
Rectf grown(float border) const
151156
{
152157
// If the size would be shrunk below 0, do not resize.
@@ -157,6 +162,22 @@ class Rectf final
157162
get_right() + border, get_bottom() + border);
158163
}
159164

165+
/**
166+
* Returns a new Rectf instance that is grown / shrunk by the amounts specified by the vector.
167+
*
168+
* @param border The passed vector contains the horizontal amount as first component and the vertical amount
169+
* as the second component.
170+
*/
171+
Rectf grown(Vector border) const
172+
{
173+
// If the size would be shrunk below 0, do not resize.
174+
if (m_size.width + border.x * 2 < 0.f || m_size.height + border.y * 2 < 0.f)
175+
return *this;
176+
177+
return Rectf(m_p1.x - border.x, m_p1.y - border.y,
178+
get_right() + border.x, get_bottom() + border.y);
179+
}
180+
160181
// leave these two public to save the headaches of set/get functions for such
161182
// simple things :)
162183

src/trigger/climbable.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,16 @@ Climbable::update(float dt_sec)
8484
{
8585
if (it2->m_activate_try_timer->started())
8686
{
87+
auto bbox_with_grace = m_col.m_bbox.grown(Vector(GRACE_DX, GRACE_DY));
8788
// The "-20" to y velocity prevents Tux from walking in place on the ground for horizonal adjustments.
88-
if (it2->m_player->get_bbox().get_left() < m_col.m_bbox.get_left() - GRACE_DX) it2->m_player->add_velocity(Vector(POSITION_FIX_AX,-20));
89-
if (it2->m_player->get_bbox().get_right() > m_col.m_bbox.get_right() + GRACE_DX) it2->m_player->add_velocity(Vector(-POSITION_FIX_AX,-20));
90-
if (it2->m_player->get_bbox().get_top() < m_col.m_bbox.get_top() - GRACE_DY) it2->m_player->add_velocity(Vector(0,POSITION_FIX_AY));
91-
if (it2->m_player->get_bbox().get_bottom() > m_col.m_bbox.get_bottom() + GRACE_DY) it2->m_player->add_velocity(Vector(0,-POSITION_FIX_AY));
89+
if (it2->m_player->get_bbox().get_left() < bbox_with_grace.get_left())
90+
it2->m_player->add_velocity(Vector(POSITION_FIX_AX, -20));
91+
if (it2->m_player->get_bbox().get_right() > bbox_with_grace.get_right())
92+
it2->m_player->add_velocity(Vector(-POSITION_FIX_AX, -20));
93+
if (it2->m_player->get_bbox().get_top() < bbox_with_grace.get_top())
94+
it2->m_player->add_velocity(Vector(0, POSITION_FIX_AY));
95+
if (it2->m_player->get_bbox().get_bottom() > bbox_with_grace.get_bottom())
96+
it2->m_player->add_velocity(Vector(0, -POSITION_FIX_AY));
9297
}
9398
if (may_climb(*(it2->m_player)))
9499
{

0 commit comments

Comments
 (0)