示例#1
0
/**
 * \brief Loads (or reloads) the sprites and sounds of the hero and his
 * equipment.
 *
 * The sprites and sounds loaded may depend on his tunic, sword and shield as
 * specified in the savegame.
 * This function must be called at the game beginning
 * and as soon as the hero's equipment is changed.
 */
void HeroSprites::rebuild_equipment() {

  // Make the tunic sprite be the default one.
  hero.set_default_sprite_name("tunic");

  int animation_direction = -1;
  if (tunic_sprite != nullptr) {
    // Save the direction.
    animation_direction = tunic_sprite->get_current_direction();
  }

  // The hero's body.
  if (has_default_tunic_sprite) {
    set_tunic_sprite_id(get_default_tunic_sprite_id());
  }

  // The hero's shadow.
  if (shadow_sprite == nullptr) {
    shadow_sprite = hero.create_sprite("entities/shadow", "shadow");
    shadow_sprite->set_current_animation("big");
  }

  // The hero's sword.
  if (has_default_sword_sprite) {
    set_sword_sprite_id(get_default_sword_sprite_id());
  }

  if (has_default_sword_sound) {
    set_sword_sound_id(get_default_sword_sound_id());
  }

  const int sword_number = equipment.get_ability(Ability::SWORD);
  if (sword_number > 0) {
    // TODO make this sprite depend on the sword sprite: sword_sprite_id + "_stars"
    std::ostringstream oss;
    oss << "hero/sword_stars" << sword_number;
    sword_stars_sprite = hero.create_sprite(oss.str(), "sword_stars");
    sword_stars_sprite->stop_animation();
  }

  // The hero's shield.
  if (has_default_shield_sprite) {
    set_shield_sprite_id(get_default_shield_sprite_id());
  }

  // The trail.
  trail_sprite = hero.create_sprite("hero/trail", "trail");
  trail_sprite->stop_animation();

  // Restore the animation direction.
  if (animation_direction != -1) {
    set_animation_direction(animation_direction);
  }

  reorder_sprites();
}
示例#2
0
/**
 * \brief Loads (or reloads) the sprites and sounds of the hero and his equipment.
 *
 * The sprites and sounds loaded depend on his tunic, sword and shield as specified in the savegame.
 * This function must be called at the game beginning
 * and as soon as the hero's equipment is changed.
 */
void HeroSprites::rebuild_equipment() {

  std::string tunic_animation;
  std::string sword_animation;
  std::string shield_animation;
  int animation_direction = -1;

  // the hero
  if (tunic_sprite != NULL) {
    // save the animation direction
    animation_direction = tunic_sprite->get_current_direction();
    tunic_animation = tunic_sprite->get_current_animation();
    delete tunic_sprite;
  }

  int tunic_number = equipment.get_ability("tunic");

  Debug::check_assertion(tunic_number > 0, StringConcat() <<
      "Invalid tunic number: " << tunic_number);

  std::ostringstream oss;
  oss << "hero/tunic" << tunic_number;
  tunic_sprite = new Sprite(oss.str());
  tunic_sprite->enable_pixel_collisions();
  if (!tunic_animation.empty()) {
    tunic_sprite->set_current_animation(tunic_animation);
  }

  // the hero's shadow
  if (shadow_sprite == NULL) {
    shadow_sprite = new Sprite("entities/shadow");
    shadow_sprite->set_current_animation("big");
  }

  // the hero's sword
  if (sword_sprite != NULL) {
    if (sword_sprite->is_animation_started()) {
      sword_animation = sword_sprite->get_current_animation();
    }
    delete sword_sprite;
    delete sword_stars_sprite;
    sword_sprite = NULL;
    sword_stars_sprite = NULL;
  }

  int sword_number = equipment.get_ability("sword");

  if (sword_number > 0) {
    // the hero has a sword: get the sprite and the sound
    oss.str("");
    oss << "hero/sword" << sword_number;
    sword_sprite = new Sprite(oss.str());
    sword_sprite->enable_pixel_collisions();
    sword_sprite->set_synchronized_to(tunic_sprite);
    if (sword_animation.empty()) {
      sword_sprite->stop_animation();
    }
    else {
      sword_sprite->set_current_animation(sword_animation);
    }

    oss.str("");
    oss << "sword" << sword_number;
    sword_sound_id = oss.str();

    oss.str("");
    oss << "hero/sword_stars" << sword_number;
    sword_stars_sprite = new Sprite(oss.str());
    sword_stars_sprite->stop_animation();
  }

  // the hero's shield
  if (shield_sprite != NULL) {
    if (shield_sprite->is_animation_started()) {
      shield_animation = shield_sprite->get_current_animation();
    }
    delete shield_sprite;
    shield_sprite = NULL;
  }

  int shield_number = equipment.get_ability("shield");

  if (shield_number > 0) {
    // the hero has a shield
    oss.str("");
    oss << "hero/shield" << shield_number;
    shield_sprite = new Sprite(oss.str());
    shield_sprite->set_synchronized_to(tunic_sprite);
    if (shield_animation.empty()) {
      shield_sprite->stop_animation();
    }
    else {
      shield_sprite->set_current_animation(shield_animation);
    }
  }

  // the trail
  trail_sprite = new Sprite("hero/trail");
  trail_sprite->stop_animation();

  // restore the animation direction
  if (animation_direction != -1) {
    set_animation_direction(animation_direction);
  }
}
示例#3
0
/**
 * \brief Restores the direction of the hero's sprite saved by the last
 * call to save_animation_direction().
 */
void HeroSprites::restore_animation_direction() {
  set_animation_direction(animation_direction_saved);
}
示例#4
0
/**
 * \brief Changes the direction of the hero's sprites.
 *
 * The direction specified is one of the 8 possible movement directions of the hero.
 * The hero's sprites only have four directions, so when
 * the specified direction is a diagonal one,
 * one of the two closest main directions is picked.
 *
 * \param direction the movement direction (0 to 7)
 */
void HeroSprites::set_animation_direction8(int direction) {

  if (get_animation_direction() != animation_directions[direction][1]) {
    set_animation_direction(animation_directions[direction][0]);
  }
}