diff --git a/src/gamelib/character.cpp b/src/gamelib/character.cpp index 1fad99b..66fa277 100644 --- a/src/gamelib/character.cpp +++ b/src/gamelib/character.cpp @@ -81,32 +81,33 @@ namespace curry { offset.y() += speed; const auto old_pos = this->position(); - const auto new_pos = old_pos + offset; - - this->set_position(new_pos); + this->set_position(old_pos + offset); if (position() == old_pos) return; - vec2us last_valid_pos(0xFFFF); - - auto is_walkable = [](vec2us idx) { - return true; //wtp.property->walkable; + const vec2us character_tile = pixel_to_world_tile(*m_world, position()); + vec2us stop_at = pixel_to_world_tile(*m_world, old_pos); + auto is_walkable = [world=m_world,&stop_at](vec2us idx) { + const bool walkable = world->tile_property(world->tile(idx)).walkable; + if (walkable) + stop_at = idx; + else + std::cout << "found non-walkable tile at " << idx << '\n'; + return walkable; }; for_each_voxel_under_segment(old_pos, this->position(), width_height(), *m_world, is_walkable, false); - //for (auto tile_vec : crossed_tiles(old_pos, this->position(), m_world->tile_size())) { - // const TileIndex* const tile = m_world->tile(tile_vec); - // for (uint16_t z = 0; z < m_world->layer_count(); ++z) { - // const auto& tile_prop = m_world->tile_property(tile + z); - // if (not tile_prop.walkable) { - // assert(last_valid_pos != vec2us(0xFFFF)); - // this->set_position(vector_cast(last_valid_pos)); - // } - // else { - // last_valid_pos = tile_vec; - // } - // } - //} + + if (character_tile != stop_at) { + vec2f blocked_pos = world_tile_to_pixel(*m_world, stop_at); + vec2f new_pos = position(); + if (character_tile.x() != stop_at.x()) + new_pos.x() = blocked_pos.x(); + if (character_tile.y() != stop_at.y()) + new_pos.y() = blocked_pos.y(); + + this->set_position(new_pos); + } } void Character::set_speed (float parSpeed) { diff --git a/src/gamelib/worldgrid.cpp b/src/gamelib/worldgrid.cpp index 8f11da0..9a57b92 100644 --- a/src/gamelib/worldgrid.cpp +++ b/src/gamelib/worldgrid.cpp @@ -140,6 +140,11 @@ namespace curry { ); } + vec2f world_tile_to_pixel (const WorldGrid& parWorld, const vec2us& parTile) { + assert(parTile < parWorld.world_size()); + return vector_cast(parTile) * vector_cast(parWorld.tile_size()); + } + bool position_is_on_map (const WorldGrid& parWorld, const vec2i& parPos) { assert(parWorld.tile_size() > vec2us(0)); diff --git a/src/gamelib/worldgrid.hpp b/src/gamelib/worldgrid.hpp index 00e08c7..ae5d371 100644 --- a/src/gamelib/worldgrid.hpp +++ b/src/gamelib/worldgrid.hpp @@ -62,5 +62,6 @@ namespace curry { vec2i world_size_pixel (const WorldGrid& parWorld); vec2us pixel_to_world_tile (const WorldGrid& parWorld, const vec2f& parPos); + vec2f world_tile_to_pixel (const WorldGrid& parWorld, const vec2us& parTile); bool position_is_on_map (const WorldGrid& parWorld, const vec2i& parPos); } //namespace curry