local gravAccel = Vec2(0,0.6)
FlyingEnemy = class(Object, function(o, x, y, world)
	Object.init(o,x,y,10,10)
	o.drawType = 'stretched'
	o.sprite = sprites['tiles']['white.png']
	o:setColor(255, 0, 0)
	o.world = world
	o.direction = Vec2(0,5)
	o.shootRate = (math.random()+0.5)/2
	o.shootCounter = o.shootRate
	o.shots = Group()
	o.range = 250
	o.health = 2
end)
function FlyingEnemy:update()
	local limits = self.world:getTowerLimits()
	if limits.maxY then
		if self.pos.y > limits.maxY then
			self.pos.y = limits.maxY
			self.direction.y = -5
		end
		if self.pos.y < limits.minY then
			self.pos.y = limits.minY
			self.direction.y = 5
		end
		-- if we are not within 25 units of the left of the building, then move towards it
		if self.pos.x > limits.minX-38 then
				self.direction.x = -2
		elseif self.pos.x < limits.minX-38 then
				self.direction.x = 2		
		else
			self.direction.x = 0
		end
		self.pos:add(self.direction)
	end
	-- run the countdown
	if self.alive then
		self.shootCounter = self.shootCounter - bludG.elapsed
		if self.shootCounter < 0 then
			if self:addShot() then
				self.shootCounter = self.shootRate
			end
		end
	end
end
function FlyingEnemy:addShot()
	-- first check to see if we are near any rooms
	if self.world then
		local ne, neDist = findNearest(self.world.rooms.members, self:center())
		if ne and neDist < self.range then
			local shot = Object(self.pos.x, self.pos.y, 5, 5)
			shot:setColor(0,0,0)
			shot.drawType = 'stretched'
			shot.sprite = self.sprite
			local targetCenter = ne:center()
			Tweener:addTween(shot.pos, {x=targetCenter.x, y=targetCenter.y}, {func='easeNone', time=0.2, onComplete=function()
				shot:kill();
				-- eventually will need to pass in the attack value here
				ne:hit();
				if not ne.alive then
					self:kill()
				end
			end})
			self.shots:addRecycle(shot)
			return true
		end
	end
	return false
end
function FlyingEnemy:draw()
	Object.drawStretched(self)
	self.shots:draw()
end
function FlyingEnemy:hit()
	-- add some particles over the enemy that is getting hit
	local particleSettings = {count=9, lifetime=40, size=0.2, updateFunction = function(particle)
	  -- update the position of the particles
	  particle.vel:add(gravAccel)
	  particle.vel:mult(1.1)
	  particle.pos:add(particle.vel)
	  -- draw the particle on the screen
	  particle.size = particle.size-0.005
	  particle.tint.a = math.max(0, (1-particle.age/40)*255)
	end, spawner = function(i, total)
	  return (math.random()-1)*5, (math.random()-0.5)*5, math.random()
	end, tint={r=100, g=20, b=20, a=200}}

	particles:addSystem(sprites['tiles']["white.png"], self.pos.x, self.pos.y, particleSettings)
	self.health = self.health - 1
	if self.health <= 0 then
		self:kill()
	end
end
function FlyingEnemy:kill()
	self.alive = false
	Tweener:addTween(self, {w=5*self.w, h=5*self.h}, {onComplete=function()
		self.exists = false
	end})
	Tweener:addTween(self.tint, {a=0})
end
-- mainState = WrapperState(Group());
-- ps = PlayState()
-- mainState:changeState(ps)
