/*--------------------------------------------------------------------------------------------------------

Example #1 - Moving character.

--------------------------------------------------------------------------------------------------------*/
#include "graphics.h"
#include "mouse.h"
#include "keyboard.h"
#include "enemy.h"
#include "player.h"
#include "healthkit.h"
#include "bullet.h"
#include "sequencer.h"
#include "stage.h"
#include "explodies.h"
#include "bg.h"
#include "sound.h"
#include "rocket.h"
#include "startPage.h"

// Variables
int score;
int highscore;
int fading=0;
sxSound MusicSound;



extern startPage startVariables;


extern gxFont Font;

// Init function - called once at the beginning, initializes the display and loads any files.
void Init()
{
	// Switch the display from the desktop to 800x600 mode and capture the mouse.
	gxInit( 800, 600, true );
	sxInit();
	msInit();
	kbInit();

	// Load a font.
	gxLoadFont( "font.raw", &Font, 648, 12, 2 );
	sxLoadWav ( "bgmusic.wav", &MusicSound );
	sxPlaySoundLooping ( &MusicSound );
	
	InitStartPage();
	InitStars();
	InitStage();
	InitSequencer ();
	InitRocket();
	InitBullet ();
	InitHealthkit ();
	InitEnemies ();
	InitExplodies();
	InitPlayer ();
}

// Exit function - called when the program exits, releases sprites and restores the desktop.
void Exit()
{

	// Restore the mouse and display to windows.  Note reverse order.
	kbDeinit();
	msDeinit();
	gxDeinit();
} 



// Update function - called repeatedly by the framework, does runtime processing.  Returns 0 to exit.

void UpdateHealth ()
{
	// Health Points
	gxDrawText(10, 10, "HEALTH:", &Font, gxRGB32(255,255,255), 0);
	gxDrawInt(70, 10, player.health, &Font, gxRGB32(255,255,255), 0);
}

void UpdateLose ()
{
	//Lose Screen
	if (player.health <= 0)
	{
		gxDrawTextScaled(100, 256, "YOU LOSE! HAHA PWNT!", &Font, gxRGB32(255,0,0), 0, 4);
		gxDrawTextScaled(270, 330, "(CLICK TO RESTART)", &Font, gxRGB32(255,255,255), 0, 2);
		if ( highscore < score )
		{
			highscore = score;
		}
		if ( msButton1 && !msOldButton1 )
		{
			score = 0;
			player.health = 100;
			player.X = 0;
			player.Y = 300-32;
			currentStage = 0;
			currentEvent = 0;
			rocketsLeft = 3*startVariables.levelNumber;
			for(int i=0;i<NUM_ENEMIES;i++)
				enemies[i].active=false;
		}
	}
}

void UpdateHighScore ()
{
	// HighScore
	gxDrawTextScaled(650, 10, "HIGH SCORE:", &Font, gxRGB32(255,255,255), 0, 1);
	gxDrawInt(760, 10, highscore, &Font, gxRGB32(255,255,255), 0);
	if ( highscore < score )
	{
		gxDrawTextScaled(280, 10, "NEW HIGH SCORE!", &Font, gxRGB32(255,255,255), 0, 2);
	}
}

void UpdateScore ()
{
	// Score
	gxDrawTextScaled(650, 30, "SCORE:", &Font, gxRGB32(255,255,255), 0, 1);
	gxDrawInt(760, 30, score, &Font, gxRGB32(255,255,255), 0);
}

void UpdateFade()
{
	if(player.health>1)
		fading=0;
	else
	{
		if(fading<255)
			fading+=5;
		gxDrawRectAlpha(0, 0, gxScreenWidth, gxScreenHeight, gxRGBA32(0, 0, 0, fading));
	}


}

int Update()
{
	// Update the status of the mouse and keyboard.
	msUpdateMouse(); 
	kbUpdateKeys();

	// Clear the screen.
	gxDrawRect( 0, 0, gxScreenWidth, gxScreenHeight, gxRGB32(0,0,0) );

	UpdateStartPage();


	if(startVariables.start)
	{
		UpdateSequencer ();
		UpdateStars();
		UpdateStage();
		UpdateRocket();
		UpdateBullet ();
		UpdateHealthkit ();
		UpdateEnemies ();
		UpdateExplodies();
		UpdatePlayer ();
		UpdateFade();
		UpdateLose ();
		UpdateHealth ();
		UpdateHighScore ();
		UpdateScore ();
	}


	// Present the drawing surface to the player.
	gxUpdateScreen();

	// If Escape or Mouse Button 1 is pressed, exit.
	if ( kbKeyStatus[KB_ESCAPE] )
		return 0;

	return 1;
}