playMusic("menu.mp3");
setOverlay(-1);

gravity.x=0;
gravity.y=50;

var enemydistance=470;
var enemyspeed=2;
var enemeyswitchx=true;
var enemeyswitchy=false;
var switchcount=0;
var breakoutcounter=0;
var switchemotioncounter=0;
var spawnchild = false;
var challenge = 0;
var viruscounter=6;
var enemycounter=4;
Utils.TEXTTIMEOUT = 6000;


setGravity(objects.flipper1, false);
setGravity(objects.flipper2, false);

//say("welcome",objects.ball);
//say("enemy",objects.enemy);
setMaterial(objects.ball, "rubber");

GameScript.data.update = function(object, time) {
		//left & right gate
		sine(object, time, 50, 4, 0, MODE_LOOP, true, true);
	}
	
GameScript.data.stop = function(object1, object2) {
	stopMove(objects.ball);
}



// she is here
GameScript.data.sheishere = function(object1, object2) 
{
	if (challenge<1)
		{
		//stopMove(objects.ball);
		say("Finaly, outline 2013. Time to kick those PC sceners their poepgat [ass].|Lets get inside and release our 16 bit demo");
		challenge=1;
		}
}

// enter room
GameScript.data.enterroom = function(object1, object2) {
	if (challenge < 2){
		//stopMove(objects.ball);
		say("Hello old famous scener, your coder Motorola is still sleeping. Just move up to reach him", objects.shifter);
		challenge=2;
		}
	}	
	
// Jail text
GameScript.data.jail = function(object1, object2) {
	if (challenge < 2){
		//stopMove(objects.ball);
		say("Hmm .. my coder is near by. I can sense his 68000 8 mhz in the last room.");
		challenge=3;
		}
	}
	

	

	
// Save me
GameScript.data.saveme = function(object1, object2) {
	if (challenge < 4){
		//stopMove(objects.ball);
		say("Destory the evil snorig old duplicated men in to free me!", objects.ballgirl);
		challenge=4;
	}
}	



// breakout block	
GameScript.data.blokje = function(object1, object2) {
		if (object2.type == BodyType.DYNAMIC) return;
		if (object2 == objects.ball2) return;
		addScore(1);
		breakoutcounter = breakoutcounter+1;
		setColor(object2, 0xffff00);
		object2.gravMass *= 1;	
		if (breakoutcounter==10)
		{
			//say("Bloody breakout completed|Acces granted",objects.breakoutpaddle)
			var ballx=object2.position.x;
			var bally=object2.position.y;
			destroyBody(objects.breakoutfreeball);
			destroyBody(objects.virusgate);
			//follow(objects.ballgirl);
			say("Thank you for saving me, now we can nag the compoteam about the non-working network", objects.ballgirl);
			destroyBody(objects.slaapdeur2);
			challenge=5;
		}
	}
	

	
GameScript.data.breakoutdead = function(object1, object2) {
	//trace("TEST", object1, object2);
		if (object1==objects.ball) return;
		if (object1==objects.ballgirl) return;
		destroyBody(object1);
		
	}
	
GameScript.data.virus = function(object1, time) {
	//circle(object1, time, 32, 4, 0, MODE_LOOP, true, true);
	cosine(object1, time, 450, 1, 0, MODE_LOOP, true, false);
	sine(object1, time, 50, 5, 0, MODE_LOOP, false, true);
}

GameScript.data.switchstate = function(object1, object2) {
	stopMove(object1);
	switchcount = switchcount +1;
	say("switch"+ switchcount, objects.ball);
	if (switchcount == 2){
		say("unlocked", objects.exit);
		objects.exit.type=BodyType.DYNAMIC;
		objects.exit.gravMass *= -10;
	}
}

// movement of enemy
GameScript.data.enemymovement = function(object1, time) {
	if (objects.enemy.type == BodyType.DYNAMIC) return;
	object1.angularVel = 1.5;
	abs_sine(object1, time, enemydistance, enemyspeed, 0, MODE_LOOP, true, false);
	//abs_circle(object1, time, enemydistance, enemyspeed, 0, MODE_LOOP, enemeyswitchx, enemeyswitchy);
}




function getSine(time) {
	return Math.sin(time * Math.PI / 180.0);
}

