Exemple #1
0
/**
 * \copydoc Detector::notify_action_command_pressed
 */
bool Chest::notify_action_command_pressed() {

  if (is_enabled() &&
      get_hero().is_free() &&
      get_keys_effect().get_action_key_effect() != KeysEffect::ACTION_KEY_NONE
  ) {

    if (can_open()) {
      Sound::play("chest_open");
      set_open(true);
      treasure_date = System::now() + 300;

      get_keys_effect().set_action_key_effect(KeysEffect::ACTION_KEY_NONE);
      get_hero().start_freezed();
    }
    else if (!get_cannot_open_dialog_id().empty()) {
      Sound::play("wrong");
      get_game().start_dialog(get_cannot_open_dialog_id(), ScopedLuaRef(), ScopedLuaRef());
    }

    return true;
  }

  return false;
}
Exemple #2
0
/**
 * \copydoc Entity::notify_action_command_pressed
 */
bool Npc::notify_action_command_pressed() {

  Hero& hero = get_hero();
  if (hero.is_free() &&
      get_commands_effects().get_action_key_effect() != CommandsEffects::ACTION_KEY_NONE
  ) {

    CommandsEffects::ActionKeyEffect effect = get_commands_effects().get_action_key_effect();
    get_commands_effects().set_action_key_effect(CommandsEffects::ACTION_KEY_NONE);

    SpritePtr sprite = get_sprite();

    // if this is a usual NPC, look towards the hero
    if (subtype == USUAL_NPC) {
      int direction = (get_hero().get_animation_direction() + 2) % 4;
      if (sprite != nullptr) {
        sprite->set_current_direction(direction);
      }
    }

    if (effect != CommandsEffects::ACTION_KEY_LIFT) {
      // start the normal behavior
      if (behavior == BEHAVIOR_DIALOG) {
        get_game().start_dialog(dialog_to_show, ScopedLuaRef(), ScopedLuaRef());
      }
      else {
        call_script_hero_interaction();
      }
      return true;
    }
    else {
      // lift the entity
      if (get_equipment().has_ability(Ability::LIFT)) {

        std::string animation_set_id = "stopped";
        if (sprite != nullptr) {
          animation_set_id = sprite->get_animation_set_id();
        }
        hero.start_lifting(std::make_shared<CarriedObject>(
            hero,
            *this,
            animation_set_id,
            "stone",
            2,
            0)
        );
        Sound::play("lift");
        remove_from_map();
        return true;
      }
    }
  }
  return false;
}
Exemple #3
0
/**
 * \copydoc Detector::notify_action_command_pressed
 */
bool Crystal::notify_action_command_pressed() {

    if (get_hero().is_free() &&
            get_keys_effect().get_action_key_effect() == KeysEffect::ACTION_KEY_LOOK
       ) {
        get_keys_effect().set_action_key_effect(KeysEffect::ACTION_KEY_NONE);

        // start a dialog
        get_game().start_dialog("_crystal", ScopedLuaRef(), ScopedLuaRef());
        return true;
    }

    return false;
}
Exemple #4
0
/**
 * \brief Updates the chest.
 *
 * This function is called repeatedly by the map.
 * This is a redefinition of MapEntity::update()
 * the handle the chest opening.
 */
void Chest::update() {

  if (is_open() && !is_suspended()) {

    if (!treasure_given && treasure_date != 0 && System::now() >= treasure_date) {

      treasure_date = 0;
      treasure.ensure_obtainable();  // Make the chest empty if the treasure is not allowed.
      if (!treasure.is_empty()) {
        // Give a treasure to the player.

        get_hero().start_treasure(treasure, ScopedLuaRef());
        treasure_given = true;
      }
      else {  // The chest is empty.

        if (treasure.is_saved()) {
          // Mark the treasure as found in the savegame.
          get_savegame().set_boolean(treasure.get_savegame_variable(), true);
        }

        treasure_given = true;

        bool done = get_lua_context().chest_on_empty(*this);
        if (!done) {
          // The script does not define any behavior:
          // by default, do nothing and unfreeze the hero.
          get_hero().start_free();
        }
      }
    }
  }

  MapEntity::update();
}
Exemple #5
0
/**
 * \brief Gives the item to the player.
 */
void Pickable::try_give_item_to_player() {

  EquipmentItem& item = treasure.get_item();

  if (!can_be_picked
      || given_to_player
      || get_game().is_dialog_enabled()
      || !get_hero().can_pick_treasure(item)) {
    return;
  }

  given_to_player = true;

  remove_from_map();

  // play the sound
  const std::string& sound_id = item.get_sound_when_picked();
  if (!sound_id.empty()) {
    Sound::play(sound_id);
  }

  // give the item
  if (item.get_brandish_when_picked()) {
    // The treasure is brandished.
    // on_obtained() will be called after the dialog.
    get_hero().start_treasure(treasure, ScopedLuaRef());
  }
  else {
    treasure.give_to_player();

    // Call on_obtained() immediately since the treasure is not brandished.
    get_lua_context().item_on_obtained(item, treasure);
    get_lua_context().map_on_obtained_treasure(get_map(), treasure);
  }
}
Exemple #6
0
/**
 * \brief Plays a music.
 *
 * If the music is different from the current one, the current one is stopped.
 * The music specified can also be Music::none_id (then the current music is
 * just stopped).
 * or even Music::unchanged_id (the current music continues in this case).
 *
 * \param music_id Id of the music to play (file name without extension).
 * \param loop Whether the music should loop when reaching its end
 * (if there is an end).
 */
void Music::play(const std::string& music_id, bool loop) {

  play(music_id, loop, ScopedLuaRef());
}
/**
 * \brief Starts a custom animation of the hero's sprites.
 *
 * All sprites of the hero that have an animation with this name take the
 * animation. The ones that don't have such an animation are not displayed.
 * Many simple animations can be started with this function.
 * More complex one have dedicated functions.
 *
 * \param animation Name of the animation to give to the hero's sprites.
 */
void HeroSprites::set_animation(const std::string& animation) {

  set_animation(animation, ScopedLuaRef());
}