Script.health = 100
Script.maxHealth = 100
Script.mouseSensitivity = 15
Script.camSmoothing = 	2
Script.moveSpeed = 2.5
Script.speedMultiplier = 1.5
Script.strafeSpeed = 4
Script.playerHeight = 1.8
Script.eyeheight=0.8
Script.input={}
Script.mouseDifference = Vec2(0,0)
Script.playerMovement = Vec3(0,0,0)

Script.energy=100

--This function will be called when an entity is loaded in a map.  Use this for intitial setup stuff.
function Script:Start()
	
	self.camRotation = self.entity:GetRotation(true)

	--create jetpack sound source
	local jetpackSound = Sound:Load("Sound/MSC Nov 16 Sound Effects/Jet Pack Loop 3.wav")
	self.jetpackSource = Source:Create()
	self.jetpackSource:SetSound(jetpackSound)
	self.jetpackSource:SetLoopMode(true)
	self.jetpackSource:SetVolume(0)
	self.jetpackSource:Play()

	self.entity:SetPickMode(0)	--don't shoot yourself
	
	--Set the type for this object to player
	self.entity:SetKeyValue("type","player")
	
	--Create a camera
	self.camera = Camera:Create()
	self.camera:SetFOV(80)
	self.camera:SetRange(0.05,1000)
	self.camera:SetMultisampleMode((System:GetProperty("multisample","1")))
	
	--Set the camera's rotation to match the player
	self.camera:SetRotation(self.entity:GetRotation(true))
	
	--Set the camera position at eye height
	self.camera:SetPosition(self.entity:GetPosition(true)+Vec3(0,self.eyeheight,0))
	
	--Create listener
	self.listener = Listener:Create(self.camera)	
	

	---------------------------------------------------------------------------
	--We want the player model visible in the editor, but invisible in the game
	--We can achieve this by creating a material, setting the blend mode to make
	--it invisible, and applying it to the model.
	---------------------------------------------------------------------------
	local material = Material:Create()
	material:SetBlendMode(5)--Blend.Invisible
	self.entity:SetMaterial(material)
	material:Release()
	self.entity:SetShadowMode(0)
	
	local window = Window:GetCurrent()
	local context = Context:GetCurrent()
	window:SetMousePosition(Math:Round(context:GetWidth()/2), Math:Round(context:GetHeight()/2))
	
	self.disk = self.entity:FindChild("spinfusor"):FindChild("disk")
	self.weapon = Pivot:Create()	--detach weapon to move it separately
	self.weapon:SetPosition(self.camera:GetPosition(true),true)
	self.weapon:SetRotation(self.camera:GetRotation(true),true) 
	self.entity:FindChild("spinfusor"):SetParent(self.weapon)

	self.jetpackParticles = self.entity:FindChild("particles")
	tolua.cast(self.jetpackParticles,"Emitter")
	self.jetpackParticles:Pause()

	self.camera:SetRotation(self.camRotation)

	self.mainFont=Font:Load("Fonts/arial.ttf",50)
end


function Script:UpdateWorld()
	local window = Window:GetCurrent()
	local context=Context:GetCurrent()
	
	if window:KeyHit(Key.P) then	--enable/disable debug view
		if self.debugCameraMode then
			self.camera:SetDebugPhysicsMode(false)
		else
			self.camera:SetDebugPhysicsMode(true)
		end
		self.debugCameraMode=not self.debugCameraMode
	end
	
	--Mouse look
	self.currentMousePos = window:GetMousePosition()
	window:SetMousePosition(Math:Round(context:GetWidth()/2), Math:Round(context:GetHeight()/2))
	self.currentMousePos.x = Math:Round(self.currentMousePos.x)
	self.currentMousePos.y = Math:Round(self.currentMousePos.y)
	
	if self.mousezpos==nil then self.mousezpos=0 end
	self.mouseDifference.x = Math:Curve(self.currentMousePos.x - Math:Round(context:GetWidth()/2),self.mouseDifference.x,3)
	self.mouseDifference.y = Math:Curve(self.currentMousePos.y - Math:Round(context:GetHeight()/2),self.mouseDifference.y,3)
	self.camRotation.x = Math:Clamp(self.camRotation.x + self.mouseDifference.y / self.mouseSensitivity,-90,90)
	self.camRotation.y = self.camRotation.y + (self.mouseDifference.x / self.mouseSensitivity)
	
	--Set the camera angle
	self.camera:SetRotation(self.camRotation)

	--position weapon at camera with slight delay
	self.weapon:SetPosition(self.camera:GetPosition(true), true)
	local a = Vec3(0,0,0)
	a.x = Math:CurveAngle(self.camera:GetRotation(true).x, self.weapon:GetRotation(true).x, 2)
	a.y = Math:CurveAngle(self.camera:GetRotation(true).y, self.weapon:GetRotation(true).y, 2)
	a.z = Math:CurveAngle(self.camera:GetRotation(true).z, self.weapon:GetRotation(true).z, 2)
	self.weapon:SetRotation(a, true)
	
	
	if window:MouseHit(1) then
		self:Fire()
	end
	
	if window:KeyHit(Key.R) then
		System:GCSuspend()
		World:GetCurrent():Clear()
		Map:Load("Maps/MSC Nov16.map")
		System:GCResume()
	end

	self.disk:SetRotation(self.disk:GetRotation(false)+Vec3(0,15,0),false)	--rotate disk inside spinfusor
	
