float void_wins(float so, float sn) { // prefer one inside the void if (in_wall(so) && in_void(sn)) return sn; // prefer one inside the void if (in_wall(sn) && in_void(so)) return so; // `so' and `sn' are positive : prefer smaller (closer to the wall) if (in_wall(so) && in_wall(sn)) return so < sn ? so : sn; // `so' and `sn' are negative : prefer bigger (closer to the wall, // with smaller abs) return so < sn ? sn : so; }
void Hedgehog::update(float delta) { if (state == DYING) { if (die_sprite.is_finished()) remove(); die_sprite.update(delta); } else { sprite.update(delta); bool was_on_ground = false; if (on_ground()) { was_on_ground = true; if (velocity.y > 0) { velocity.y = 0; pos.y = int(pos.y / TILE_SIZE) * TILE_SIZE + TILE_SIZE - 1; } if (direction_left) velocity.x = -32; else velocity.x = 32; } else { velocity.y += GRAVITY * delta; } Vector old_pos = pos; pos += velocity * delta; if ((was_on_ground && !on_ground()) || in_wall()) { direction_left = !direction_left; pos = old_pos; } } // Check if the player got hit // FIXME: Insert pixel perfect collision detection here Vector player_pos = Player::current()->get_pos(); if (pos.x - 20 < player_pos.x && pos.x + 20 > player_pos.x && pos.y - 20 < player_pos.y && pos.y + 5 > player_pos.y) Player::current()->hit(5); }
bool in_void(float s) {return !in_wall(s);}