-- Приложение силы к физическому объекту
-- Created by Tunduk Vladimir aka Sidorovich

----------------
class "ph_force"
----------------
function ph_force:__init (obj, storage)
	self.object = obj
	self.st	= storage
	self.time = 0
	self.process = false
end
----------------
function ph_force:reset_scheme ()
    if self.st.delay ~= 0 then
       self.time = time_global () + self.st.delay
    end      
	self.process = false
end
----------------
function ph_force:update (delta)   
	if xr_logic.try_switch_to_another_section (self.object, self.st, db.actor) then
	   return
	end
	
	if self.process == true then
	   return
	end    
	
	if self.st.delay ~= nil then
	   if time_global () - self.time < 0 then
	      return
	   end
	end
    
    local dir = self.st.point:sub (self.object:position ())
    dir:normalize ()
	self.object:set_const_force (dir, self.st.force, self.st.time)
	self.process = true
end
----------------
function add_to_binder (npc, ini, scheme, section, storage)
	local new_action = ph_force (npc, storage)
	xr_logic.subscribe_action_for_events (npc, storage, new_action)
end
----------------
function set_scheme (npc, ini, scheme, section, gulag_name)
	local st    = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
	st.logic    = xr_logic.cfg_get_switch_conditions(ini, section, npc)
	
	st.force    = utils.cfg_get_number (ini, section, "force", npc, true, 0)
	st.time     = utils.cfg_get_number (ini, section, "time", npc, true, 0)
	st.delay    = utils.cfg_get_number (ini, section, "delay", npc, false, 0)
	
	local path_name    = utils.cfg_get_string(ini, section, "point",	npc, true, "")
	local index    = utils.cfg_get_number (ini, section, "point_index", npc, false, 0)
	
	if st.force == nil or st.force <= 0 then
	   abort ("PH_FORCE : invalid force !")
	end
	
	if st.time == nil or st.time <= 0 then
	   abort ("PH_FORCE : invalid time !")
	end
	
	if path_name == nil or path_name == "" then
	   abort ("PH_FORCE : invalid waypoint name !")
	end   
	
	local path = patrol (path_name)
	
	if index >= path:count () then
	   abort ("PH_FORCE : invalid waypoint index !")
	end   
	st.point = path:point (index)
end
