
-- name - access particle name (ID)
function ParticleAdd(fxName, depth, pos, name)
	if pos == nil then pos = Mouse.GetPosition() end
	if depth == nil then depth = 6000 end 
	if name == nil then name = fxName end
    
	Particle:Add(name, fxName)
  
	Particle:SetPosition(name,pos, depth)
end 

function MoveParticle(fxname, from, to, time, name )
  if time == nil then time = 1  end
  if id == nil then id = name end
  local depth = 6000
  ParticleAdd(fxname, depth, from, name)
  local vel  = (to - from) / time
  
  local pos  = from
  local lastTime = Time:GetTime()/1000000
  local q = Cmd(
  tWhile(
    function() return (pos.x ~= to.x or pos.y ~= to.y) end, 
    function() 
      local currTime = Time:GetTime()/1000000
      local dt = currTime - lastTime
      lastTime = currTime 
      
      pos = pos + (vel * dt)      
      if vel.x < 0 and pos.x < to.x then pos.x = to.x end
      if vel.x > 0 and pos.x > to.x then pos.x = to.x end
      if vel.y < 0 and pos.y < to.y then pos.y = to.y end
      if vel.y > 0 and pos.y > to.y then pos.y = to.y end
      Particle:SetPosition(name,pos, depth)
    end
    )
  )
  return q
end


function ParticleStart(name)
    Particle:Start(name)
end

function ParticleStop(name)
    Particle:Stop(name)
end

function ParticlePause(name)
    Particle:Pause(name)
end

function ParticleRestart(name)
    Particle:Restart(name)
end


function StartSkyMotion(motionSkySpeed, startXpos, motionSkySpeed1, startXpos1)
    _G.motionSkySpeed = Default(motionSkySpeed, 1)
    _G.motionSkyStartXpos  = Default(startXpos, 0)
    _G.motionSkySpeed1 = Default(motionSkySpeed1, 1)
    _G.motionSkyStartXpos1  = Default(startXpos1, 0)    
end



function UpdateSkyMotion(self_)
    if not _G.motionSkyLastTime then
        _G.motionSkyLastTime = GetTime()
        return 
    end
    local dT = GetTime() - _G.motionSkyLastTime 
    local currModule = _G[self_:GetName()]
    UpdateSky(currModule["spr_sky"], "", dT)
    UpdateSky(currModule["spr_sky1"], "1", dT)
    _G.motionSkyLastTime = GetTime()
end



function UpdateSky(sky, number, dT)
    local number = Default(number, "")
    if sky then
        local dX = 0.01 * _G["motionSkySpeed"..number] * dT
        local sPos = sky.self:GetPosition()
        local width = sky.self:GetWidth()
        local startX = _G["motionSkyStartXpos"..number]
      
        local deltaX = sPos.x - (startX + (width * 0.5))
        if deltaX>0 then
            sky.self:SetPosition(Ogre.Vector2(startX + deltaX, sPos.y))
        else
            if sPos.x < startX then
                sky.self:SetPosition(Ogre.Vector2(startX, sPos.y))
            end
        end    
        
        sky.self:SetPosition(sPos+Ogre.Vector2(dX, 0))
        sPos = sky.self:GetPosition()
    end
end





