package.path = package.path ..';./Scripts/DemoScenes/?.lua;'
local sceneEnvironment = require("demosceneEnvironment")

scene = {} -- mettre tous les objets crees ici
local startTime = os.clock()
local function pingpong(length, t)
    local tt = (t%length) / length
    return tt
end
local length = 10 -- duree totale de l'effet
local value = -1
function loadScene(scenePtr)
	sceneAPI = sceneEnvironment.getInterface(scenePtr)
	sceneAPI:setUpdateFunc('sceneVRUpdate')
	sceneAPI:setEnvironmentMap("Bazar/Graphics/VRMainScene.dds",0.9);
	sceneAPI:setSun(math.rad(-90), math.rad(0)) -- placer le soleil sous l'horizon pour qu'il ne brille pas

	scene.m	= sceneAPI:addModel("KA-50_3", 7, 1.7, -5); -- lower moves back, hauteur machine, lower moves right
	scene.m:setLivery("FictionalFrenchCamov1", "KA-50_3"); -- texture de l'appareil
	scene.m.transform:setOrient(0, -150, 0)	-- Rotation l'appareil
	scene.m:setArgument(0, 1); -- train avant sorti
	scene.m:setArgument(1, 0.3); -- compression train avant
	scene.m:setArgument(2, 0.2); -- rotation train avant
	scene.m:setArgument(3, 1); -- train arriere droit sorti
	scene.m:setArgument(4, 0.2); -- compression train arriere droit
	scene.m:setArgument(5, 1); -- train arriere gauche sorti 
	scene.m:setArgument(6, 0.2); -- compression train arriere gauche
	scene.m:setArgument(9, 1); -- trappe roue arriere droit ouverte
	scene.m:setArgument(10, 1); -- trappe roue arriere gauche ouverte
	scene.m:setArgument(25, 1); -- trappe pilote ouverte
	scene.m:setArgument(31, 0.5); -- numero machine 5X
	scene.m:setArgument(32, 0.1); -- numero machine X1
	scene.m:setArgument(38, 1); -- porte pilote ouverte
	scene.m:setArgument(40, -1); -- position des pales
	scene.m:setArgument(46, 1); -- spot allume
	scene.m:setArgument(50, 0.5); -- sans pilote mais presence siege ejectable
	scene.m:setArgument(190, 1); -- lumiere signalisation
	scene.m:setArgument(193, 1); -- girophare intensite lumineuse de 0 a 1
	scene.m:setArgument(209, 1); -- rotation spot
	scene.m:setArgument(428, 1); -- toutes les trappes maintenance ouverte
	scene.m:setArgument(737, 1); -- radar present
	
	--KA-50 Loadout
	scene.m2 = sceneAPI:addModel("PU_9S846_STRELEC", 0.05, -0.32, 0);
	scene.m2:attachTo(scene.m, "HardPoint-1");
	scene.m2 = sceneAPI:addModel("PU_9S846_STRELEC", 0.05, -0.32, 0);
	scene.m2:attachTo(scene.m, "HardPoint-6");
	
	--KA-50 Loadout2
	scene.m3 = sceneAPI:addModel("9k121", 0.05, -0.5, 0);
	scene.m3:attachTo(scene.m, "HardPoint-9");
	scene.m3 = sceneAPI:addModel("9k121", 0.05, -0.5, 0);
	scene.m3:attachTo(scene.m, "HardPoint-10");
	
	--KA-50 Loadout2
	--scene.m3 = sceneAPI:addModel("9k121", 0, 0, 0);
	--scene.m3:attachTo("AttachPoint", scene.m, "HardPoint-9");
	--scene.m3 = sceneAPI:addModel("9k121", 0, 0, 0);
	--scene.m3:attachTo("AttachPoint", scene.m, "HardPoint-10");
	
	--KA-50 Loadout3
	--scene.m4 = sceneAPI:addModel("9k121", 0, -0.4, 0);
	--scene.m4:attachTo(scene.m, "HardPoint-3");
	--scene.m4 = sceneAPI:addModel("9k121", 0, 0, 0);
	--scene.m4:attachTo(scene.m, "HardPoint-4");
	
			
	encyclopediaAnimation = {
}
	
	local cam_level = 1.8

	scene.cam		= sceneAPI:addCamera(4, cam_level , 4)
	scene.cam:setFarClip(1000.0)
	scene.cam:setFov(90)
	scene.cam.transform:lookAtPoint(-10.0, cam_level, 0); 

	scene.flr		= sceneAPI:addModel("shelter_floor", 0,0,0);
	scene.flr.transform:setOrient(0, -150, 0) -- rotation sol
	scene.flr:drawToEnvironment(true);
	scene.flr.transform:scale(1,1,1); --(XY,X,Y)

	scene.sh		= sceneAPI:addModel("ukrytie", 7,0,-5); 
	scene.sh.transform:setOrient(0, -150, 0) -- rotation abri	
	scene.sh:drawToEnvironment(true);
	scene.sh.transform:scale(1.1,1.1,1.1);

	scene.L1			= sceneAPI:addLightSpot(0, 5, 0,	1,1,1);
	scene.L1:setRadius(5);
	scene.L1:setAmount(1);

	scene.cam:setActive()
end

function sceneVRUpdate(t, dt)
    local currentTime = os.clock() - startTime
	if value >= 1 then
        value = 0
        startTime = currentTime
    end
    local value = pingpong(length, currentTime-startTime)--local value = math.sin(t/length) --value = pingpong(length, currentTime)
    scene.m:setArgument(195, value)
end