-- Copyright ©2022 by Todd Hundersmarck (ThundR)
-- All Rights Reserved
THFirefly = {} local THFirefly_mt = Class(THFirefly) local function runScript() local globalEnv = g_thMain:getGlobalEnv() globalEnv.THFirefly = THFirefly end function THFirefly:onCreate(id) if entityExists(id) then local object = THFirefly.new(id) if object ~= nil then g_currentMission:addUpdateable(object) end end end function THFirefly.new(id, mt) mt = Utils.getNoNil(mt, THFirefly_mt) local newEnv = setmetatable({}, mt) if newEnv ~= nil then local timeOn = Utils.getNoNil(getUserAttribute(id, "timeOn"), 1) local timeOnMax = Utils.getNoNil(getUserAttribute(id, "timeOnMax"), timeOn) local timeOff = Utils.getNoNil(getUserAttribute(id, "timeOff"), 1) local timeOffMax = Utils.getNoNil(getUserAttribute(id, "timeOffMax"), timeOff) if timeOn > timeOnMax then timeOnMax = timeOn end if timeOff > timeOffMax then timeOffMax = timeOff end local deltaTimeOn = timeOnMax - timeOn local deltaTimeOff = timeOffMax - timeOff newEnv.id = id newEnv.isVisible = false newEnv.minTimeOn = timeOn * 1000 newEnv.deltaTimeOn = deltaTimeOn * 1000 newEnv.minTimeOff = timeOff * 1000 newEnv.deltaTimeOff = deltaTimeOff * 1000 newEnv.onDuration = 0 newEnv.offDuration = 0 newEnv.isOn = true setVisibility(id, newEnv.isVisible) g_messageCenter:subscribe(MessageType.WEATHER_CHANGED, newEnv.onWeatherChanged, newEnv) end return newEnv end function THFirefly:update(dt) local id = self.id if entityExists(id) then if self.isVisible then if self.isOn then self.onDuration = self.onDuration - dt if self.onDuration <= 0 then local deltaOff = math.random() * self.deltaTimeOff self.onDuration = 0 self.offDuration = self.minTimeOff + deltaOff self.isOn = false setVisibility(id, false) end else self.offDuration = self.offDuration - dt if self.offDuration <= 0 then local deltaOn = math.random() * self.deltaTimeOn self.offDuration = 0 self.onDuration = self.minTimeOn + deltaOn self.isOn = true setVisibility(id, true) end end end end end function THFirefly:onWeatherChanged() local id = self.id if entityExists(id) then self.isVisible = false if g_currentMission ~= nil then local environment = g_currentMission.environment if environment ~= nil and not environment.isSunOn then local visualSeason = environment.currentVisualSeason if visualSeason ~= nil and visualSeason ~= Environment.SEASON.WINTER then local weather = environment.weather if weather ~= nil and not weather:getIsRaining() then self.isVisible = true end end end end setVisibility(id, self.isVisible) end end function THFirefly:delete() g_currentMission:removeUpdateable(self) g_messageCenter:unsubscribeAll(self) end runScript()