class "amk_particle"
function amk_particle:__init(params)
	self.type = params.typ or "absolute" --bone,absolute
	
	self.dir = params.dir or vector():set(0,0,0)
	self.len = params.len or 1
	self.looped = params.looped or false
	self.life_time = params.life_time or -1
	self.obj = params.obj or nil
	self.bone = params.bone or nil
	self.pos = params.pos or nil
	self.stop_on_death = params.stop_on_death or false
	self.gravity = params.gravity or vector():set(0,0,0)
	self.give_dmg = params.give_dmg or false
	self.target = params.target or nil
	
	if params.sound then
		self.snd = sound_object(params.sound)
		self.snd_looped = params.sound_looped or false
	end
	
	self.cgravity=vector():set(0,0,0)
	self.particle = particles_object(params.particle)
	self.started=false
	self.start_time = time_global()
	self.finalized=false
	self:start()
end

function amk_particle:__finalize()
	self.particle:stop()
end

function amk_particle:start()
	if not self.particle:playing() then
		local pos
		if self.bone and self.obj then
	    pos = self.obj:bone_position(self.bone)
		elseif self.pos then
			pos = self.pos
		end
		self.start_pos = pos
		if pos then
			if self.snd then
				if self.snd_looped then
					self.snd:play_at_pos(db.actor, pos, 1, sound_object.looped)
				else
					self.snd:play_at_pos(db.actor, pos)
				end
				self.snd=nil
			end
			self.particle:play_at_pos(pos)
			self.started=true
		end
	end
end

function amk_particle:update(delta)
	if self.finalized then return end
	self.cgravity = self.cgravity:add(self.gravity)
	if self.particle:playing() then
		if self.life_time>-1 and time_global()>self.start_time+self.life_time then 
			self.started=false
			self.looped=false
			self:stop() 
			self.finalized=true
		end
		local pos
		if self.bone and self.obj then
	    pos = self.obj:bone_position(self.bone)
			self.pos = pos
		elseif self.dir then
			self.pos = self.pos:add(self.dir:set_length(self.len/self.life_time*delta))
			self.pos = self.pos:add(self.cgravity)
			pos = self.pos
		end
		if pos then
			if self.snd and self.snd:playing() then
				self.snd:set_position(pos)
			end
			self.particle:move_to(pos,vector():set(0,0,0))
		end
	else
		if self.started then
			if self.looped then
				self:start()
			end
		end
	end
end

function amk_particle:stop()
	self.give_dmg=false
	if self.particle:playing() then
		self.particle:stop_deffered()
	end
	if self.snd and self.snd:playing() then
		self.snd:stop()
	end
end

function amk_particle:get_pos()
	return self.pos
end

function amk_particle:is_finished()
	return self.finalized
end

function amk_particle:on_death()
	if self.stop_on_death then
		self:stop()
	end
end