function rotateObject(body, angle) {
	var olda = body.rotation;
	body.angularVel = (angle - olda)*25;
}

function rotateObjectReverse(body, angle) {
	var olda = body.rotation;
	body.angularVel = (olda - angle)*-25;
}

// hier staat het bram	
GameScript.data.rotate3 = function(object1, time) {
	rotateObject(object1, ( -30 * getSine(time*2)) * 0.0175);
}
	

GameScript.data.rotate2 = function(object1, time) {
	rotateObject(object1, ((30 * getSine(time*2)) * 0.0175));
}

GameScript.data.rotate4 = function(object1, time) {
	rotateObject(object1, ((360 * getSine(time*2)) * 0.0175));
}

GameScript.data.fan = function(object1, time) {
	if (challenge <2) rotateObject(object1, (time*-0.05));
}

GameScript.data.fan2 = function(object1, time) {
	if (challenge >5) rotateObject(object1, (time*-0.05));
}

GameScript.data.fanreverse = function(object1, time) {
	rotateObjectReverse(object1, (time*0.05));
}

GameScript.data.paddle = function(object1, time) {
	abs_sine(object1, time, 160, 2, 0, MODE_LOOP, true, false);
}

GameScript.data.switchemotion = function(object1, object2) 
{
		if (object1 == objects.ballgirl) 
		{
			if (challenge <7 )
			{
			say("Lets get physical if we want to continue",objects.ballgirl);
			//follow(objects.ball);
			challenge=7;
			return;
			}
		}
		
		


}

GameScript.data.jointeam = function(object1, object2) 
{
		//say("zeg" + object1.userData["name"] );
		if (object1 == objects.ball)
		{		
			//if (challenge < 8)
			//{
			say("2 x 16 = 32 bits, let us tear down the PC wall",objects.ball);
			var ballx=objects.ball.position.x;
			var bally=objects.ball.position.y;
			destroyBody(objects.ball);
			destroyBody(objects.ballgirl);
			createCircle(ballx, bally, 20, "ball");
			//challenge=8;
			destroyBody(objects.wall);
			return;
			//}
			
		}
}

	// Curry 9
GameScript.data.wantcurry = function(object1, object2) {
	if (challenge < 8){
		//stopMove(objects.ball);
		say("Excuse me, remember to pre-order your curry", objects.shifter);
		//challenge=9;
		}
	}


// Enter the pit  10
GameScript.data.enterthepit = function(object1, object2) {
		if (challenge < 10)
		{
		stopMove(objects.ball);
		say("||||I need to fix this backstage and then sell  chickens to outlines for their curry");
		say("|||Tok tok Tok, i am MOD .. to drunk to Tok Tooook Tok",objects.mod);
		say("||Tok tok Tok",objects.compo3);
		say("|Yes, it does not run on our 64 bits emulators",objects.compo2);
		say("Compo team here, there is a bug in your demo",objects.compo1);
		challenge=10;
		}
	}

// Feel it
GameScript.data.canyoufeelit = function(object1, object2) {
	if (challenge < 11)
	{
	stopMove(objects.ball);
	say("Backstage .. firewall on, shields up, engage", objects.ball);
	challenge=11;
	}
}	
	
// Warning
GameScript.data.warning = function(object1, object2) {
	if (challenge < 12)
	{
	stopMove(objects.ball);
	say("|Big overheaded mouth, infecting your system dll is enough.", objects.ball);
	say("Hold, trespasser!, I am Steve of Ballmer. Upgrade to windows 8 or we will terminate.", objects.enemy);
	challenge=12;
	}
}	
	


// bounce flipper
GameScript.data.bounce = function(object1, object2) {
	var velx=object1.velocity.x;b
	var vely=object1.velocity.y;
	stopMove(object1)
	object1.velocity.x = -2 * velx
	object1.velocity.y = -2 * vely
	}
	
// bounce paddle
GameScript.data.bouncepaddle = function(object1, object2) {
	//var velx=object1.velocity.x;
	//var vely=object1.velocity.y;
	//stopMove(object1)
	//object1.velocity.x = -2 * velx
	//object1.velocity.y = -2 * vely
	if (object1==objects.ball) return;
	if (object1==objects.ballgirl) return;
	destroyBody(object1);
	}
	
