--@Author: Alex2009, Sven777b
--@Web: www.ls-landtechnik.de, www.alex2009.de

fenceSmalDoorPlaceable = {};
fenceSmalDoorPlaceable_mt = Class(fenceSmalDoorPlaceable, Placeable);
InitObjectClass(fenceSmalDoorPlaceable, "fenceSmalDoorPlaceable");

function fenceSmalDoorPlaceable:new(isServer, isClient, customMt)
    local self = Placeable:new(isServer, isClient, fenceSmalDoorPlaceable_mt);
    registerObjectClassName(self, "fenceSmalDoorPlaceable");
    return self;
end;

function fenceSmalDoorPlaceable:delete()
    removeTrigger(self.doorTrigger);
    unregisterObjectClassName(self);
    fenceSmalDoorPlaceable:superClass().delete(self);
end;

function fenceSmalDoorPlaceable:deleteFinal()
    fenceSmalDoorPlaceable:superClass().deleteFinal(self);
end;

function fenceSmalDoorPlaceable:load(xmlFilename, x,y,z, rx,ry,rz, moveMode, initRandom)
    if not fenceSmalDoorPlaceable:superClass().load(self, xmlFilename, x,y,z, rx,ry,rz, moveMode, initRandom) then
        return false;
    end;
    local xmlFile = loadXMLFile("TempXML", xmlFilename);
    self.doorTrigger = Utils.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.FenceDoorTrigger#index"));
    addTrigger(self.doorTrigger, "triggerCallback", self);    
    self.Door = getChildAt(getChildAt(self.doorTrigger, 0),0);
    self.count = 0; 
    self.angle = math.rad(0);
    self.maxAngle = math.rad(90);
    self.minAngle = math.rad(0);
    self.isEnabled = true;
    delete(xmlFile);
    return true;
end;

function fenceSmalDoorPlaceable:update(dt)
    local old = self.angle;
    if self.count > 0 then
        if self.angle < self.maxAngle then
            self.angle = self.angle + dt*0.0006;    
        end;
    else
        if self.angle > self.minAngle then
            self.angle = self.angle - dt*0.0006;    
        end;
    end;
    if old ~= self.angle then
        local x,y,z = getRotation(self.Door);
        setRotation(self.Door,x,(-self.angle),z);
    end;
end;

function fenceSmalDoorPlaceable:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay)  
    if onEnter and self.isEnabled then
        self.count = self.count + 1;
    elseif onLeave then
        self.count = self.count - 1;
    end;
end;
registerPlaceableType("fenceSmalDoorPlaceable", fenceSmalDoorPlaceable);