Bullet = Object.extend(Object)

function Bullet:new( player )
	self.player = player
	self.ex = 0
	self.ey = 0
	self.ox = 0
	self.oy = 0
	self.angle = 0
	self.emitted = 0
	self.lifetime = 1
	self.finished = false
	self.speed = 8
end

function Bullet:update(dt)
	self.emitted = self.emitted + dt

	if self.emitted > self.lifetime then
		self.finished = true
	else
		self.ex = self.ex + math.lengthdir_x( self.speed, self.angle ) + self.ox
		self.ey = self.ey + math.lengthdir_y( self.speed, self.angle ) + self.oy

		self.source:setPosition(self.ex, self.ey,0)
	end
end

function Bullet:draw()
	if self.finished == false then
		local bx = self.ex
		local by = self.ey
		local a = math.clamp(0, 1-(-0.3+(self.emitted/self.lifetime)), 1)

		love.graphics.setColor(1,1,1,a)
		love.graphics.draw( bulletShot, bx, by, self.angle*(math.pi/180) )
	end
end

function Bullet:emit( ex, ey, ox, oy, angle, lifetime )
	self.ex = ex
	self.ey = ey
	self.ox = ox
	self.oy = oy
	self.angle = angle
	self.lifetime = lifetime

	self:generateSound(ex,ey)
end

function Bullet:generateSound(px,py)
	local sound = sfxr.newSound()
	sound:resetParameters()
	sound.waveform = 0
	sound.frequency.start = math.variance(0.50948863032722, 50)
	sound.frequency.min = math.variance(0.23191561855507, 50)
	sound.frequency.slide = -(math.variance(0.30302405172478, 50))
	sound.duty.ratio = math.variance(0.11477388876231, 50)
	sound.duty.sweep = math.variance(0.10811502385025, 50)
	sound.envelope.attack = math.random(0.0,0.1)
	sound.envelope.sustain = 0.14060104631004 * (math.random(1,100)/100)
	sound.envelope.decay = math.variance(0.22882606362934, 50)
	sound.envelope.punch = math.variance(0.29031108049385,50)
	sound.phaser.offset = 0
	sound.phaser.sweep = 0
	sound.highpass.cutoff = math.variance(0.25467943576175, 50)

	local soundData = sound:generateSoundData()
	self.source = love.audio.newSource(soundData)
	self.source:setVolume(1)
	self.source:setPosition(px,py)
	self.source:play()
end
