Exemplo n.º 1
0
/**
 * \brief Creates a pickable item with the specified subtype.
 *
 * This method acts like a constructor, except that it can return nullptr in several cases:
 * - the treasure is saved and the player already has it,
 * or:
 * - the treasure is empty,
 * or:
 * - the item cannot be obtained by the hero yet,
 * or:
 * - the animation of the item is missing in sprite 'entities/items'.
 *
 * \param game the current game
 * \param name Name identifying the entity on the map or an empty string.
 * \param layer layer of the pickable item to create on the map
 * \param x x coordinate of the pickable item to create
 * \param y y coordinate of the pickable item to create
 * \param treasure the treasure to give
 * \param falling_height to make the item fall when it appears
 * \param force_persistent true to make the item stay forever
 * (otherwise, the properties of the item
 * decide if it disappears after some time)
 * \return the pickable item created, or nullptr
 */
std::shared_ptr<Pickable> Pickable::create(
    Game& /* game */,
    const std::string& name,
    Layer layer,
    int x,
    int y,
    Treasure treasure,
    FallingHeight falling_height,
    bool force_persistent
) {
  treasure.ensure_obtainable();

  // Don't create anything if there is no treasure to give.
  if (treasure.is_found() || treasure.is_empty()) {
    return nullptr;
  }

  std::shared_ptr<Pickable> pickable = std::make_shared<Pickable>(
      name, layer, x, y, treasure
  );

  // Set the item properties.
  pickable->falling_height = falling_height;
  pickable->will_disappear = !force_persistent && treasure.get_item().get_can_disappear();

  // Initialize the pickable item.
  if (!pickable->initialize_sprites()) {
    return nullptr;  // No valid sprite: don't create the pickable.
  }
  pickable->initialize_movement();

  return pickable;
}
Exemplo n.º 2
0
/**
 * \brief Creates a new chest with the specified treasure.
 * \param name Name identifying this chest.
 * \param layer Layer of the chest to create on the map.
 * \param xy Coordinates of the chest to create.
 * \param sprite_name Name of the animation set of the
 * sprite to create for the chest. It must have animations "open" and "closed".
 * \param treasure The treasure in the chest.
 */
Chest::Chest(
    const std::string& name,
    Layer layer,
    const Point& xy,
    const std::string& sprite_name,
    const Treasure& treasure):

  Detector(COLLISION_FACING, name, layer, xy, Size(16, 16)),
  treasure(treasure),
  open(treasure.is_found()),
  treasure_given(open),
  treasure_date(0),
  opening_method(OpeningMethod::BY_INTERACTION),
  opening_condition_consumed(false) {

  // Create the sprite.
  Sprite& sprite = *create_sprite(sprite_name);
  std::string animation = is_open() ? "open" : "closed";
  sprite.set_current_animation(animation);

  set_origin(get_width() / 2, get_height() - 3);

  // TODO set this as the default drawn_in_y_order for MapEntity
  set_drawn_in_y_order(sprite.get_max_size().height > get_height());
}
Exemplo n.º 3
0
/**
 * @brief Creates a new chest with the specified treasure.
 * @param name name identifying this chest
 * @param layer layer of the chest to create on the map
 * @param x x coordinate of the chest to create
 * @param y y coordinate of the chest to create
 * @param big_chest true to make a big chest, false to make a normal chest
 * @param treasure the treasure in the chest (will be deleted automatically)
 */
Chest::Chest(const std::string &name, Layer layer, int x, int y,
	     bool big_chest, const Treasure &treasure):

  Detector(COLLISION_FACING_POINT, name, layer, x, y, 16, 16),
  treasure(treasure),
  big_chest(big_chest),
  open(treasure.is_found()),
  treasure_given(open),
  treasure_date(0) {

  initialize_sprite();
}
Exemplo n.º 4
0
/**
 * \brief Creates a new shop treasure with the specified treasure and price.
 * \param game the current game
 * \param name the name identifying this entity
 * \param layer layer of the entity to create
 * \param x x coordinate of the entity to create
 * \param y y coordinate of the entity to create
 * \param treasure the treasure that the hero can buy
 * \param price the treasure's price in rupees
 * \param dialog_id id of the dialog describing the item when the player watches it
 * \return the shop treasure created, or nullptr if it is already bought or if it
 * is not obtainable.
 */
std::shared_ptr<ShopTreasure> ShopTreasure::create(
    Game& /* game */,
    const std::string& name,
    Layer layer,
    int x,
    int y,
    const Treasure& treasure,
    int price,
    const std::string& dialog_id
) {
  // See if the item is not already bought and is obtainable.
  if (treasure.is_found() || !treasure.is_obtainable()) {
    return nullptr;
  }

  return std::make_shared<ShopTreasure>(
      name, layer, x, y, treasure, price, dialog_id
  );
}
Exemplo n.º 5
0
/**
 * @brief Creates a new chest with the specified treasure.
 * @param name Name identifying this chest.
 * @param layer Layer of the chest to create on the map.
 * @param x X coordinate of the chest to create.
 * @param y Y coordinate of the chest to create.
 * @param sprite_name Name of the animation set of the
 * sprite to create for the chest. It must have animations "open" and "closed".
 * @param treasure The treasure in the chest.
 */
Chest::Chest(
    const std::string& name,
    Layer layer,
    int x,
    int y,
    const std::string& sprite_name,
    const Treasure& treasure):

  Detector(COLLISION_FACING_POINT, name, layer, x, y, 16, 16),
  treasure(treasure),
  open(treasure.is_found()),
  treasure_given(open),
  treasure_date(0) {

  // Create the sprite.
  Sprite& sprite = create_sprite(sprite_name);
  std::string animation = is_open() ? "open" : "closed";
  sprite.set_current_animation(animation);

  set_origin(get_width() / 2, get_height() - 3);
}