/* Wii Shooting Gallery

	Written by Arikado
*/

	

#include "mainhead.h"	
	      
	//Functions
	
	void SaveHighscores(){
	
	fp = fopen("sd:/apps/WiiShootingGallery/data/scores.highscores","wb");
	
	if(fp){
	
	fwrite(&hscores[0], sizeof(hscores), 1, fp);
	
	}
	
	fclose(fp);
	
	}
	
	void LoadHighscores(){
	
	fp = fopen("sd:/apps/WiiShootingGallery/data/scores.highscores","rb");
	
	if(fp){
	
	fread(&hscores[0], sizeof(hscores), 1, fp);
	
	}
	
	}
	
	void SetHighScores(){
	
	switch(mode){
			
			case 1:
	        if(score > hscores[0])
			hscores[0] = score;
			break;
			
			case 2:
	        if(score > hscores[1])
			hscores[1] = score;
			break;
			
			case 3:
	        if(score > hscores[2])
			hscores[2] = score;
			break;
			
			case 4:
	        if(score > hscores[3])
			hscores[3] = score;
			break;
			
			case 5:
	        if(score > hscores[4])
			hscores[4] = score;
			break;
			
			case 6:
	        if(score > hscores[5])
			hscores[5] = score;
			break;
			
			case 7:
			
			switch(anim){
			
			case 1:
	        if(score > hscores[6])
			hscores[6] = score;
			break;
			
			case 2:
	        if(score > hscores[7])
			hscores[7] = score;
			break;
			
			case 3:
	        if(score > hscores[8])
			hscores[8] = score;
			break;
			
			case 4:
	        if(score > hscores[9])
			hscores[9] = score;
			break;
			
			}
			break;
			
			case 8:
			
			switch(anim){
			
			case 1:
	        if(score > hscores[10])
			hscores[10] = score;
			break;
			
			case 2:
	        if(score > hscores[11])
			hscores[11] = score;
			break;
			
			case 3:
	        if(score > hscores[12])
			hscores[12] = score;
			break;
			
			case 4:
	        if(score > hscores[13])
			hscores[13] = score;
			break;
			
			}
			break;
			
			}
	         }
			 
	void doPadPowerOff(s32 chan){	
	if (chan == WPAD_CHAN_0)
		SYS_ResetSystem(SYS_POWEROFF, 0, 0);

	return;
    }
		  
	void Menu(GameWindow *gwd){
	
	        int buttonx = 320 - Button[0]->GetWidth()/2;
		  
	        Button[0]->SetPosition(buttonx, 90);
			Button[1]->SetPosition(buttonx, 150);
			Button[2]->SetPosition(buttonx, 210);
			Button[3]->SetPosition(buttonx, 270);
			Button[4]->SetPosition(buttonx, 330);
			Button[5]->SetPosition(buttonx, 390);
			
			for(int i = 0; i < 6; i++){
			
			Arrow[i]->SetPosition(buttonx - (Arrow[i]->GetWidth()*2), Button[i]->GetY());
			
			}
			
			for(int i = 6; i < 12; i++){
			
			Arrow[i]->SetPosition(buttonx + Button[i - 6]->GetWidth() + Arrow[i]->GetWidth(), Button[i - 6]->GetY());
			
			}
			
			Target[0]->SetImage(target, 80, 80);
			buttonx = 320 - Target[0]->GetWidth()/2;
			Target[0]->SetPosition(buttonx, 5);
			
			print.setFont(font_ttf, font_ttf_size);
			Text.SetPosition(80, 0);
		    Text.SetImage(&print);
		  
		    Background->SetPosition(0, 0);
			
			buttonx = 320 - Start->GetWidth()/2;
			
			Start->SetPosition(buttonx, 35);
		  
		    while(true){
			
			WPAD_ScanPads();
			
			MBackground->Draw();
			
			if(Aim->CollidesWith(Button[2]) || Aim->CollidesWith(Arrow[2]) || Aim->CollidesWith(Arrow[8]))
			Background->Draw();
			
			print.setColor(Color::Color(0,0,255));
			print.setSize(18);
			
	        print.printf("             Wii Shooting Gallery\n\n\n\n\n");
			
			Target[0]->Draw();
			
			Start->Draw();
			
			print.setColor(Color::Color(0,0,0));
			
			for(int i = 0; i < 6; i++){
			Button[i]->SetImage(button);
			}
			
			if(Aim->CollidesWith(Arrow[0]) || Aim->CollidesWith(Arrow[6]))
			Button[0]->SetImage(button2);
			
			if(Aim->CollidesWith(Arrow[1]) || Aim->CollidesWith(Arrow[7]))
			Button[1]->SetImage(button2);
			
			if(Aim->CollidesWith(Arrow[2]) || Aim->CollidesWith(Arrow[8]))
			Button[2]->SetImage(button2);
			
			if(Aim->CollidesWith(Arrow[3]) || Aim->CollidesWith(Arrow[9]))
			Button[3]->SetImage(button2);
			
			if(Aim->CollidesWith(Arrow[4]) || Aim->CollidesWith(Arrow[10]))
			Button[4]->SetImage(button2);
			
			if(Aim->CollidesWith(Arrow[5]) || Aim->CollidesWith(Arrow[11]))
			Button[5]->SetImage(button2);
			
			
			for(int i = 0; i < 6; i++){
			Button[i]->Draw();
			}
			
			for(int i = 0; i < 12; i++){
			Arrow[i]->Draw();
			}
			
			switch(mode){
			
			  case 1:
			  print.printf("                Still Targets\n\n\n");
			  break;
			  
			  case 2:
			  print.printf("                Horizontal Moving Targets\n\n\n");
			  break;
			  
			  case 3:
			  print.printf("                Vertical Moving Targets\n\n\n");
			  break;
			  
			  case 4:
			  print.printf("                Crazy Targets\n\n\n");
			  break;
			  
			  case 5:
			  print.printf("                Crisscross Targets\n\n\n");
			  break;
			  
			  case 6:
			  print.printf("                Teleporting Targets\n\n\n");
			  break;
			  
			  case 7:
			  print.printf("                Hogans New Alley\n\n\n");
			  break; 
			  
			  case 8:
			  print.printf("                Wii Shoot In Space\n\n\n");
			  break;
			  
			}
			
			switch(trans){
			
			case 0:
			print.printf("                Crosshair 1\n\n\n");
			Aim->SetImage(crosshair1);
			Aim->SetTransparency(0xFF);
			break;
			
			case 1:
			print.printf("                Crosshair 2\n\n\n");
			Aim->SetImage(crosshair2);
			break;
			
			case 2:
			print.printf("                Crosshair 3\n\n\n");
			Aim->SetImage(crosshair3);
			break;
			
			case 3:
			print.printf("                Crosshair 4\n\n\n");
			Aim->SetImage(crosshair4);
			break;
			
			case 4:
			print.printf("                Crosshair 5\n\n\n");
			Aim->SetImage(crosshair5);
			Aim->SetTransparency(0xFF);
			break;
			
			case 5:
			print.printf("                Invisible Crosshair\n\n\n");
			Aim->SetTransparency(0);
			break;
			
			}
			
			switch(backgroundchoice){
			
			case 1:
			Background->SetTransparency(0xFF);
			Background->SetImage(background1);
			print.printf("                Background 1\n\n\n");
			break;
			
			case 2:
			Background->SetImage(background2);
			print.printf("                Background 2\n\n\n");
			break;
			
			case 3:
			Background->SetTransparency(0xFF);
			Background->SetImage(background3);
			print.printf("                Background 3\n\n\n");
			break;
			
			case 4:
			Background->SetTransparency(0);
			print.printf("                No Background\n\n\n");
			break;
			}
			
			if(mode < 7){
			switch(anim){
			case 1:
			print.printf("                Animation On\n\n\n");
			break;
			
			case 2:
			print.printf("                Animation Off\n\n\n");
			break;
			}
			}
			
			if(mode == 7 || mode == 8){
			switch(anim){
			case 1:
			print.printf("                Easy\n\n\n");
			break;
			
			case 2:
			print.printf("                Medium\n\n\n");
			break;
			
			case 3:
			print.printf("                Hard\n\n\n");
			break;
			
			case 4:
			print.printf("                Very Hard\n\n\n");
			break;
			
			}
			}
			
			switch(rumble){
			
			case 0:
			print.printf("                Rumble Off\n\n\n");
			break;
			
			case 1:
			print.printf("                Rumble On\n\n\n");
			break;
			}
			
			switch(timechoice){
			
			case 0:
			print.printf("                Unlimited Time\n\n\n");
			maxtime = 0;
			break;
			
			case 1:
			print.printf("                One minute\n\n\n");
			maxtime = 3529;
			break;
			
			case 2:
			print.printf("                Two Minutes\n\n\n");
			maxtime = 7059;
			break;
			
			case 3:
			print.printf("                Three Minutes\n\n\n");
			maxtime = 10588;
			break;
			
			case 4:
			print.printf("                Four Minutes\n\n\n");
			maxtime = 14118;
			break;
			
			case 5:
			print.printf("                Five Minutes\n\n\n");
			maxtime = 17647;
			break;
			
			}
			
			print.setColor(Color::Color(0,255,0));
			
			print.printf("Score: %d", score);
			
			switch(mode){
			
			case 1:
	        print.printf("                         High Score: %d", hscores[0]);
			break;
			
			case 2:
	        print.printf("                         High Score: %d", hscores[1]);
			break;
			
			case 3:
	        print.printf("                         High Score: %d", hscores[2]);
			break;
			
			case 4:
	        print.printf("                         High Score: %d", hscores[3]);
			break;
			
			case 5:
	        print.printf("                         High Score: %d", hscores[4]);
			break;
			
			case 6:
	        print.printf("                         High Score: %d", hscores[5]);
			break;
			
			case 7:
			
			switch(anim){
			
			case 1:
	        print.printf("                        High Score: %d", hscores[6]);
			break;
			
			case 2:
	        print.printf("                        High Score: %d", hscores[7]);
			break;
			
			case 3:
	        print.printf("                        High Score: %d", hscores[8]);
			break;
			
			case 4:
	        print.printf("                        High Score: %d", hscores[9]);
			break;
			
			}
			break;
			
			case 8:
			
			switch(anim){
			
			case 1:
	        print.printf("                        High Score: %d", hscores[10]);
			break;
			
			case 2:
	        print.printf("                        High Score: %d", hscores[11]);
			break;
			
			case 3:
	        print.printf("                        High Score: %d", hscores[12]);
			break;
			
			case 4:
	        print.printf("                        High Score: %d", hscores[13]);
			break;
			
			}
			break;
			
			}
			
			
		    ir_t ir;
		    WPAD_IR(WPAD_CHAN_0, &ir);
		    Aim->SetPosition(ir.sx-WSP_POINTER_CORRECTION_X, ir.sy-WSP_POINTER_CORRECTION_Y);
		    Aim->Move(-((f32)Aim->GetWidth()/2), -((f32)Aim->GetHeight()/2));
		    Aim->SetRotation(ir.angle/2);
		    Aim->Draw();
			
			print.flush();
			
			Text.Draw();
			
			print.clear();
			
			print.reset();
			
			 
			 if(mode < 1)
			 mode = 8;
			 
			 if(mode > 8)
			 mode = 1;
			 
			 if(trans > 5)
			 trans = 0;
			 
			 if(trans < 0)
			 trans = 5;
			 
			 if(backgroundchoice > 4)
			 backgroundchoice = 1;
			 
			 if(backgroundchoice < 1)
			 backgroundchoice = 4;
			 
			 if(timechoice > 5)
			 timechoice = 0;
			 
			 if(timechoice < 0)
			 timechoice = 5;
			 
			 if(mode < 7){
			 if(anim > 2)
			 anim = 1;
			 if(anim < 1)
			 anim = 2;
			 }
			 
			 if(mode == 7 || mode == 8){
			 if(anim > 4)
			 anim = 1;
			 if(anim < 1)
			 anim = 4;
			 }
			 
			 if(rumble < 0)
			 rumble = 1;
			 if(rumble > 1)
			 rumble = 0;
			 
			 if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_B){
			 if(Aim->CollidesWith(Arrow[0]))
			 mode--;
			 if(Aim->CollidesWith(Arrow[1]))
			 trans--;
			 if(Aim->CollidesWith(Arrow[2]))
			 backgroundchoice--;
			 if(Aim->CollidesWith(Arrow[3]))
			 anim--;
			 if(Aim->CollidesWith(Arrow[4]))
			 rumble--;
			 if(Aim->CollidesWith(Arrow[5]))
			 timechoice--;
			 if(Aim->CollidesWith(Arrow[6]))
			 mode++;
			 if(Aim->CollidesWith(Arrow[7]))
			 trans++;
			 if(Aim->CollidesWith(Arrow[8]))
			 backgroundchoice++;
			 if(Aim->CollidesWith(Arrow[9]))
			 anim++;
			 if(Aim->CollidesWith(Arrow[10]))
			 rumble++;
			 if(Aim->CollidesWith(Arrow[11]))
			 timechoice++;
			 }
			 
			 if((WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_B) && (Aim->CollidesWith(Start)))
			 break;
			 
			 if((WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_PLUS) || (WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_CLASSIC_BUTTON_PLUS)){
			 quit = 1;
			 break;
			 }
			 
			 if((WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_HOME) || (WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_CLASSIC_BUTTON_HOME)){
			 homemenu(gwd);
			 }
			 
			 Target[0]->NextFrame();
			 
			 gwd->Flush();
		  }
		 }
		  
		  bool CheckForHit(){
		  
		  for(int i = 0; i < 5; i++){
		  
		  if(Aim->CollidesWith(Target[i])){
		  if(talive[i] == true){
		  talive[i] = false;
		  score++;
		  if(rumble == 1){
		  WPAD_Rumble(WPAD_CHAN_0, 1);
		  rumblecounter = 1;
		  }
		  return true;
		   }
		  }
		 }
		 return false;
		}
		 
		
		 void SpawnTarget(){
		 
		 int placement = 0;//FOR MODES 2 AND 3 AND 5
		 
		 for(int i = 0; i < 5; i++){
		 
		 if(talive[i] == false){
		 talive[i] = true;
		 
		 if(mode == 1 || mode == 4 || mode == 6){
		 x[i] = rand() % 560 + 1;//Changes made for target boundary
		 y[i] = rand() % 380 + 1;//Changes Made for target boundary
		 }
		 
		 dir[i] = rand() % 2 + 1;
		 
		 if(mode == 2){
		 if(dir[i] == 1)
		 x[i] = 0;
		 if(dir[i] == 2)
		 x[i] = 640;
		 
		 placement = rand() % 5 + 1;
		 
		 if(placement == 1)
		 y[i] = 100;
		 
		 if(placement == 2)
		 y[i] = 200;
		 
		 if(placement == 3)
		 y[i] = 300;
		 
		 if(placement == 4)
		 y[i] = 400;
		 
		 if(placement == 5)
		 y[i] = 500;
		 }
		 
		 if(mode == 3){
		 if(dir[i] == 1)
		 y[i] = 0;
		 if(dir[i] == 2)
		 y[i] = 480;
		 
		 placement = rand() % 5 + 1;
		 
		 if(placement == 1)
		 x[i] = 100;
		 
		 if(placement == 2)
		 x[i] = 200;
		 
		 if(placement == 3)
		 x[i] = 300;
		 
		 if(placement == 4)
		 x[i] = 400;
		 
		 if(placement == 5)
		 x[i] = 500;
		 }
		 
		 if(mode == 5){
		 
		 dir[i] = rand() % 4 + 1;
		 
		 if(dir[i] == 1)
		 y[i] = 0;
		 if(dir[i] == 2)
		 y[i] = 480;
		 if(dir[i] == 3)
		 x[i] = 0;
		 if(dir[i] == 4)
		 x[i] = 640;
		 
		 placement = rand() % 5 + 1;
		 
		 if(dir[i] == 1 || dir[i] == 2){
		 
		 if(placement == 1)
		 x[i] = 100;
		 
		 if(placement == 2)
		 x[i] = 200;
		 
		 if(placement == 3)
		 x[i] = 300;
		 
		 if(placement == 4)
		 x[i] = 400;
		 
		 if(placement == 5)
		 x[i] = 500;
		 }
		 
		 if(dir[i] == 3 || dir[i] == 4){
		 
		 if(placement == 1)
		 y[i] = 100;
		 
		 if(placement == 2)
		 y[i] = 200;
		 
		 if(placement == 3)
		 y[i] = 300;
		 
		 if(placement == 4)
		 y[i] = 400;
		 
		 if(placement == 5)
		 y[i] = 500;
		 
		 }
		 
		 }
		 
		 if(mode == 6)
		 counter[i] = rand() % 6 + 1;//Set the teleport counter
		 
		 animdir[i] = rand() % 2 + 1;
		 
		 CheckTarget();
		 
		 Target[i]->SetPosition(x[i], y[i]);
		  
		 }
		}
	   }
	   
	   void MoveTarget(){
	   
	   if(mode == 2){
	   
	   for(int i = 0; i < 5; i++){
	   
	   if(dir[i] == 1)
	   x[i] += tspeed[i];
	   
	   if(dir[i] == 2)
	   x[i] -= tspeed[i];
	   
	   }
	   }
	   
	   if(mode == 3){
	   
	   for(int i = 0; i < 5; i++){
	   
	   if(dir[i] == 1)
	   y[i] += (tspeed[i] + 1);
	   
	   if(dir[i] == 2)
	   y[i] -= (tspeed[i] - 1);
	   
	   }
	   }
	   
	   if(mode == 4){
	   
	   for(int i = 0; i < 5; i++){
	   
	   int lastchancetomove = 0;
	   
	   lastchancetomove = rand() % 30 + 1;
	   
	   if(lastchancetomove == 3)
	   dir[i] = rand() % 8 + 1;
	   
	   if(dir[i] == 1)
	   y[i] -= (tspeed[i]);
	   
	   if(dir[i] == 2)
	   y[i] += (tspeed[i]);
	   
	   if(dir[i] == 3)
	   x[i] -= tspeed[i];
	   
	   if(dir[i] == 4)
	   x[i] += tspeed[i];
	   
	   if(dir[i] == 5){
	   y[i] -= tspeed[i]/2;
	   x[i] -= tspeed[i]/2;
	   }
	   
	   if(dir[i] == 6){
	   y[i] -= tspeed[i]/2;
	   x[i] += tspeed[i]/2;
	   }
	   
	   if(dir[i] == 7){
	   y[i] += tspeed[i]/2;
	   x[i] -= tspeed[i]/2;
	   }
	   
	   if(dir[i] == 8){
	   y[i] += tspeed[i]/2;
	   x[i] -= tspeed[i]/2;
	   }
	   }
	   }
	   
	   if(mode == 5){
	   
	   for(int i = 0; i < 5; i++){
	   
	   if(dir[i] == 1)
	   y[i] += (tspeed[i]);
	   
	   if(dir[i] == 2)
	   y[i] -= (tspeed[i]);
	   
	   if(dir[i] == 3)
	   x[i] += tspeed[i];
	   
	   if(dir[i] == 4)
	   x[i] -= tspeed[i];
	   
	   }
	   }
	   
	   for(int i = 0; i < 5; i++){
	   
	   bool check1 = false;//Horrible glitch without this
	   
	   if(x[i] < 0)
	   x[i] = 640;
	   
	   if(x[i] > 640)
	   x[i] = 0;
	   
	   if(y[i] < 0)
	   y[i] = 480;
	   
	   if(y[i] > 480){
	   y[i] = 0;
	   check1 = true;
	   }
	   
	   if(mode != 4){
	   if(x[i] == 0 || x[i] == 640 || check1 == true || y[i] == 480){
	   int newspeed = 0;
	   newspeed = rand() % 2 + 1;
	   if(newspeed == 1)
	   tspeed[i] = rand() % 10 + 6;
	   int newpos = 0;
	   newpos = rand() % 10 + 1;
	   if(newpos == 3)
	   talive[i] = false;
	   }
	   }
	   
	   }
	   
	   }
	   
	   void DrawHealth(Sprite *Health, int health){
	   
	   Health->SetPosition(500, 0);
	   Health->Draw();
	   if(health >= 2){
	   Health->SetPosition(520, 0);
	   Health->Draw();
	   }
	   if(health >= 3){
	   Health->SetPosition(540, 0);
	   Health->Draw();
	   }
	  }
	  
	  void TeleportTarget(){
	  
	  for(int i = 0; i < 5; i++){
	  
	  if(counter[i] == 40){
	  x[i] = rand() % 640 + 1;
	  y[i] = rand() % 480 + 1;
	  CheckTarget();
	  Target[i]->SetPosition(x[i], y[i]);
	  counter[i] = 0;
	  }
	  
	  counter[i]++;
	  
	  }
	  }
	  
	 void CheckTarget(){

     if(x[0] == x[1] && y[0] == y[1]){  
	 x[0] = rand() % 640 + 1;
	 y[0] = rand() % 480 + 1;
	 }
	 
	 if(x[0] == x[2] && y[0] == y[2]){  
	 x[0] = rand() % 640 + 1;
	 y[0] = rand() % 480 + 1;
	 }
	 
	 if(x[0] == x[3] && y[0] == y[3]){  
	 x[0] = rand() % 640 + 1;
	 y[0] = rand() % 480 + 1;
	 }
	 
	 if(x[0] == x[4] && y[0] == y[4]){  
	 x[0] = rand() % 640 + 1;
	 y[0] = rand() % 480 + 1;
	 }
	 
	 if(x[1] == x[2] && y[1] == y[2]){  
	 x[1] = rand() % 640 + 1;
	 y[1] = rand() % 480 + 1;
	 }
	 
	 if(x[1] == x[3] && y[1] == y[3]){  
	 x[1] = rand() % 640 + 1;
	 y[1] = rand() % 480 + 1;
	 }
	 
	 if(x[1] == x[4] && y[1] == y[4]){  
	 x[1] = rand() % 640 + 1;
	 y[1] = rand() % 480 + 1;
	 }
	 
	 if(x[2] == x[3] && y[2] == y[3]){  
	 x[2] = rand() % 640 + 1;
	 y[2] = rand() % 480 + 1;
	 }
	 
	 if(x[2] == x[4] && y[2] == y[4]){  
	 x[2] = rand() % 640 + 1;
	 y[2] = rand() % 480 + 1;
	 }
	 
	 if(x[3] == x[4] && y[3] == y[4]){  
	 x[3] = rand() % 640 + 1;
	 y[3] = rand() % 480 + 1;
	 }
	 }

    void TargetGames(GameWindow *gwd){
	
	for(int i = 0; i < 5; i++){
	tspeed[i] = 6;
	}
	
	for(;;)
	{
		
		if(mode == 8)
		break;
		
		WPAD_ScanPads();
		
		Background->Draw();
		
		DrawHealth(Health, health);
		
		if(mode != 1 && mode != 6)
		MoveTarget();
		
		if(mode == 6)
		TeleportTarget();
		
		SpawnTarget();
		
		for(int i = 0; i < 5; i++){
		
		if(talive[i] == true){
		Target[i]->SetPosition(x[i], y[i]);
		Target[i]->Draw();
		if(anim == 1){
		if(animdir[i] == 2)
		Target[i]->NextFrame();
		if(animdir[i] == 1)
		Target[i]->PrevFrame();
		}
		}
		
		}
		
		if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_HOME)
		homemenu(gwd);
		
		if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_PLUS)
		break;
			
		// Infrared calculation - The X and Y of the wiimote sprite
		ir_t ir; // The struct for infrared
		WPAD_IR(WPAD_CHAN_0, &ir); // Let's get our infrared data
		// Give our sprite the positions and the angle.
		Aim->SetPosition(ir.sx-WSP_POINTER_CORRECTION_X, ir.sy-WSP_POINTER_CORRECTION_Y); // We use these constants to translate the position correctly to the screen
		Aim->Move(-((f32)Aim->GetWidth()/2), -((f32)Aim->GetHeight()/2)); // And these to make our image appear at the center of this position.
		Aim->SetRotation(ir.angle/2); // Set angle/2 to translate correctly
		
		Aim->Draw();
		
		if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_B){
		if(!CheckForHit())
		health--;
		}
		
		if(rumblecounter == 7){
		WPAD_Rumble(WPAD_CHAN_0, 0);
		rumblecounter = 0;
		}
		
		if(rumblecounter != 0)
		rumblecounter++;
		
		if(maxtime > 0)
		timeing++;
		
		gwd->Flush();
		
		if(health == 0)
		break;
		
		if(timeing > maxtime)
		break;
		
	}
   }


    int main(int argc, char **argv){

	
	fatInitDefault();//Initialize the SanDisk for loading images
	
	// Create the game window and initalise the VIDEO subsystem
	GameWindow *gwd = new GameWindow();
	gwd->InitVideo();
	
	WPAD_Init(); //Initialize the Wiimote
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);//Allows us to use the Wiimote pointer
	WPAD_SetPowerButtonCallback(doPadPowerOff);//Set up the power button
	
    crosshair1->LoadImage("/apps/WiiShootingGallery/data/crosshair1.png");
	crosshair2->LoadImage("/apps/WiiShootingGallery/data/crosshair2.png");
	crosshair3->LoadImage("/apps/WiiShootingGallery/data/crosshair3.png");
	crosshair4->LoadImage("/apps/WiiShootingGallery/data/crosshair4.png");
	crosshair5->LoadImage("/apps/WiiShootingGallery/data/crosshair5.png");
	
	target->LoadImage("/apps/WiiShootingGallery/data/target.png");
	target2->LoadImage("/apps/WiiShootingGallery/data/target2.png");
	
	bullet->LoadImage("/apps/WiiShootingGallery/data/bullet.png");
	
	background1->LoadImage("/apps/WiiShootingGallery/data/background1.png");
	background2->LoadImage("/apps/WiiShootingGallery/data/background2.png");
	background3->LoadImage("/apps/WiiShootingGallery/data/background3.png");
	
	button->LoadImage("/apps/WiiShootingGallery/data/button.png");
	button2->LoadImage("/apps/WiiShootingGallery/data/button2.png");
	
	arrow->LoadImage("/apps/WiiShootingGallery/data/arrow.png");
	
	mbackground->LoadImage("/apps/WiiShootingGallery/data/mbackground.png");
	MBackground->SetImage(mbackground);
	
	start->LoadImage("/apps/WiiShootingGallery/data/start.png");
	Start->SetImage(start);
	
	Health->SetImage(bullet);
	
	srand(time(0));//Start random number generator
	
	print2.setFont(font_ttf, font_ttf_size);
	Text2.SetPosition(80, 0);
	Text2.SetImage(&print2);
	print2.setColor(Color::Color(0,0,255));
	print2.setSize(18);
	Text2.SetPosition(0, 0);
	
	for(int i = 0; i < 5; i++){
	Target[i] = new Sprite();
	x[i] = 0;
	y[i] = 0;
	talive[i] = false;
	}
	
	for(int i = 0; i < 6; i++){
	Button[i] = new Sprite();
	Button[i]->SetImage(button);
	}
	
	for(int i = 0; i < 6; i++){
	Arrow[i] = new Sprite();
	Arrow[i]->SetImage(arrow);
	}
	
	for(int i = 6; i < 12; i++){
	Arrow[i] = new Sprite();
	Arrow[i]->SetImage(arrow);
	Arrow[i]->SetTransform(1);
	}
	
	LoadHighscores();
	
	while(true){
	
	health = 3;
	timeing = 0;
	Menu(gwd);
	Background->SetPosition(0, 0);
	
	switch(anim){
	
	case 1:
	for(int i = 0; i < 5; i++){
	Target[i]->SetImage(target, 80, 80);
	}
	break;
	
	case 2:
	for(int i = 0; i < 5; i++){
	Target[i]->SetImage(target2);
	}
	break;
	}
	
	
	score = 0;
	
	if(quit == 1)
	break;

	for(;;)
	{
	
	    if(mode < 7){
		TargetGames(gwd);
		break;
		}
	
	    if(mode == 7){
		score = hogansnewalley(Aim, Health, Background, rumble, maxtime, anim, gwd);
		break;
		}
		
		if(mode == 8){
		score = shooter(Aim, Health, Background, rumble, maxtime, anim, gwd);
		break;
		}
		
		
		
	}
	//deinitialise target variables to prevent code dumps
	if(mode < 7){
	for(int i = 0; i < 5; i++){
	x[i] = 0;
	y[i] = 0;
	dir[i] = 0;
	counter[i] = 0;
	animdir[i] = 0;
	talive[i] = false;
	}
	 }
	
	SetHighScores();
	SaveHighscores();
   }
	return 0;
}

