예제 #1
0
/**
 * \brief Updates the animation of the hero's sprites if necessary.
 */
void HeroSprites::update() {

  // Keep the current sprites here in case they change from a script during the operation.
  SpritePtr tunic_sprite = this->tunic_sprite;
  SpritePtr sword_sprite = this->sword_sprite;

  // update the frames
  tunic_sprite->update();

  if (is_sword_visible()) {
    sword_sprite->update();
    sword_sprite->set_current_frame(tunic_sprite->get_current_frame());
    hero.check_collision_with_detectors(*sword_sprite);
  }
  hero.check_collision_with_detectors(*tunic_sprite);

  if (is_sword_stars_visible()) {
    // the stars are not synchronized with the other sprites
    sword_stars_sprite->update();
  }

  if (is_shield_visible()) {
    shield_sprite->update();
    if (walking) {
      shield_sprite->set_current_frame(tunic_sprite->get_current_frame());
    }
  }

  if (is_trail_visible()) {
    trail_sprite->update();
  }

  if (is_ground_visible()) {
    ground_sprite->update();
  }

  if (lifted_item != nullptr && walking) {
    lifted_item->get_sprite().set_current_frame(tunic_sprite->get_current_frame() % 3);
  }

  // blinking
  if (is_blinking()
      && end_blink_date != 0
      && System::now() >= end_blink_date) {
    stop_blinking();
  }

  // Lua callback.
  if (tunic_sprite->is_animation_finished() &&
      !animation_callback_ref.is_empty()) {
    animation_callback_ref.clear_and_call("hero animation callback");
  }
}
예제 #2
0
파일: Sprite.cpp 프로젝트: Arvek/SOLARME
/**
 * \brief Checks whether the frame has to be changed.
 *
 * If the frame changes, next_frame_date is updated.
 */
void Sprite::update() {

  Drawable::update();

  if (suspended || paused) {
    return;
  }

  frame_changed = false;
  uint32_t now = System::now();

  // update the current frame
  if (synchronize_to == NULL
      || current_animation_name != synchronize_to->get_current_animation()
      || synchronize_to->get_current_direction() > get_nb_directions()
      || synchronize_to->get_current_frame() > get_nb_frames()) {
    // update the frames normally (with the time)
    int next_frame;
    while (!finished && !suspended && !paused && get_frame_delay() > 0
        && now >= next_frame_date) {

      // we get the next frame
      next_frame = get_next_frame();

      // test whether the animation is finished
      if (next_frame == -1) {
        finished = true;
        if (lua_context != NULL) {
          lua_context->sprite_on_animation_finished(*this, current_animation_name);
        }
      }
      else {
        current_frame = next_frame;
        next_frame_date += get_frame_delay();
      }
      set_frame_changed(true);

      if (lua_context != NULL) {
        lua_context->sprite_on_frame_changed(*this, current_animation_name, current_frame);
      }
    }
  }
  else {
    // take the same frame as the other sprite
    if (synchronize_to->is_animation_finished()) {
      finished = true;
      if (lua_context != NULL) {
        lua_context->sprite_on_animation_finished(*this, current_animation_name);
      }
    }
    else {
      int other_frame = synchronize_to->get_current_frame();
      if (other_frame != current_frame) {
        current_frame = other_frame;
        next_frame_date = now + get_frame_delay();
        set_frame_changed(true);

        if (lua_context != NULL) {
          lua_context->sprite_on_frame_changed(*this, current_animation_name, current_frame);
        }
      }
    }
  }

  // update the special effects
  if (is_blinking()) {
    // the sprite is blinking

    while (now >= blink_next_change_date) {
      blink_is_sprite_visible = !blink_is_sprite_visible;
      blink_next_change_date += blink_delay;
    }
  }
}