/** * \brief Checks whether a sprite collides with this detector. * * If there is a collision, the notify_collision(Entity&, Sprite&, Sprite&) method is called. * * \param entity the entity to check * \param sprite the sprite of that entity */ void Detector::check_collision(Entity& entity, Sprite& sprite) { if (has_collision_mode(COLLISION_SPRITE) && &entity != this && (has_layer_independent_collisions() || get_layer() == entity.get_layer())) { // we check the collision between the specified entity's sprite and all sprites of the current entity for (const SpritePtr& this_sprite: get_sprites()) { if (this_sprite->test_collision(sprite, get_x(), get_y(), entity.get_x(), entity.get_y())) { notify_collision(entity, *this_sprite, sprite); } } } }
/** * \brief Checks whether a sprite collides with this detector. * * If there is a collision, the notify_collision(MapEntity&, Sprite&, Sprite&) method is called. * * \param entity the entity to check * \param sprite the sprite of that entity */ void Detector::check_collision(MapEntity& entity, Sprite& sprite) { if (has_collision_mode(COLLISION_SPRITE) && &entity != this && (has_layer_independent_collisions() || get_layer() == entity.get_layer())) { // we check the collision between the specified entity's sprite and all sprites of the current entity std::vector<Sprite*>::const_iterator it; for (it = get_sprites().begin(); it != get_sprites().end(); it++) { Sprite& this_sprite = *(*it); if (this_sprite.test_collision(sprite, get_x(), get_y(), entity.get_x(), entity.get_y())) { notify_collision(entity, sprite, this_sprite); } } } }
/** * \brief Checks whether an entity collides with this detector. * * This function is called by the map when an entity has just moved. * It checks whether the entity collides with this detector. * Depending on the detector collision mode(s), the appropriate * test_collision_* functions are called. * If there is a collision, the notify_collision() method is called. * * \param entity the entity to check */ void Detector::check_collision(Entity& entity) { if (&entity != this && (has_layer_independent_collisions() || get_layer() == entity.get_layer())) { // the entity is in the same layer as the detector // detect the collision depending on the collision mode if (has_collision_mode(COLLISION_OVERLAPPING) && test_collision_rectangle(entity)) { notify_collision(entity, COLLISION_OVERLAPPING); } if (has_collision_mode(COLLISION_CONTAINING) && test_collision_inside(entity)) { notify_collision(entity, COLLISION_CONTAINING); } if (has_collision_mode(COLLISION_ORIGIN) && test_collision_origin_point(entity)) { notify_collision(entity, COLLISION_ORIGIN); } if (has_collision_mode(COLLISION_FACING) && test_collision_facing_point(entity)) { if (entity.get_facing_entity() == nullptr) { // make sure only one entity can think "I am the facing entity" entity.set_facing_entity(this); } notify_collision(entity, COLLISION_FACING); } if (has_collision_mode(COLLISION_TOUCHING) && test_collision_touching(entity)) { notify_collision(entity, COLLISION_TOUCHING); } if (has_collision_mode(COLLISION_CENTER) && test_collision_center(entity)) { notify_collision(entity, COLLISION_CENTER); } if (has_collision_mode(COLLISION_CUSTOM) && test_collision_custom(entity)) { notify_collision(entity, COLLISION_CUSTOM); } } }
/** * \brief Notifies this custom entity that another entity has detected a * pixel-precise collision with it. * \param other_entity The other entity. * \param other_sprite Sprite of the other entity involved in the collision. * \param this_sprite Sprite of this custom entity entity involved in the collision. */ void CustomEntity::notify_collision_from(MapEntity& other_entity, Sprite& other_sprite, Sprite& this_sprite) { // No need to check the collision again: sprite collisions are symmetrical. notify_collision(other_entity, other_sprite, this_sprite); }