-- Exercise scripting a bit in our test-level

-- write out that we loaded sucessfully (more or less, anyway)
io.write("Starting script for Level1 in BlinkenLUA / ",_VERSION,"!\n")

GLOBALS = {};

-- Required function: Initializing the level
function initLevel ()
	SetFreeMovement(1);

	GLOBALS.gfx = {};
	GLOBALS.obj = {};
	GLOBALS.snd = {};
	GLOBALS.player = {};
	GLOBALS.computer = {};
	GLOBALS.ball = {};

	-- Graphics and Objects
	GLOBALS.gfx.hitter = LoadGFX("hitter.bmp");
	GLOBALS.obj.playerh = AddObject(GLOBALS.gfx.hitter, 1, 1, 0, 1, 0, 0);
	GLOBALS.obj.computerh = AddObject(GLOBALS.gfx.hitter, 1, 1, 0, 1, 0, 0);
	GLOBALS.gfx.ball = LoadGFX("ball.bmp");
	GLOBALS.obj.ball = AddObject(GLOBALS.gfx.ball, 1, 1, 0, 1, 0, 0);
	GLOBALS.gfx.computer = LoadGFX("computer1.bmp");
	GLOBALS.obj.computer = AddObject(GLOBALS.gfx.computer, 1, 1, 0, 1, 1, 0);

	-- Sound
	GLOBALS.snd.killed = LoadSoundFX("killed.ogg");
	GLOBALS.snd.wall = LoadSoundFX("wall.ogg");
	GLOBALS.snd.hitter = LoadSoundFX("hitter.ogg");

	GLOBALS.player.pixels = 0;

	initPositions();

end

function initPositions ()
	-- Init ball
	GLOBALS.ball.x = 304;
	GLOBALS.ball.y = 224;
	GLOBALS.ball.vx = 2.0;
	GLOBALS.ball.vy = -1.0;
	GLOBALS.ball.vy = 0;

	-- Init Computer
	GLOBALS.computer.y = 224;
	GLOBALS.computer.vy = 0;

	-- Init Player
	GLOBALS.player.y = 224;
	SetPlayerY(GLOBALS.player.y);
	SetCollectedPixelCount(GLOBALS.player.pixels);
	SetRequiredPixelCount(3);

end

-- Required function: Physics-loop for scripting part
function scriptPhysics ()
	SetFreeMovement(1);
	GLOBALS.player.y = GetPlayerY();

	-- Computer Physics
		-- Move in direction of ball and slow down if in limits
	if GLOBALS.ball.y < (GLOBALS.computer.y - 30) and GLOBALS.ball.vy < 0 then
		GLOBALS.computer.vy = GLOBALS.computer.vy - 0.05;
	else
		if GLOBALS.ball.y > (GLOBALS.computer.y + 30) and  GLOBALS.ball.vy > 0 then
			GLOBALS.computer.vy = GLOBALS.computer.vy + 0.05;
		else
			GLOBALS.computer.vy = GLOBALS.computer.vy * 0.94;
		end
	end 
		-- Limit speed
	if GLOBALS.computer.vy < -5 then
		GLOBALS.computer.vy = -5;
	end
	if GLOBALS.computer.vy > 5 then
		GLOBALS.computer.vy = 5;
	end

	GLOBALS.computer.y = GLOBALS.computer.y +  GLOBALS.computer.vy;
	if GLOBALS.computer.y < 32 then
		GLOBALS.computer.y = 32;
		GLOBALS.computer.vy = 0;
	end
	if GLOBALS.computer.y > 416 then
		GLOBALS.computer.y = 416;
		GLOBALS.computer.vy = 0;
	end


	-- Ball physics
	--        Y Axis
	GLOBALS.ball.y = GLOBALS.ball.y + GLOBALS.ball.vy;
	if GLOBALS.ball.y < 0 then
		GLOBALS.ball.y = 0;
		GLOBALS.ball.vy = GLOBALS.ball.vy * -1;
		PlaySoundFX(GLOBALS.snd.wall);
	end
	if GLOBALS.ball.y > 448 then
		GLOBALS.ball.y = 448;
		GLOBALS.ball.vy = GLOBALS.ball.vy * -1;
		PlaySoundFX(GLOBALS.snd.wall);
	end


	--       X Axis
	GLOBALS.ball.x = GLOBALS.ball.x + GLOBALS.ball.vx;


	--       Player side
	if GLOBALS.ball.x > 576 then
		if GLOBALS.ball.y > (GLOBALS.player.y - 64) and GLOBALS.ball.y < (GLOBALS.player.y + 64) then
			GLOBALS.ball.x = 576;
			GLOBALS.ball.vx = GLOBALS.ball.vx * -1;
			PlaySoundFX(GLOBALS.snd.hitter);
			vector =  (GLOBALS.ball.y - GLOBALS.player.y)/32;
			GLOBALS.ball.vy = GLOBALS.ball.vy + vector;
		else
			KillPlayer();
			--PlaySoundFX(GLOBALS.snd.killed);
			initPositions();
		end
	end

	--       Computer side
	if GLOBALS.ball.x < 32 then
		if GLOBALS.ball.y > (GLOBALS.computer.y - 64) and GLOBALS.ball.y < (GLOBALS.computer.y + 64) then
			GLOBALS.ball.x = 32;
			GLOBALS.ball.vx = GLOBALS.ball.vx * -1;
			PlaySoundFX(GLOBALS.snd.hitter);
			vector =  (GLOBALS.ball.y - GLOBALS.computer.y)/32;
			GLOBALS.ball.vy = GLOBALS.ball.vy + vector;
		else
			PlaySoundFX(GLOBALS.snd.killed);
			GLOBALS.player.pixels = GLOBALS.player.pixels + 1;
			SetCollectedPixelCount(GLOBALS.player.pixels);
			initPositions();
		end
	end


	if GLOBALS.ball.x < 0 then
		GLOBALS.ball.x = 0;
		GLOBALS.ball.vx = GLOBALS.ball.vx * -1;
		PlaySoundFX(GLOBALS.snd.wall);
	end
	if GLOBALS.ball.x > 608 then
		GLOBALS.ball.x = 608;
		GLOBALS.ball.vx = GLOBALS.ball.vx * -1;
		PlaySoundFX(GLOBALS.snd.wall);
	end

	-- Update object positions
	SetObjPos(GLOBALS.obj.playerh, 608, GLOBALS.player.y - 32);
	SetObjPos(GLOBALS.obj.computerh, 0, GLOBALS.computer.y - 32);
	SetObjPos(GLOBALS.obj.computer, 0, GLOBALS.computer.y);
	SetObjPos(GLOBALS.obj.ball, GLOBALS.ball.x, GLOBALS.ball.y);


end

-- Required function: Callback on "Action"-Button on FG-Objects
function handleAction (obj_id)

end

