Bullet = class(CenteredObject, function(o, x, y, target, damage)
	o.sprite = o.sprite or sprites['tiles']['bullet_cannon_' .. player.currentWorld.id .. '.png']
	o.h = o.sprite:getTexHeight()
	o.w = o.sprite:getTexWidth()
	o.h=o.h*r_scale/2
	o.w=o.w*r_scale/2

	CenteredObject.init(o, x, y, o.w, o.h)
	
	o.scale = r_scale/2

	o.moves = true
	o.maxAge = 200
	o.damageAmount = damage or game_settings["bullet_damage"]
	if box2d then
		o.b2d = BludBox2dRect(box2d, o.pos.x, o.pos.y, o.w/2, o.h/2, 1, 0.4, 0.4, true)
		o.b2d:setData(o)
	end
end)
function Bullet:update()
	if self.b2d and self.killb2d then self.b2d:destroy(); self.killb2d = false end
	Object.update(self)
	if self.b2d then
		self.b2d:setPosition(self.pos.x, self.pos.y)
	end
	self.maxAge = self.maxAge-1
	if self.maxAge < 0 then self:kill() end
end
function Bullet:updateRotation()
	self.rot = math.deg(math.pi-self.velocity:angle())
end
function Bullet:handleCollision(other)
	if other:is_a(Enemy) then
		pd:startList("fromOF")
		pd:addSymbol("bulletHit")
		pd:addFloat(1)
		pd:finish("fromOF")
		self:kill(true)
		other:hit(self.damageAmount, self.damageType)
		if mainState.bulletParticles and not self:is_a(FreezeBullet) then
			mainState.bulletParticles.pl.particles:setPosition(self.pos.x, self.pos.y)
			mainState.bulletParticles.pl.particles:add(math.random(5, 12))
		end
	end
end
FreezeBullet = class(Bullet, function(o, x, y, freezeTime)
	o.sprite = sprites['tiles']['bullet_freeze.png']
	Bullet.init(o, x, y)
	o.damageType = "freeze"
	o.damageAmount = freezeTime
	-- o.svg = SvgLoader(svg_root .. "defence/bullet_freeze.svg", true)
end)

function FreezeBullet:kill()
	Object.kill(self)
	mainState.particles:addRecycle(ParticleHandler(self.pos.x, self.pos.y, "particle_data_snowsplosion.lua", sprites['tiles']['freeze_flake.png'], {preroll=.1, lifetime=0.1, cooldown=2}))
end

HomingBullet = class(Bullet, function(o, x, y, target, damage)
	o.sprite = sprites['tiles']['bullet_cannon_seek_' .. player.currentWorld.id .. '.png']
	Bullet.init(o, x, y, target, damage)
	o.target = target
end)
function HomingBullet:update()
	Bullet.update(self)
	local steerForce = 10.0*30.0*bludG.elapsed
	-- get the vector to the target (steering vector)
	local steer = self.target.pos:copy():sub(self.pos):normalize():mult(steerForce*r_scale)
	-- apply the steering vector to the main movement vector
	self.velocity:add(steer)
	-- maxspeed the bullet
	Bullet.updateRotation(self)
end
function HomingBullet:kill()
	Object.kill(self)
	mainState.particles:addRecycle(Explosion(self.pos.x, self.pos.y, 0.7))
end