Ejemplo n.º 1
0
void Sprite::update() {
	int next_frame;
	uint32_t now = System::now();

	//only check for animation with multiple frames.
	while(!finished && now >= next_frame_date && get_frame_delay() > 0) {
		next_frame = get_next_frame();
	    if(next_frame == -1) {
			this->finished = true;
	    } else {
		    current_frame = next_frame;
			next_frame_date += get_frame_delay();
	    }
	}
}
Ejemplo n.º 2
0
void Sprite::set_current_frame(int frame) {
	finished = false;
	next_frame_date = System::now() + get_frame_delay();

	frame_changed = (current_frame != frame);
	this->current_frame = frame;
}
Ejemplo n.º 3
0
/**
 * \brief Suspends or resumes the animation.
 *
 * Nothing is done if the parameter specified does not change.
 *
 * \param suspended true to suspend the animation, false to resume it
 */
void Sprite::set_suspended(bool suspended) {

  if (suspended != this->suspended && !ignore_suspend) {
    this->suspended = suspended;

    // compte next_frame_date if the animation is being resumed
    if (!suspended) {
      uint32_t now = System::now();
      next_frame_date = now + get_frame_delay();
      blink_next_change_date = now;
    }
    else {
      blink_is_sprite_visible = true;
    }

    // Also suspend or resumed the transition effect and the movement if any.
    Transition* transition = get_transition();
    if (transition != NULL) {
      transition->set_suspended(suspended);
    }

    Movement* movement = get_movement();
    if (movement != NULL) {
      movement->set_suspended(suspended);
    }
  }
}
Ejemplo n.º 4
0
/**
 * \brief Sets the current frame of the sprite's animation.
 *
 * If the animation was finished, it is restarted.
 * If the animation is suspended, it remains suspended
 * but the specified frame is drawn.
 *
 * \param current_frame The current frame.
 * \param notify_script \c true to notify the Lua sprite if any.
 * Don't set notify_script to \c false unless you do the notification
 * yourself later.
 */
void Sprite::set_current_frame(int current_frame, bool notify_script) {

  finished = false;
  next_frame_date = System::now() + get_frame_delay();

  if (current_frame != this->current_frame) {
    this->current_frame = current_frame;
    set_frame_changed(true);

    if (lua_context != NULL) {
      lua_context->sprite_on_frame_changed(
          *this, current_animation_name, current_frame);
    }
  }
}
Ejemplo n.º 5
0
/**
 * \brief Pauses or resumes the animation.
 *
 * Nothing is done if the parameter specified does not change.
 *
 * \param paused true to pause the animation, false to resume it
 */
void Sprite::set_paused(bool paused) {

  if (paused != this->paused) {
    this->paused = paused;
 
    // compte next_frame_date if the animation is being resumed
    if (!paused) {
      uint32_t now = System::now();
      next_frame_date = now + get_frame_delay();
      blink_next_change_date = now;
    }
    else {
      blink_is_sprite_visible = true;
    }
  }
}
Ejemplo n.º 6
0
void new_ship( char *t )
{
  Sint32 frames;
  Sint32 delay;
  Sint32 speed;
  Sint32 x, y;

  x = screen_width() / 2;
  y = screen_height() - 50;

  frames = get_num_frames( t );
  delay = get_frame_delay( t );
  speed = get_speed( t );
  ship.hp = get_hp( t ) * PLAYER_HP_FACTOR; 
  ship.hp_max = ship.hp;
  ship.type = strdup( t );

  ship.left = new_sprite_array( x, y, frames, get_left( t ), delay );
  if( ship.left == NULL ) { 
    fprintf( stderr, "Error creating new sprite: %s.\n", SDL_GetError() );
    exit( 1 );
  }
  set_speed( ship.left, speed );
  no_off_screen( ship.left );

  ship.right = new_sprite_array( x, y, frames, get_right( t ), delay );
  if( ship.right == NULL ) { 
    fprintf( stderr, "Error creating new sprite: %s.\n", SDL_GetError() );
    exit( 1 );
  }
  set_speed( ship.right, speed );
  no_off_screen( ship.right );

  ship.center = new_sprite_array( x, y, frames, get_center( t ), delay );
  if( ship.center == NULL ) { 
    fprintf( stderr, "Error creating new sprite: %s.\n", SDL_GetError() );
    exit( 1 );
  }
  set_speed( ship.center, speed );
  no_off_screen( ship.center );

  ship.current = ship.center;
  ship_center();
}
Ejemplo n.º 7
0
void new_enemy( Sint32 x, Sint32 y, const char *t )
{
  /* Paths to directions. */
  Sint32 frames;
  Sint32 delay;
  Uint32 speed;
  
  ENEMY *e;
   
  /* Create new ENEMY and add it to the list. */
  e = (ENEMY*) malloc( sizeof(ENEMY) );

  e->flags = 0;
  e->next = head;
  head = e;

  /* Set defaults. */
  e->hp = 1;
  e->type = "no_type";
  e->move_funct = NULL;
  speed = 1;
  delay = 100;
  frames = 1;
  memset( e->move_vars, -1, NUM_MOVE_VARS );

  frames = get_num_frames( t );
  delay = get_frame_delay( t );
  speed = get_speed( t );
  e->hp = get_hp( t );
  e->type = strdup( t );
  e->move_funct = get_move_funct( t );

  /* Make the left facing sprite */
  e->left = new_sprite_array( x, y, frames, get_left( t ), delay );
  if( e->left == NULL ) {
    fprintf( stderr, "Error creating new sprite: %s.\n",
              SDL_GetError() );
    exit(1);
  }
  set_speed( e->left, speed ); 

  /* Make the left facing sprite */
  e->right = new_sprite_array( x, y, frames, get_right( t ), delay );
  if( e->right == NULL ) {
    fprintf( stderr, "Error creating new sprite: %s.\n",
              SDL_GetError() );
    exit(1);
  }
  set_speed( e->right, speed ); 

  /* Make the left facing sprite */
  e->center = new_sprite_array( x, y, frames, get_center( t ), delay );
  if( e->center == NULL ) {
    fprintf( stderr, "Error creating new sprite: %s.\n",
              SDL_GetError() );
    exit(1);
  }
  set_speed( e->center, speed ); 
  
  e->current = e->center;
  enemy_center( e );

  num_enemies++;
}
Ejemplo n.º 8
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;
    }
  }
}