-- /* Start File Header -------------------------------------------------------
-- Copyright (C) 2010 DigiPen Institute of Technology.
-- Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited.
-- File Name: player.lua
-- Purpose: Manages player action logic
-- Authors: Trevor Bennett - 20%
--			    Dan Rosas      - 20%
--          Robert Francis - 60%
-- End File Header --------------------------------------------------------*/

dofile "Resources\\lua\\defines.lua";

left = false;
right = false;

--Player moving
CreateKeyboardAction(VK_LEFT, "PlayerLeft");    --Left Arrow
CreateKeyboardAction(VK_RIGHT, "PlayerRight");  --Right Arrow
CreateKeyboardAction(VK_A, "PlayerLeft");		-- A
CreateKeyboardAction(VK_D, "PlayerRight");		-- D

--Player jumping
CreateKeyboardAction(VK_UP,    "PlayerJump");  --Up Arrow
CreateKeyboardAction(VK_W,    "PlayerJump");  -- W
CreateKeyboardAction(VK_SPACE, "PlayerJump");  -- Space
--CreateMouseAction(MOUSE_LEFT,  "PlayerJump");  --Left Click
--CreateKeyboardAction(VK_SPACE, "StartTime");  --Space

--Player viewing

RegisterForInput("MapZoom", "ToggleZoom", ACTION_TRIGGER);

function ToggleZoom()
  a = GOGetActive("PauseConfirm", 0);
  b = GOGetActive("ExitPause", 0);
  if a or b then
    return;
  end
  c = GetCamera();
  Camera_ToggleMinMaxZDistance(c);
  --Use Camera_SetMaxZDistance(c, float) and Camera_SetMinZDistance(c, float)
  --to set the min/max distance.  Default is 7.5/20
end


MoveType = 0;
isJumping = false;
direction = 1;
stop = 1;

RegisterForMessage("Physics", "PlayerMovement");

function PlayerMovement(type, dir)		
		MoveType = type;
		x1, y2, z3 = GOGetPos("Player", 0);
		
		if not IsPaused() then
				if type == 1 then			--walk
					PlaySound("FootStep");
					isJumping = false;
				elseif type == 2 then		--jump
					PlaySound("Note1");
				elseif type == 3 then		--bounce
					PlaySound("Bounce");
				end
		end
end

function PlayerJump()
  c = GetCamera();
  Camera_GoToMinZDistance(c);
	if isJumping == false then
		Jump("Player", 0, 0, 5.2);	-- Jump Height
		isJumping = true;
		ClearBlendQueue("Player", 0);
		PushBackBlend("Player", 0, "jump_1", 5, .5);
		PushBackBlend("Player", 0, "jump_2", 5);
	end
end

function PlayerLeft()
  c = GetCamera();
  Camera_GoToMinZDistance(c);
	left = true;
	if not right then
		Walk("Player", 0, -4, 6, 4.2);	  -- Run and Air Control Left
	end
end

function PlayerRight()
  c = GetCamera();
  Camera_GoToMinZDistance(c);
	right = true;
	if not left then
		Walk("Player", 0, 4, 6, 4.2);	  -- Run and Air Control Right
	end
end

function PlayerStopLeft()
	left = false;
	if not right then
		Walk("Player", 0, 0, 400.0, 0.0);	-- Landing traction
	end
end


function PlayerStopRight()
	right = false;
	if not left then
		Walk("Player", 0, 0, 400.0, 0.0);	-- Landing traction
	end
end

function DemoStop()
	StopSound("Moonlight");
end

function UpdateSun(name, inst)
  g = GetGraphics();
  c = GetCamera();
  tx, ty, tz = Camera_GetTar(c);
  GraphicsCore_SetSunPos(g, Vec3(tx + 9, ty + 8, tz - 5));
  GraphicsCore_SetSunLookAt(g, Vec3(tx, ty, tz));
end

