

-----------------------------------------------------------------------------------------------------
class "action_particle"
------------------------
function action_particle:__init (obj, storage)
	self.object = obj
	self.st = storage
    self.particles = {}
    self.path = nil
    self.last_update = 0
    self.started = false
    self.first_played = false
end
------------------------
function action_particle:reset_scheme()
    
    if self.st.mode == 2 then
       self.path = patrol (self.st.path)
       local flags = utils.path_parse_waypoints (self.st.path)
       local count = self.path:count ()
       for a = 1, count, 1 do   
           local d = 0
           if flags[a - 1]["d"] ~= nil then
              d = tonumber (flags[a - 1]["d"])
              if d == nil then d = 0 end
           end
           local sound_name = nil
           if flags[a - 1]["s"] ~= nil then
              sound_name = flags[a - 1]["s"]
           end   
           local snd_obj = nil
           if sound_name ~= nil and sound_name ~= "" then
              snd_obj = xr_sound.get_sound_object(sound_name, "random")
           end   
           self.particles[a] = {particle = particles_object (self.st.name),
                                snd = snd_obj,
                                delay = d,
                                time = time_global (),
                                played = false
                                }
       end 
    else    
       self.particles[1] = {particle = particles_object (self.st.name),
                            snd = nil,
                            delay = 0,
                            time = time_global (),
                            played = false
                            }
       self.path = nil
    end
    self.st.signals = {}
    self.last_update = 0
    self.started = false
    self.first_played = false
end
------------------------
function action_particle:update (delta)

    local time = time_global ()
    if self.last_update ~= 0 then
       if time - self.last_update < 50 then
          return
       else
          self.last_update = time
       end     
    else
       self.last_update = time
    end       
    
    if self.started == false then
       self.started = true
       if self.st.mode == 1 then
          printf ("Load path %s", self.st.path)
          self.particles[1].particle:load_path (self.st.path)
          self.particles[1].particle:start_path (self.st.looped) 
          self.particles[1].particle:play ()
          self.particles[1].played = true
          self.first_played = true
       end
       return
    end    
    
    if self.st.mode == 1 then    
       self:update_mode_1 ()
    else
       self:update_mode_2 ()
    end        
    
    self:is_end ()
    
    xr_logic.try_switch_to_another_section (self.object, self.st, db.actor)
end
------------------------
function action_particle:is_end ()
    if self.st.looped == true or self.first_played == false then 
       return false 
    end
    local size = #self.particles
    if size == 0 then
       return true
    end
    
    for a = 1, size, 1 do
        if self.particles[a].particle:playing () == true then
           return false
        end    
    end        
    self.st.signals["particle_end"] = true
    return true
end
------------------------
function action_particle:update_mode_1 ()
    if self.particles[1].particle:playing () == false and self.st.looped == true then
       self.particles[1].particle:play ()
    end    
end
------------------------
function action_particle:update_mode_2 ()
    local size = #self.particles
    if size == 0 then return end
    local time = time_global ()
    for a = 1, size, 1 do
        if (time - self.particles[a].time) > self.particles[a].delay and self.particles[a].particle:playing () == false then
           if self.particles[a].played == false then
              self.particles[a].particle:play_at_pos (self.path:point (a - 1))
              if self.particles[a].snd ~= nil then
                 self.particles[a].snd:play_at_pos (self.object, self.path:point (a - 1), 0)
              end
              self.particles[a].played = true
              self.first_played = true
           else 
              if self.st.looped == true then     
                 self.particles[a].particle:play_at_pos (self.path:point (a - 1))
                 if self.particles[a].snd ~= nil then
                    self.particles[a].snd:play_at_pos (self.object, self.path:point (a - 1), 0)
                 end
              end
           end
        end         
    end    
end
------------------------
function action_particle:deactivate ()
    local size = #self.particles
    for a = 1, size, 1 do
        if self.particles[a].particle:playing () == true then
           self.particles[a].particle:stop ();
        end    
        self.particles[a].particle = nil
        if self.particles[a].snd ~= nil and self.particles[a].snd:playing () == true then
           self.particles[a].snd:stop ()
        end    
        self.particles[a].snd = nil
    end
end



-----------------------------------------------------------------------------------------------------
function add_to_binder (npc, ini, scheme, section, storage)
	printf("DEBUG: add_to_binder: scheme='%s', section='%s'", scheme, section)
	local new_action = action_particle (npc, storage)
	xr_logic.subscribe_action_for_events (npc, storage, new_action)
	
end
-----------------------------------------------------------------------------------------------------
function set_scheme (obj, ini, scheme, section, gulag_name)

	local st = xr_logic.assign_storage_and_bind (obj, ini, scheme, section)
	st.logic = xr_logic.cfg_get_switch_conditions (ini, section, obj)

    st.name         = utils.cfg_get_string (ini, section, "name", obj, true, "", nil)
    st.path         = utils.cfg_get_string (ini, section, "path", obj, true, "", nil)
    st.mode         = utils.cfg_get_number (ini, section, "mode", obj, true) 
    st.looped       = utils.cfg_get_bool   (ini, section, "looped", obj, false)
    if st.path == nil or st.path == "" then
       abort ("SR_PARTICLE : invalid path name")
    end
    if st.mode ~= 1 and st.mode ~= 2 then
       abort ("SR_PARTICLE : invalid mode") 
    end   
            
end
