-- vehicle_rul
-- Klasse für Fahrzeuge mit Rundumleuchte
--
-- @author  Sven777b (ICQ 95176001)
-- @date  19/09/08

vehicle_rul = {};

function vehicle_rul:new(configFile, positionX, offsetY, positionZ, rotationY, customMt)

    if vehicle_rul_mt == nil then
        vehicle_rul_mt = Class(vehicle_rul, Vehicle);
    end;

    local mt = customMt;
    if mt == nil then
        mt = vehicle_rul_mt;
    end;
    local instance = Vehicle:new(configFile, positionX, offsetY, positionZ, rotationY, mt);

    local xmlFile = loadXMLFile("TempConfig", configFile);

    instance.rundumleuchtenAnz = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.rundumleuchten#count"),0);
    instance.rundumleuchtenKey = getXMLString(xmlFile, "vehicle.rundumleuchten#key");
    instance.rundumleuchtenAn = false;
    instance.rundumleuchten = {};
    for i=1, instance.rundumleuchtenAnz do
        local objname = string.format("vehicle.rundumleuchten.light" .. "%d",i);
        instance.rundumleuchten[i] = {};
        instance.rundumleuchten[i].rotNode = Utils.indexToObject(instance.rootNode, getXMLString(xmlFile, objname .. "#rotNode"));
        instance.rundumleuchten[i].light = Utils.indexToObject(instance.rootNode, getXMLString(xmlFile, objname .. "#light"));
        instance.rundumleuchten[i].source = Utils.indexToObject(instance.rootNode, getXMLString(xmlFile, objname .. "#lightsource"));
        instance.rundumleuchten[i].speed = Utils.getNoNil(getXMLInt(xmlFile,  objname .. "#rotSpeed"), 1)/1000;
        instance.rundumleuchten[i].emit = Utils.getNoNil(getXMLBool(xmlFile, objname .. "#emitLight"), true);
        if not instance.rundumleuchten[i].emit and instance.rundumleuchten[i].source ~= nil then
           setVisibility(instance.rundumleuchten[i].source, false);
        end;
    end;

    delete(xmlFile);
    return instance;
end;

function vehicle_rul:keyEvent(unicode, sym, modifier, isDown)
    vehicle_rul:superClass().keyEvent(self, unicode, sym, modifier, isDown);
    if self.isEntered then
      if self.rundumleuchtenKey ~= nil then
         if isDown and sym == Input[self.rundumleuchtenKey] then
           self.rundumleuchtenAn = not self.rundumleuchtenAn;
           for i=1, self.rundumleuchtenAnz do
             setVisibility(self.rundumleuchten[i].light, self.rundumleuchtenAn);
           end;
         end;
      end;
    end;
end;

function vehicle_rul:update(dt)
  if self.isEntered then
    if self.rundumleuchtenAnz > 0 then
      if self.rundumleuchtenAn then
        for i=1, self.rundumleuchtenAnz do
          rotate(self.rundumleuchten[i].rotNode, 0, dt*self.rundumleuchten[i].speed, 0);
        end;
      end;
    end;
  end;
  Vehicle.update(self, dt);
end;

function vehicle_rul:onLeave()
    self.rundumleuchtenAn = false;
    for i=1, self.rundumleuchtenAnz do
       setVisibility(self.rundumleuchten[i].light, self.rundumleuchtenAn);
    end;
    Vehicle.onLeave(self);
end;

function vehicle_rul:draw()
  if self.isEntered then
    if self.rundumleuchtenKey ~= nil then
      if self.rundumleuchtenAn then
        g_currentMission:addExtraPrintText("Taste "..string.upper(string.char(Input[self.rundumleuchtenKey]))..": Rundumleuchte aus");
      else
        g_currentMission:addExtraPrintText("Taste "..string.upper(string.char(Input[self.rundumleuchtenKey]))..": Rundumleuchte ein");
      end;
    end;
  end;
  Vehicle.draw(self);
end;