-- Derive our 'GraphicsSample' class from 'usul.LuaScript' base class.
class 'GraphicsSample' (usul.LuaScript)

-- Constructor must call usul.LuaScript constructor.
function GraphicsSample:__init(luaScriptId, luaScriptEntity)
	-- Call the parent constructor
    usul.LuaScript.__init(self, luaScriptId, luaScriptEntity)
end

-- Called on every frame. Time in µs
function GraphicsSample:Update(timeSinceLastFrameUs)
	-- Exit demo when user hits escape key by stopping the auto-updated loop.
	if(usul.input:GetKey(usul.KeyCode.ESCAPE)) then
		usul.Stop()
	end
	
	-- Switch debug rendering mode when user hit space key. 200ms anti-rebound.
	if(self.antiRebound == nil) then
		self.antiRebound = timeSinceLastFrameUs
	else
		self.antiRebound = self.antiRebound + timeSinceLastFrameUs
	end
	if(usul.input:GetKey(usul.KeyCode.SPACE) and (self.antiRebound > 200000)) then
		currentRenderingMode = self.entity.camera.renderingMode
		currentRenderingMode = currentRenderingMode + 1
		if(currentRenderingMode > 16) then
			currentRenderingMode = 0
		end
		self.entity.camera.renderingMode = currentRenderingMode
		self.antiRebound = nil;
	end
	
	-- F1 to change AntiAliasing mode, use the same anti-rebound for simplicity
	if(usul.input:GetKey(usul.KeyCode.F1) and (self.antiRebound > 200000)) then
		currentAntiAliasingMode = self.entity.camera.antiAliasingMode
		currentAntiAliasingMode = currentAntiAliasingMode + 1
		if(currentAntiAliasingMode > 4) then
			currentAntiAliasingMode = 0
		end
		self.entity.camera.antiAliasingMode = currentAntiAliasingMode
		self.antiRebound = nil;
	end
	
	-- F2 to change SSAO mode, use the same anti-rebound for simplicity
	if(usul.input:GetKey(usul.KeyCode.F2) and (self.antiRebound > 200000)) then
		currentSsaoMode = self.entity.camera.ssaoMode
		currentSsaoMode = currentSsaoMode + 1
		if(currentSsaoMode > 1) then
			currentSsaoMode = 0
		end
		self.entity.camera.ssaoMode = currentSsaoMode
		self.antiRebound = nil;
	end
	
	-- Move at 10 units per seconds
	speed = timeSinceLastFrameUs * 0.00001
	
	-- Move the entity this script is attached to.
	if(usul.input:GetKey(usul.KeyCode.UP)) then
		self.entity.transform:Translate(0, 0, -speed)	-- -Z is forward in Usul
	end
	if(usul.input:GetKey(usul.KeyCode.DOWN)) then
		self.entity.transform:Translate(0, 0, speed)
	end
	if(usul.input:GetKey(usul.KeyCode.LEFT)) then
		self.entity.transform:Translate(-speed, 0, 0)
	end
	if(usul.input:GetKey(usul.KeyCode.RIGHT)) then
		self.entity.transform:Translate(speed, 0, 0)
	end

	-- Orient with mouse
	self.entity.transform:Rotate(usul.Degree(-usul.input.mouseAxisX.relative * 0.15), usul.Vector3.AXIS_Y, usul.TransformSpace.WORLD)
	self.entity.transform:Rotate(usul.Degree(-usul.input.mouseAxisY.relative * 0.15), usul.Vector3.AXIS_X)
end
