RequireSystemScript('colors.js')


function game()
{

	// Define our two polygons: red P1 and blue P2
	var P1 = [{x:0,y:30}, {x:60,y:0}, {x:120,y:30}, {x:60,y:120}, {x:40,y:70} ];
	var P2 = [{x:30+90,y:10+90}, {x:60+90,y:10+90}, {x:60+90,y:60+90}, {x:30+90,y:60+90}];

	var dx,dy; // Delta X and Y for mouse to polygon translation
	var wobble = 0;
	var wobble_prev = 0;
	var result = 0;

	// Set Initial Mouse position to the first point of Polygon 1
	SetMousePosition(P1[0].x,P1[0].y)

	// Big loop!
	while(!IsAnyKeyPressed())
	{

		// This is one way to do it, you can also use offsets instead of
		// modifying the polygons.

		// update the polygon1 position using the mouse position X-axis
		if( dx = (P1[0].x - GetMouseX()) ){
			for(var i= P1.length-1;i>=0;--i){
				P1[i].x -= dx;
			}
		}
		
		// update the polygon1 position using the mouse position Y-axis
		if( dy = (P1[0].y - GetMouseY()) ){
			for(var i= P1.length-1;i>=0;--i){
				P1[i].y -= dy;
			}
		}

		// Make one side wobble a bit, just for fun.
		wobble_prev = wobble; 
		wobble = 5* Math.sin(GetTime()/1000);
		P1[2].x += (wobble - wobble_prev);
		P1[0].x -= (wobble - wobble_prev);


		// Draw the two polygons on the canvas and then display the rest
		// (draw blue first, as we move red with the mouse)
		Polygon(P2, Blue);
		Polygon(P1, Red);

		// Get the collision result
		result = PolygonCollision(P1,P2);
		
		// If its colliding, draw a yellow circle around the colliding point
		// Unfortunately, if there are multiple colliding points, we just see the first.
		if(result){
			if(result >0)
				var point = P1[result -1];
			else
				var point = P2[-result -1];
			Point(point.x, point.y, Yellow);
			OutlinedCircle(point.x, point.y, 4, Yellow, true);
		}
		GetSystemFont().drawText(0,GetScreenHeight()-10, "Polygon collision="+ result );
		FlipScreen();

		GarbageCollect(1); // agressively GarbageCollect! (not required, it's to test Engine stability)

	}
	
}
