Try to make collision work - WiP

This commit is contained in:
King_DuckZ 2017-07-24 01:19:06 +01:00
parent 40967784a3
commit 64969766d9
3 changed files with 27 additions and 20 deletions

View file

@ -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<vec2f>(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) {

View file

@ -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<vec2f>(parTile) * vector_cast<vec2f>(parWorld.tile_size());
}
bool position_is_on_map (const WorldGrid& parWorld, const vec2i& parPos) {
assert(parWorld.tile_size() > vec2us(0));

View file

@ -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