//===============================================
// App.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)
	{
	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("Images/Background.png");
	ShipImage	  = ImageAdd("Images/Ship.png");
	ShipBurnersImage	  = ImageAdd("Images/ShipBurners.png");

	BackdropView = ViewAdd(BackdropImage, 0, GlobalY);

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

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

void AppExit()
{

}

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

	//WorldSetxy(0, GlobalY);
    //if (!LeftRightPressed)
	//    ViewSetxy(ShipView, ShipX, ShipY);

	//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); 
		}
	}

	//ok we can also move the ship with tilt
	 int tx=0,ty=0;
	tx = TiltGetx();
    if((tx>0)&&(ShipX<360))
       ShipX+=2;
	else if ((tx < 0)&& (ShipX>6))
		ShipX-=2;
	
      ty =TiltGety();
	  if((ty>0)&&(ShipY<260))
		  ShipY+=2;
	  else if((ty<0)&&(ShipY>6))
		  ShipY-=2;

	ViewSetxy(ShipView,ShipX,ShipY); //
}
//===============================================

