//===============================================
// SpaceShip.cpp
//===============================================


#include "DragonFireSDK.h"

// Left and Right Button ID's:
#define Left 1
#define Right 2
// Events:
#define TouchDown 1
#define TouchMove 2
#define TouchUp 3
// Offset for Bullet to center on the ship.
#define BulletXOffset 23;

// Keeps track of the "world's" Y coordinate.
int GlobalY;
// Sets the ships velocity.
int DeltaY;

// Touch handles:
int AfterburnerTouch;
int LeftTouch;
int RightTouch;
int FireTouch;

// Image handles:
int BackdropImage;
int BackdropView;
int ShipImage;
int ShipBurnersImage;
int ShipView;

// Ship's coordinates:
int ShipY = 400;
int ShipX = 130;
int L;
int R;

int BulletID = 0;
int Bullets[25];

//===============================================
int OnTouchAfterburn(int id, int evt, int x, int y)
{
  
if(evt==1)
   {
      ViewSetImage(ShipView,ShipBurnersImage);
      DeltaY=10;
   }
  
if(evt==3)
   {
      ViewSetImage(ShipView,ShipImage);
      DeltaY=2;
   }

   printf(
"Afterburn touched! X: %d Y: %d\n", x, y);
  
return 0;
}

//===============================================
int OnTouchLeftRight(int id, int evt, int x, int y)
{
  
if (id==Left)
      L=1;

  
if (id==Right)
      R=1;

  
if (evt==3)
   {
      L=0;
      R=0;
   }
  
return 0;
}

//===============================================
int OnTouchFire(int id, int evt, int x, int y)
{
  
if (evt==1)
   {
      SoundPlay(Fire);
      int BulletX = ViewGetx(ShipView) + BulletXOffset;
      ViewSetxy(Bullets[BulletID], BulletX, 409);
      ViewSetVisible(Bullets[BulletID],
true);
      printf(
"Fire touched! X: %d Y: %d\n", x, y);
      BulletID++;
        
if(BulletID==25)
            BulletID=0;
   }
  
return 0;
}

//===============================================
void AppMain()
{
   DeltaY=2;
   GlobalY = -4800;

   AfterburnerTouch = TouchAdd(0, 0, 320, 75, OnTouchAfterburn, 0);
   LeftTouch = TouchAdd(0, 330, 50, 150, OnTouchLeftRight, Left);
   RightTouch = TouchAdd(270, 330, 50, 150, OnTouchLeftRight, Right);
   FireTouch = TouchAdd(50, 430, 220, 50, OnTouchFire, 0);

   BackdropImage = ImageAdd(
"Background.png");
   ShipImage = ImageAdd(
"Ship.png");
   ShipBurnersImage = ImageAdd(
"ShipBurners.png");
   Fire = SoundAdd(
"FireSound.wav");
   Jet = SoundAdd(
"Jet.wav");
   j=26;

   BackdropView = ViewAdd(BackdropImage, 0, GlobalY);
   ViewAdd("Overlay.png", 0, 0);

  
for (int i = 0; i < 25; i++)
   {
      Bullets[i] = ViewAdd(
"Bullet.png", 0, 0);
      ViewSetVisible(Bullets[i],
false);
   }

   ShipView = ViewAdd(ShipImage, ShipX, ShipY);
}

//===============================================
void OnTimer()
{
  
//Scroll the Background
   GlobalY += DeltaY;
     
if (GlobalY>=0)
         GlobalY=-4800;
   ViewSetxy(BackdropView, 0, GlobalY);

  
//Move the Ship left or right
   ShipX = ViewGetx(ShipView);

  
if (L==1)
      ShipX -= 7;

  
if (R==1)
      ShipX += 7;

  
if (ShipX < 0)
      ShipX = 0;

  
if (ShipX > 260)
      ShipX = 260;

   ViewSetxy(ShipView, ShipX, ShipY);

  
// Move the Bullets
  
int BulletX;
  
int BulletY;

  
for (int i = 0; i < 25; i++)
   {
      BulletX = ViewGetx(Bullets[i]);
      BulletY = ViewGety(Bullets[i]);
     
if (BulletY > -24)
      {
         ViewSetxy(Bullets[i], BulletX, BulletY - 10);
      }
   }

  
//Jet Sound Loop
  
if(j)
   {
      --j;

     
if(j==0)
      {
         SoundPlay(Jet);
         j=26;
      }
   }

}

//===============================================