//---------------------------------------------------//
/*
 *Copyright Bryce Thorup, William Thorup, BitJag 2014
 *
 * Contact: contact@bitjag.com
 *
 *All content developed by BitJag (graphics & Code) is
 *for your personal use only.  Feel free to burn it to a CD, 
 *flash to a SkunkBoard, or use in an emulator.  We 
 *hope you also take the chance to learn from it if needed.
 * 
 *Do not redistribute, duplicate, charge for "handling" 
 *or cost to print, or charge for profit.
 * 
 *If there is someone you know who wants a copy,
 *physical or digital, please direct them to our website.
 *Any other questions about the use of the demo, please
 *shoot us an email.  We will be liberal, promise.
 *
 * http://bitjag.com/
 * 
 *This demo was developed with the Removers Library,
 *which is used under the LGPL licence 2.1, included
 *with the source.  The Removers library is free to 
 *download and use at this location.
 * 
 * http://removers.free.fr/softs/download.php?lang=en
 *
//---------------------------------------------------*/

#include <jagdefs.h>
#include <jagtypes.h>
#include <stdlib.h> 
#include <sprite.h>
#include <joypad.h>

#include "bird_motion.h"
 
 //Motion function
  int birdy_move(sprite *bird, bird_fly *bird_move, int flap){
    
    //Fall down
    if(bird_move->up_or_down == 0){
      
      bird_move->delay += 1;
      
      bird_move->begin = (bird_move->delay) / (bird_move->durration);
      
      bird->y = (bird_move->begin) + bird->y;
      
    }
            
    //The following if statements change the sprite frames to make it look like the bird is rotating downwards.  
    if(bird_move->delay >= -10 && bird_move->delay <= 0 && bird_move->up_or_down == 1){
      
      bird->animation_data.index = 2;
      
    }
      
    if(bird_move->delay >= -20 && bird_move->delay <= -11 && bird_move->up_or_down == 1){
      
      bird->animation_data.index = 1;
      
    }
      
    if(bird_move->delay <= -21 && bird_move->up_or_down == 1){
      
      bird->animation_data.index = 0;
      
    }


    //Fly up - This is run after the "up_or_down" varible has been set by the users button press.
    if(bird_move->up_or_down == 1){
      
      bird_move->delay -= 1; //This is the count down for how long we let the bird rise.
      
      bird_move->begin = (bird_move->delay) / (bird_move->durration); //Ease out motion code for upward movement to simulate the pull of gravity on rising objects.
      
      bird->y = -(bird_move->begin) + bird->y;
	      
    }
      
    //The following if statements change the sprite frames to make it look like the bird is rotating upwards.  
    if(bird_move->delay <= 25 && bird_move->delay >= 21 && bird_move->up_or_down == 1){
      
      bird->animation_data.index = 2;
      
    }
      
    if(bird_move->delay <= 20 && bird_move->delay >= 13 && bird_move->up_or_down == 1){
      
      bird->animation_data.index = 3;
      
    }
      
    if(bird_move->delay <= 12 && bird_move->delay >= 0 && bird_move->up_or_down == 1){
      
      bird->animation_data.index = 4;
      
    }
    
    //Fly up
    if(flap == 1){
      
      bird_move->delay = bird_move->height; //Set delay to the predefined hieght so the bird flies as high as we want to.
      
      bird_move->controller_delay = 0; //Set the controller delay back to 0, so, if the user releases the button, they can press again to gain altitude.
      
      bird_move->up_or_down = 1;  //Set the direction to up
      
    }
    
  }
  
  
