-- www.schwabenmodding.bplaced.net 
-- Version 2: 
-- .. Im Singleplayer ist es nun möglich die Tore per Tastendruck offen zu halten bis erneutem Tastendruck. Im MP nimmt alles seinen gewohnten Gang wie bisher


rotationDoors = {};

  
local rotationDoors_mt = Class(rotationDoors);
  
function rotationDoors.onCreate(id)
    g_currentMission:addUpdateable(rotationDoors:new(id));
    print("rotationDoors: rotationDoors onCreate aufgerufen.  OnCreate Id: "..id.." .")	
end;
  
function rotationDoors:new(id, customMt) 
    local instance = {};
    if customMt ~= nil then
        setmetatable(instance, customMt);
    else
        setmetatable(instance, rotationDoors_mt);
    end;
  
    instance.triggerId = id;
    addTrigger(id, "triggerCallback", instance);
  	instance.rotationDoors = {};	  	
	local lar = {};
	lar.Rdoor = getChildAt(id, 1);
	lar.Ldoor = getChildAt(id, 0);
	local marr = getUserAttribute(id, "maxRotRight");
	local marl = getUserAttribute(id, "maxRotLeft");
	local mirr = getUserAttribute(id, "minRotRight"); 
	local mirl = getUserAttribute(id, "minRotLeft"); 
	lar.maxRotR = Utils.degToRad(marr);
	lar.maxRotL = Utils.degToRad(marl);
	lar.minRotR = Utils.degToRad(mirr);
	lar.minRotL = Utils.degToRad(mirl);
	lar.rotL = 0;
	lar.rotR = 0;
	lar.isOpen = false;
	lar.stayOpen = false;
	self.count = 0;
	if lar.Rdoor ~= nil and lar.Ldoor ~= nil then
	    table.insert(instance.rotationDoors, lar);	
	else
	   print("rotationDoors: Error, no doors Specified.")
	end;
		
    return instance;
end;
  
function rotationDoors:delete()
    removeTrigger(self.triggerId);
end;
  
function rotationDoors:update(dt) 
    for i=1, table.getn(self.rotationDoors) do
		if self.count > 0 then
			self.rotationDoors[i].isOpen = true;
			if g_currentMission.missionDynamicInfo.isMultiplayer == false then
				if InputBinding.hasEvent(InputBinding.TOGGLE_DOOR) then
					self.rotationDoors[i].stayOpen = not self.rotationDoors[i].stayOpen;
				end;	
			end;
		else
			if self.rotationDoors[i].stayOpen == false then
				self.rotationDoors[i].isOpen = false;
			else
				self.rotationDoors[i].isOpen = true;
			end;
		end;
	
		if self.rotationDoors[i].isOpen == true then
			if self.rotationDoors[i].maxRotL > self.rotationDoors[i].minRotL then
				if self.rotationDoors[i].rotL < self.rotationDoors[i].maxRotL then
					self.rotationDoors[i].rotL = self.rotationDoors[i].rotL + dt*0.001;
				end;
			elseif self.rotationDoors[i].maxRotL < self.rotationDoors[i].minRotL then
				if self.rotationDoors[i].rotL > self.rotationDoors[i].maxRotL then
					self.rotationDoors[i].rotL = self.rotationDoors[i].rotL - dt*0.001;
				end;
			end;
			if self.rotationDoors[i].maxRotR > self.rotationDoors[i].minRotR then
				if self.rotationDoors[i].rotR < self.rotationDoors[i].maxRotR then
					self.rotationDoors[i].rotR = self.rotationDoors[i].rotR + dt*0.001;
				end;
			elseif self.rotationDoors[i].maxRotR < self.rotationDoors[i].minRotR then
				if self.rotationDoors[i].rotR > self.rotationDoors[i].maxRotR then
					self.rotationDoors[i].rotR = self.rotationDoors[i].rotR - dt*0.001;
				end;
			end;	
	    else
			if self.rotationDoors[i].maxRotL > self.rotationDoors[i].minRotL then
				if self.rotationDoors[i].rotL > self.rotationDoors[i].minRotL then
					self.rotationDoors[i].rotL = self.rotationDoors[i].rotL - dt*0.001;
				end;
			elseif self.rotationDoors[i].maxRotL < self.rotationDoors[i].minRotL then
				if self.rotationDoors[i].rotL < self.rotationDoors[i].minRotL then
					self.rotationDoors[i].rotL = self.rotationDoors[i].rotL + dt*0.001;
				end;
			end;
			if self.rotationDoors[i].maxRotR > self.rotationDoors[i].minRotR then
				if self.rotationDoors[i].rotR > self.rotationDoors[i].minRotR then
					self.rotationDoors[i].rotR = self.rotationDoors[i].rotR - dt*0.001;
				end;
			elseif self.rotationDoors[i].maxRotR < self.rotationDoors[i].minRotR then
				if self.rotationDoors[i].rotR < self.rotationDoors[i].minRotR then
					self.rotationDoors[i].rotR = self.rotationDoors[i].rotR + dt*0.001;
				end;
			end;
        end;
	end;
    for i=1, table.getn(self.rotationDoors) do
        setRotation(self.rotationDoors[i].Ldoor, 0, self.rotationDoors[i].rotL, 0);
        setRotation(self.rotationDoors[i].Rdoor, 0, self.rotationDoors[i].rotR, 0);			
    end; 

end;
  
function rotationDoors:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay)
    if onEnter then
        self.count = self.count + 1;
    elseif onLeave then
        self.count = self.count - 1;
    end; 
end;
    
g_onCreateUtil.addOnCreateFunction("rotationDoorsOnCreate", rotationDoors.onCreate);

