#include "scripts/common.txt"
#include "scripts/bpong/main.txt"

BPong::Main @bPong = null;
int menuID = 0;
int menuItem = 0;
int difficulty = 1;
bool inMenu = true;
//------------------------------------------------------------------------------------------------------------------------
// Start
//------------------------------------------------------------------------------------------------------------------------
$script 0 {
	Game.PlaySound("sounds/shaders/generic_242.ksnd");
	@bPong = BPong::Main();
	bPong.ShowTurok();
    Game.CallDelayedMapScript(1, instigator, 0);
}
//------------------------------------------------------------------------------------------------------------------------
// Menu Update
//------------------------------------------------------------------------------------------------------------------------
void MenuUpdate() {
	uint16 buttons = Player.Buttons();
	
	if (menuID == 0) {
		Game.PrintLine("Jump to Start Game", 0, 20);
		Game.PrintLine(" ", 1, 20);
		Game.PrintLine("WASD to move cursor", 2, 20);
	} else if (menuID == 1) {
		Game.PrintLine(menuItem == 1 ? "-Exit" : "Exit", 0, 20);
		Game.PrintLine(menuItem == 0 ? (difficulty == 0 ? "-Easy Normal Hard" : difficulty == 1 ? "Easy -Normal Hard" : "Easy Normal -Hard") : "Easy Normal Hard", 1, 20);
	}
	
	//Game.PrintLine(Game.GetLocalizedText("$str_179"), 1);
	
	if ((buttons & BC_FORWARD) != 0) {
		if (menuID == 1) {
			Game.PlaySound("sounds/shaders/generic_89_jump_swish_1.ksnd");
			menuItem++;
			if (menuItem > 1) {
				menuItem = 0;
			}
			delay(0.25f);
		}
	} else if ((buttons & BC_BACKWARD) != 0) {
		if (menuID == 1) {
			Game.PlaySound("sounds/shaders/generic_89_jump_swish_1.ksnd");
			menuItem--;
			if (menuItem < 0) {
				menuItem = 1;
			}
			delay(0.25f);
		}
	}
	
	if ((buttons & BC_STRAFELEFT) != 0) {
		if (menuID == 1 and menuItem == 0) {
			Game.PlaySound("sounds/shaders/generic_89_jump_swish_1.ksnd");
			difficulty--;
			if (difficulty < 0) {
				difficulty = 2;
			}
		}
		delay(0.25f);
	}
	if ((buttons & BC_STRAFERIGHT) != 0) {
		if (menuID == 1 and menuItem == 0) {
			Game.PlaySound("sounds/shaders/generic_89_jump_swish_1.ksnd");
			difficulty++;
			if (difficulty > 2) {
				difficulty = 0;
			}
		}
		delay(0.25f);
	}
	
	if ((buttons & BC_JUMP) != 0) {
		delay(0.25f);
		if (menuID == 0) {
			menuID = 1;
			Game.PlaySound("sounds/shaders/generic_168.ksnd"); //ooook then
		}
		else if (menuID == 1) {
			if (menuItem == 0) {
				bPong.HideTurok();
				Game.PlaySound("sounds/shaders/campainger_shorting_out.ksnd"); //campainger laugh
				inMenu = false;
				bPong.Start(difficulty);
			} else if (menuItem == 1) {
				Game.Restart();
			}
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Pong Update
//------------------------------------------------------------------------------------------------------------------------
$script 1 {
	bPong.MandatoryUpdate();
	if (inMenu) {
		MenuUpdate();
	} else {
		bPong.Update();
		//If game is over then exit or restart
		if (!bPong.IsRunning()) {
			if (bPong.UserQuit()) {
				PlayLoop.ChangeMap("levels/level47.map");
				return;
			} else if (bPong.Player1Won()) {
				bPong.HideBalls();
				Game.PlaySound("sounds/shaders/generic_234.ksnd"); //I am turok!
				Game.PrintLine("VICTORY", 0, 100);
				delay(2.0f);
				bPong.End();
				bPong.ShowTurok();
				inMenu = true;
			} else if (bPong.Player2Won()) {
				bPong.HideBalls();
				Game.PlaySound("sounds/shaders/campainger_rage.ksnd"); //the universe is mine!
				Game.PrintLine("GAME OVER", 0, 180);
				delay(4.0f);
				bPong.End();
				bPong.ShowTurok();
				inMenu = true;
			}
		}
	}
    $restart;
}
//------------------------------------------------------------------------------------------------------------------------
