Skip to content

Commit

Permalink
fix: moving tiles work again
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahkittyy committed Oct 30, 2022
1 parent 5c0461f commit d498add
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
5 changes: 3 additions & 2 deletions game/moving_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void moving_tile_manager::update(sf::Time dt) {
// check for static collision
sf::FloatRect aabb = t.get_ghost_aabb(new_xp, new_yp);
debug::get().box(util::scale(aabb, float(m_tmap.tile_size())), sf::Color::Red);
auto contacts = m_tmap.intersects(aabb);
auto contacts = m_tmap.intersects(aabb, true);
decltype(contacts) solid_contacts;
std::copy_if(contacts.begin(), contacts.end(), std::back_inserter(solid_contacts), [](std::pair<sf::Vector2f, tile> t) {
return t.second.blocks_moving_tiles();
Expand All @@ -92,7 +92,7 @@ void moving_tile_manager::update(sf::Time dt) {
(util::same_sign(t2.vel().x, t.vel().x) && util::neither_zero(t.vel().x, t2.vel().x)) || //
(util::same_sign(t2.vel().y, t.vel().y) && util::neither_zero(t.vel().y, t2.vel().y)) //
) { continue; }
sf::FloatRect tile_ghost_aabb = std::abs(new_xv) > 0.01f ? t2.get_ghost_aabb_x() : t2.get_ghost_aabb_y();
sf::FloatRect tile_ghost_aabb = t2.get_ghost_aabb();
sf::FloatRect tile_aabb = t2.get_aabb();
sf::Vector2f sz = moving_tile::size();
if (aabb.intersects(tile_ghost_aabb)) {
Expand All @@ -113,6 +113,7 @@ void moving_tile_manager::update(sf::Time dt) {
t2.m_yv = -t2.m_yv;
}
}

break;
}
}
Expand Down
9 changes: 7 additions & 2 deletions game/tilemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,19 @@ void tilemap::m_set_quad(int i, tile t) {
}
}

std::vector<std::pair<sf::Vector2f, tile>> tilemap::intersects(sf::FloatRect aabb) const {
std::vector<std::pair<sf::Vector2f, tile>> tilemap::intersects(sf::FloatRect aabb, bool roofs) const {
std::vector<std::pair<sf::Vector2f, tile>> ret;
sf::IntRect rounded_aabb(aabb.left - 1, aabb.top - 1, 3, 3);

// get all tiles around the player
for (int x = rounded_aabb.left; x <= rounded_aabb.left + rounded_aabb.width; x++) {
for (int y = rounded_aabb.top; y <= rounded_aabb.top + rounded_aabb.height; ++y) {
tile t = get(x, y);
tile t;
if (m_oob(x, y) && roofs) {
t = tile(tile::block, x, y);
} else {
t = get(x, y);
}
sf::FloatRect tile_aabb(x, y, 1, 1);
// add non-empty ones that intersect to the list
if (t != tile::empty && tile_aabb.intersects(aabb)) {
Expand Down
2 changes: 1 addition & 1 deletion game/tilemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class tilemap : public sf::Drawable,
static ImRect calc_uvs(tile::tile_type type, sf::Texture& tex, int tile_size = 16); // calculates the uv coordinates of a tile for imgui purposes

// all tiles that intersect the given aabb
std::vector<std::pair<sf::Vector2f, tile>> intersects(sf::FloatRect aabb) const;
std::vector<std::pair<sf::Vector2f, tile>> intersects(sf::FloatRect aabb, bool roofs = false) const;

sf::Vector2i size() const; // get the map size
int tile_size() const; // get the size of a tile
Expand Down
2 changes: 1 addition & 1 deletion game/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool same_sign(float a, float b) {
}

bool neither_zero(float a, float b) {
return std::abs(a) < 0.001f && std::abs(b) < 0.001f;
return std::abs(a) > 0.001f && std::abs(b) > 0.001f;
}

float deg2rad(float d) {
Expand Down

0 comments on commit d498add

Please sign in to comment.