function PlayerAnimation(name, inst)
  UpdateSun(name, inst);
 
	VX, VY, VZ = GOGetVelocity("Player", 0);
	isJumping = not (GetCompFloat("Player", 0, "Logic", "OnGround") > 0) and not (GetCompFloat("Player", 0, "Logic", "OnGround2") > 0);
	
			
	if VX > .3 then
		GORotUp("Player", 0, -90);
		if not isJumping then
			PushBackBlend("Player", 0, "run", 5);
		end
	elseif VX < -.3 then
  		GORotUp("Player", 0, 90);
  		if not isJumping then
  			PushBackBlend("Player", 0, "run", 5);
  		end
  	end
	
	b = GetCompBool("Player", 0, "Logic", "BounceUp") or GetCompBool("Player", 0, "Logic", "LeftWall") or GetCompBool("Player", 0, "Logic", "RightWall");
	if b then
		ClearBlendQueue("Player", 0);
		PushBackBlend("Player", 0, "jump_1", 5, .5);
		PushBackBlend("Player", 0, "jump_2", 5);
	end
		
	b = isJumping and not GetCompBool("Player", 0, "Logic", "Land") and not GetCompBool("Player", 0, "Logic", "Land2") and not GetCompBool("Player", 0, "Logic", "BounceUp")
	--if VY < .1 and VY > 0 and b then
		--PushBackBlend("Player", 0, "jump_3", 5, .5);
	--end
	if VY < 0 and b then
		PushBackBlend("Player", 0, "jump_4", 5);
	end
	
	b = GetCompBool("Player", 0, "Logic", "Land") and GetCompBool("Player", 0, "Logic", "Land2") and not GetCompBool("Player", 0, "Logic", "BounceUp")
	if b then
		PushBackBlend("Player", 0, "jump_5", 5, .5);
	elseif VX < .3 and VX > -.3 and not isJumping then
		PushBackBlend("Player", 0, "idle", 5);
	end

  x, y, z = GOGetPos("Player", 0);
  if y < -50 then
    RestartToCheckPoint();
  end
  
  --Walk("Player", 0, 5.5 * direction * stop, 3, 1); 

  min, max = MinMaxInstances("Tile2");
  for i = min, max, 1 do
    ExtraBlocks("Tile2", i, "grid");
  end

  min, max = MinMaxInstances("BounceTile");
  for i = min, max, 1 do
    ExtraBlocks("BounceTile", i, "grid");
    ExtraBlocks("BounceTile", i, "phyrotate");
  end
  
  min, max = MinMaxInstances("BounceTileLeft");
  for i = min, max, 1 do
    ExtraBlocks("BounceTileLeft", i, "grid");
    ExtraBlocks("BounceTileLeft", i, "phyrotate");
  end
  
  min, max = MinMaxInstances("BounceTileRight");
  for i = min, max, 1 do
    ExtraBlocks("BounceTileRight", i, "grid");
    ExtraBlocks("BounceTileRight", i, "phyrotate");
  end
  
  min, max = MinMaxInstances("Accelerator");
  for i = min, max, 1 do
    ExtraBlocks("Accelerator", i, "grid");
  end
  
  min, max = MinMaxInstances("Accelerator45");
  for i = min, max, 1 do
    ExtraBlocks("Accelerator45", i, "grid");
  end
  
  min, max = MinMaxInstances("Accelerator90");
  for i = min, max, 1 do
    ExtraBlocks("Accelerator90", i, "grid");
  end
  
  min, max = MinMaxInstances("Accelerator270");
  for i = min, max, 1 do
    ExtraBlocks("Accelerator270", i, "grid");
  end

end

function ExtraBlocks(name, inst, modelName)
  if timestop == true and IsGameObject(name, inst) then
    go = GetGOPointer(name, inst);
    DrawModel(modelName, go);
  end
end

function AddPlayerLight()
  p = GetGOPointer("Player", 0);
  GameObject_AddCompByName(p, "Light");
  SetCompString("Player", 0, "Light", "LightName", "test");
end