// bounce the ball	
GameScript.data.bounce = function(object1, object2) {
	//say("bounce");
	//object1.velocity.x *=100; 
	//object1.velocity.y *=5;
	}
	

// hit of the small balls challenge 10 & 11
GameScript.data.joinballs = function(obj1, obj2) {
	if (challenge < 14)
	{
	var radius=getRadius(obj1);
	var radius2=getRadius(obj2);
	radius = radius + radius2;
	destroyBody(obj1);
	destroyBody(obj2);
	createCircle(obj2.position.x-(radius/2), obj2.position.y-(radius/2), radius, "ball");
	//createCircle(400, 400, radius, "ball");
	follow(objects.ball);
	if(!enemyspeed == 5) {enemyspeed=5;}
	say("Alert!|We are under attack", objects.enemy);
	challenge=challenge + 1;
	if (challenge == 14) {
	say("DLL disabled, you are doomed steve", objects.ball);
	}
	}
}	

// hit and dead of the enemy
GameScript.data.hit = function(object1, object2) 
{
	if (challenge < 15)
	{
		if (objects.enemy.type == BodyType.DYNAMIC) return;	
		if (objects.ball.mass>2.5) 
		{
			// enemy die
			var ballx=object2.position.x;
			var bally=object2.position.y;
			objects.enemy.type=BodyType.DYNAMIC;
			objects.enemy.gravMass *= -2;
			//createCircle(ballx, bally, 8, "ball");
			spawnchild=true;
			say("Nooo .. i'm loosing all my votes. Pouet.net and scene.org are drained.|New school is removed from the scene history. AAaaaaah", objects.enemy);
			challenge=15;
			destroyBody(objects.stevebalk);
		}

	if (object1==objects.ball) 
		{
			enemycounter = enemycounter -1;
			if (!enemycounter == 1) say("This is draining my ram");
			if (enemycounter == 1) say("I will be dead next time");
			if (enemycounter == 0) reset(false);
		}
	}
}
	





//deze functie is voor de eieren, onhit//
var stopped = false;
GameScript.data.hitchild = function(object1, object2)
 {
 	if (stopped == true) return;
	//destroyBody(object1);
	if (object1 == objects.ball) 
		{
		destroyBody(object2);
		var radius=getRadius(object1);
		var radius2=getRadius(object2);
		radius = radius + radius2;
		
			if (radius <140)
			{
			var ballx=object1.position.x;//-(radius/2);
			var bally=object1.position.y;//-(radius/2);
			
			ballx = (ballx * 0.9 + 52.7);
			bally = (bally * 0.9 + 177.7);
			//527, 1777
			
			destroyBody(object1);
			destroyBody(object2);
			createCircle(ballx, bally, radius, "ball");
			}
	
			if (radius >140 && (!stopped))
			{
				stopped = true;
				say("You won every vote on the planet but diversity is the core of the scene");
				setTimeout(function() {
					Utils.LEVELNAME="./levels/MainMenu.svg";
					reset(false);
				}, 100);
			}
	}	
	//follow(objects.ball);	
}

function makeCircle() 
{
	var circle = createCircle(objects.enemy.position.x, objects.enemy.position.y, Math.random()*5+8, "ballchild");
	//var circle = objects.ballchild;
	//circle.position.x = 400;
	//circle.position.y = 0;
	makeScriptTrigger(circle);
	circle.userData["hit"] = "hitchild"; //assign function, andere properties werken ook
	//follow(objects.ball);
}

GameScript.data.hitvirus = function(object1, object2) {
	viruscounter = viruscounter -1;
	if (viruscounter > 0) say("I am not drunk, i am running on windows 98",objects.mod);
	if (viruscounter == 1) say("I will be dead next time");
	if (viruscounter == 0) reset(false);
}

GameScript.data.hitmod = function(object1, object2) {
	viruscounter = viruscounter -1;
	if (viruscounter > 0) say("I am not drunk, i am running on windows 98",objects.mod);
	if (viruscounter == 1) say("Warning: Touch one more time and die, objects.mod");
	if (viruscounter == 0) reset(false);
}

CustomScript.create([
	'if (spawnchild) makeCircle();',
	'delay(5);',
	'goto(1)'
]);