/**
 * \brief Returns the rectangle representing the specified frame on the source image.
 * \param frame a frame number
 * \return the rectangle of this frame
 */
const Rectangle& SpriteAnimationDirection::get_frame(int frame) const {

  if (frame < 0 || frame >= get_nb_frames()) {
    Debug::die(StringConcat() << "Invalid frame " << frame
        << ": this direction has " << get_nb_frames() << " frames");
  }
  return frames[frame];
}
/**
 * \brief Returns the rectangle representing the specified frame on the source image.
 * \param frame a frame number
 * \return the rectangle of this frame
 */
const Rectangle& SpriteAnimationDirection::get_frame(int frame) const {

  if (frame < 0 || frame >= get_nb_frames()) {
    std::ostringstream oss;
    oss << "Invalid frame " << frame
        << ": this direction has " << get_nb_frames() << " frames";
    Debug::die(oss.str());
  }
  return frames[frame];
}
/**
 * \brief Calculates the bit fields representing the non-transparent pixels
 * of the images in this direction.
 *
 * This method has to be called if you want a sprite having this animations
 * to be able to detect pixel-perfect collisions.
 * If the pixel-perfect collisions are already enabled, this function does nothing.
 *
 * \param src_image the surface containing the animations
 */
void SpriteAnimationDirection::enable_pixel_collisions(Surface& src_image) {

  if (!are_pixel_collisions_enabled()) {
    for (int i = 0; i < get_nb_frames(); i++) {
      pixel_bits.emplace_back(src_image, frames[i]);
    }
  }
}
/**
 * \brief Calculates the bit fields representing the non-transparent pixels
 * of the images in this direction.
 *
 * This method has to be called if you want a sprite having this animations
 * to be able to detect pixel-perfect collisions.
 * If the pixel-perfect collisions are already enabled, this function does nothing.
 *
 * \param src_image the surface containing the animations
 */
void SpriteAnimationDirection::enable_pixel_collisions(Surface* src_image) {

  if (!are_pixel_collisions_enabled()) {
    for (int i = 0; i < get_nb_frames(); i++) {
      pixel_bits.push_back(new PixelBits(*src_image, frames[i]));
    }
  }
}
/**
 * \brief Returns the size of a frame.
 * \return The size of a frame.
 */
Size SpriteAnimationDirection::get_size() const {

  Debug::check_assertion(get_nb_frames() > 0, "Invalid number of frames");
  return { frames[0].get_width(), frames[0].get_height() };
}
Esempio n. 6
0
/**
 * \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;
    }
  }
}
Esempio n. 7
0
/**
 * \brief Returns true if the last frame is reached.
 * \return true if the last frame is reached
 */
bool Sprite::is_last_frame_reached() const {

  return get_current_frame() == get_nb_frames() - 1;
}