end



function Script:UpdatePhysics()

	local window = Window:GetCurrent()
	
	--Player Movement
	local movex=0
	local movez=0
	self.input[0]=0
	self.input[1]=0
	if window:KeyDown(Key.W) then self.input[1]=self.input[1]+1 end
	if window:KeyDown(Key.S) then self.input[1]=self.input[1]-1 end
	if window:KeyDown(Key.D) then self.input[0]=self.input[0]+1 end
	if window:KeyDown(Key.A) then self.input[0]=self.input[0]-1 end
	
	local playerMovement = Vec3(0)
	playerMovement.x = self.input[0] * self.moveSpeed
	playerMovement.z = self.input[1] * self.moveSpeed
	
	--This prevents "speed hack" strafing due to lazy programming
	if self.input[0]~=0 and self.input[1]~=0 then
		playerMovement = playerMovement * 0.70710678
	end

	local velocity=playerMovement*3	--speed that we want to get (if walking)
	local currentSpeed = self.entity:GetVelocity(false)
	currentSpeed.y=0	--we don't want to affect the gravity so we don't include it in calculations
	local extraSpeed = velocity-currentSpeed	--speed that we lack
	velocity=velocity+extraSpeed*200	--adding lacking speed

	local pickinfo=PickInfo()	--check if airborne
	local p1=self.entity:GetPosition(true)
	local onTheGround = World:GetCurrent():Pick(p1,p1-Vec3(0,1,0),pickinfo)

	if not window:KeyDown(Key.Space) and onTheGround then	--apply walking force only if walking on the ground
		self.entity:AddForce(velocity,false)
	end

	if window:KeyDown(Key.Space) then self.entity:SetFriction(0,0)	--skiing
	else self.entity:SetFriction(1,1) end

	local jetPackActive = false
	if window:MouseDown(2) and self.energy>1 then	--jetpack
		local jetpackForce=playerMovement*200
		jetpackForce.y=700
		self.entity:AddForce(jetpackForce, false)
		jetPackActive=true	--for sound and particles
		self.energy=self.energy-1
	end
	self.energy=self.energy+0.4
	if self.energy > 100 then self.energy = 100 end
	if self.energy < 0 then self.energy = 0 end
	--end of movement code

	--Position camera at correct height and playerPosition
	local playerPos = self.entity:GetPosition()
	local newCameraPos = self.camera:GetPosition()
	--local playerTempHeight = ((self:IsCrouched() == 1) and crouchHeight or playerHeight)
	newCameraPos = Vec3(playerPos.x, newCameraPos.y ,playerPos.z)
	
	if newCameraPos.y<playerPos.y + self.eyeheight then
		newCameraPos.y = Math:Curve(playerPos.y + self.eyeheight, newCameraPos.y, self.camSmoothing)
	else
		newCameraPos.y = playerPos.y + self.eyeheight
	end
	
	self.camera:SetPosition(newCameraPos)

	self.entity:PhysicsSetRotation(0,self.camera:GetRotation(true).y,0)	--keeping player straight

	--jetpack particles and sound
	if jetPackActive then
		self.jetpackParticles:Play()
		self.jetpackSource:SetVolume(self.jetpackSource:GetVolume()+0.2)
	else
		self.jetpackParticles:Pause()
		self.jetpackSource:SetVolume(self.jetpackSource:GetVolume()-0.2)
	end
	if self.jetpackSource:GetVolume()>1 then self.jetpackSource:SetVolume(1) end
	if self.jetpackSource:GetVolume()<0 then self.jetpackSource:SetVolume(0) end
end



function Script:Fire()
	local disk = Prefab:Load("Prefabs/Disk.pfb")
	disk:SetPosition(self.camera:GetPosition(true),true)
	disk:SetRotation(self.camera:GetRotation(true),true)
	disk.script.inheritance=self.entity:GetVelocity()/80
end

function Script:PostRender(context)
	context:SetFont(self.mainFont)
	context:SetBlendMode(Blend.Alpha)
	context:SetColor(1,1,1,1)

	context:DrawRect(context:GetWidth()/2-2,context:GetHeight()/2-2,4,4)	--crosshair
	local barLength = self.energy*2	--energy bar
	context:DrawRect(context:GetWidth()/2-barLength/2,context:GetHeight()-10,barLength,6)
	if Window:GetCurrent():KeyDown(Key.Space) then	--skiing speed
		local speed = Math:Round(self.entity:GetVelocity():Length())..""
		context:DrawText(speed,context:GetWidth()/2-self.mainFont:GetTextWidth(speed)/2, context:GetHeight()-15-self.mainFont:GetHeight())
	end
end