Exemple #1
0
/**
 * @brief Creates a new destructible item with the specified subtype.
 * @param name Unique name identifying the entity on the map or an empty string.
 * @param layer layer of the destructible item to create on the map
 * @param x x coordinate of the destructible item to create
 * @param y y coordinate of the destructible item to create
 * @param subtype subtype of destructible item to create
 * @param treasure the pickable item that appears when the destructible
 * item is lifted or cut
 */
Destructible::Destructible(const std::string& name, Layer layer, int x, int y,
    Subtype subtype, const Treasure &treasure):

  Detector(COLLISION_NONE, name, layer, x, y, 16, 16),
  subtype(subtype),
  treasure(treasure),
  is_being_cut(false),
  regeneration_date(0),
  is_regenerating(false),
  destruction_callback_ref(LUA_REFNIL) {

  set_origin(8, 13);
  create_sprite(get_animation_set_id());

  // set the collision mode
  if (features[subtype].can_be_lifted) {
    add_collision_mode(COLLISION_FACING_POINT);
  }

  if (features[subtype].can_be_cut
      || features[subtype].can_explode) {
    add_collision_mode(COLLISION_SPRITE);
  }

  if (has_special_ground()) { // display a special ground under the hero
    add_collision_mode(COLLISION_CUSTOM);
  }
}
/**
 * \brief Sets the appropriate collisions modes.
 *
 * This depends on whether the object is an obstacle, can be cut
 * or can explode.
 */
void Destructible::update_collision_modes() {

  // Reset previous collision modes.
  set_collision_modes(0);

  // Sets the new ones.
  if (get_modified_ground() == Ground::WALL) {
    // The object is an obstacle.
    // Set the facing collision mode to allow the hero to look at it.
    add_collision_mode(COLLISION_FACING);
  }

  if (get_can_be_cut()
      || get_can_explode()) {
    add_collision_mode(COLLISION_SPRITE);
  }
}
/**
 * \brief Registers a function to be called when the specified test detects a
 * collision.
 * \param collision_test A built-in collision test.
 * \param callback_ref Lua ref to a function to call when this collision is
 * detected.
 */
void CustomEntity::add_collision_test(
    CollisionMode collision_test,
    const ScopedLuaRef& callback_ref
) {
    Debug::check_assertion(collision_test != COLLISION_NONE, "Invalid collision mode");
    Debug::check_assertion(!callback_ref.is_empty(), "Missing collision callback");

    if (collision_test == COLLISION_SPRITE) {
        add_collision_mode(COLLISION_SPRITE);
    }
    else {
        add_collision_mode(COLLISION_CUSTOM);
    }

    collision_tests.emplace_back(
        get_lua_context(),
        collision_test,
        callback_ref
    );
}
/**
 * \brief Registers a function to be called when the specified test detects a
 * collision.
 * \param collision_test_ref Lua ref to a custom collision test.
 * \param callback_ref Lua ref to a function to call when this collision is
 * detected.
 */
void CustomEntity::add_collision_test(
    const ScopedLuaRef& collision_test_ref,
    const ScopedLuaRef& callback_ref
) {
    Debug::check_assertion(!callback_ref.is_empty(), "Missing collision callback");

    add_collision_mode(COLLISION_CUSTOM);

    collision_tests.emplace_back(
        get_lua_context(),
        collision_test_ref,
        callback_ref
    );
}