-- if not pulseQueue, and no children, pulsequeue in 32
-- if not pulseQueue, and children
	-- if children complete, pulsequeue in 8
-- kid enemy should spawn away from the main building, then walk into to it, then jump onto it.
KidEnemy = class(Enemy, function(o, x, y, world, ...)
	if not kidHangOffset then
		kidHangOffset = Vec2(12, 0)
		kidHangOffset:mult(r_scale)
		kidHangOffsetFlip = Vec2(18, 0)
		kidHangOffsetFlip:mult(r_scale)
	end
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	local runAnimation = options.runAnimation or childRunAnimation
	local hitAnimation = options.hitAnimation or childHitAnimation

	o.settingName = options.settingName or 'child_'
	o.stint = options.tint or {255, 255, 255}
	o.hit_power = game_settings[o.settingName.."hit_power"] or 0.5

	local size = 75

	size = math.floor(size * r_scale / 2)

	Enemy.init(o,x,y,size,size)
	
	if not runAnimation[1].getTexWidth then
		for i,v in ipairs(runAnimation) do
			runAnimation[i] = sprites['enemies'][v]
		end
	end
	if not hitAnimation[1].getTexWidth then
		for i,v in ipairs(hitAnimation) do
			hitAnimation[i] = sprites['enemies'][v]
		end
	end

	o.runAnimation = AnimationModel(runAnimation, {rate=options.rate or 40})
	o.hitAnimation = AnimationModel(hitAnimation, {rate=options.rate or 40})
	o.currentAnimation = o.runAnimation
	o.sprite = o.currentAnimation:getSprite()

	o.drawType = 'stretched'
	o.sprite = sprites['tiles']['white.png']
	o.moves = true
	o.world = world
	o.immovable = false

	-- get a random position to init the kid at
	-- this isn't perfect yet, since the kids can land on top of the building
	o:initialPosition()

	o.health = game_settings[o.settingName.."health"]
	o.hitRate = game_settings[o.settingName.."hit_rate"]

	o.nextHit = o.hitRate
	-- this is the gravity
	o.y_speed = 1200
	o.x_speed = game_settings[o.settingName.."run_rate"]
	o.y_speed = o.y_speed *r_scale / 2; o.x_speed = r_scale*o.x_speed/2;
	o.velocity = Vec2(0, 0)
	o.acceleration = Vec2(0, o.y_speed)
	o:setColor(o.stint[1], o.stint[2], o.stint[3], 0)
	o.colorTween = Tweener:addTween(o.tint, {a=255}, {time=1.5})
	o.sheet = sheets['enemies']
	o.entity:setSheet(o.sheet)
end)
function KidEnemy:sync()
	if not Enemy.sync(self) then return end
	if self.alive then
		self.nextHit = self.nextHit - 1
		if self.nextHit <= 0 then
			self.nextHit = self.hitRate
			if self.hittingRoom and self.hittingRoom.alive then
				self.hittingRoom:hit(self.hit_power)
				pd:startList("fromOF")
				pd:addSymbol("kid_hit_building")
				pd:addFloat(1)
				pd:finish("fromOF")
				mainState.particles:addRecycle(ParticleHandler(self.pos.x, self.pos.y, "particle_data_feathers_start.lua", sprites['tiles']['smash.png'], {preroll=.25, lifetime=0.1, cooldown=2}))
			end
		end
	end
end

function KidEnemy:draw()
	if self.alive then

		if not self.o_w then self.o_w = self.w; self.o_h = self.h end
		local opx, opy = self.pos.x, self.pos.y
		-- Object.drawDebug(self)
		if self.flip == 0 then
			self.pos.x = self.pos.x+self.sprite:getSpriteX()
		else
			self.pos.x = self.pos.x+self.sprite:getWidth()-(self.sprite:getSpriteX()+self.sprite:getTexWidth())
		end
		self.pos.y = self.pos.y+self.sprite:getSpriteY()
		self.w = (self.o_w/self.sprite:getWidth())*self.sprite:getTexWidth()
		self.h = (self.o_h/self.sprite:getHeight())*self.sprite:getTexHeight()

		if self.currentAnimation == self.hitAnimation then
			if self.flip == 0 then
				self.pos:add(kidHangOffset)
			else
				self.pos:sub(kidHangOffsetFlip)
			end
		end
		Object.draw(self)
		self.w, self.h = self.o_w, self.o_h
		self.pos.x, self.pos.y = opx, opy

		if self.particles then self.particles:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale) end
	end
end

function KidEnemy:kill()
	Object.kill(self)
	mainState.particles:addRecycle(DeathSpin(self.pos.x+self.w/2, self.pos.y+self.h/2, self.sprite, self.tint, self.sheet, self.flip)) 
	pd:startList("fromOF")
	pd:addSymbol("kid_kill")
	pd:addFloat(1)
	pd:finish("fromOF")
end
-- move the missile to a random distance from the center of the building
function KidEnemy:initialPosition()
  local limits = self.world:getTowerLimits()
  limits.min = Vec2(limits.minX, limits.minY)
  limits.max = Vec2(limits.maxX, limits.maxY)
  local x_dist = math.random(100, 200)
  x_dist = x_dist*r_scale
  if math.random() < 0.5 then
    self.pos.x, self.pos.y=limits.min.x-x_dist,0
  else
    self.pos.x, self.pos.y=limits.max.x+x_dist,0
  end
  self.b2d:setPosition(self.pos.x+self.w/2, self.pos.y+self.h/2)
end

function KidEnemy:update()
	if self.particles then self.particles:update(bludG.elapsed) end
	if Enemy.isFrozen(self) then return end
	self.sprite = self.currentAnimation:getSprite()
	self.acceleration.x = 0;
	self.hittingRoom = nil
	-- run a check to see if we are touching a room
	local touchingRoom = false
	self.hittingRoom = false
	if self.touching.other then
		for i,v in ipairs(self.touching.other) do
			if v:is_a(Room) then touchingRoom = v; break; end
		end
	end
	if touchingRoom and touchingRoom.alive then
		self.hittingRoom = touchingRoom
		self.currentAnimation = self.hitAnimation
		if self.touching.LEFT then self.flip = 1 else self.flip = 0 end
	else
		if self.touching.DOWN then
			self.currentAnimation = self.runAnimation
			-- if we are on the ground, then we should move towards the nearest room
			if not self.nearestRoom or not self.nearestRoom.alive then
				self.nearestRoom = findNearest(self.world.rooms.members, self:center())
			end
			if self.nearestRoom then
				-- if the room is within a certain distance, then jump at it.
				if self.nearestRoom:center():distance(self:center()) < 100 then
					local jumpDir = self.nearestRoom:center()
					jumpDir:sub(self:center())
					jumpDir:mult(0.8)
					jumpDir.y = jumpDir.y*8;
					self.velocity:add(jumpDir)
				elseif self.nearestRoom:center().x < self:center().x then
					self.flip = 1
					self.acceleration.x = -self.x_speed
				elseif self.nearestRoom:center().x > self:center().x then
					self.flip = 0
					self.acceleration.x = self.x_speed
				end
			end
		end
		Enemy.update(self)
		-- bang the kid off the groundplane, and the building, and the other enemies
		self.world.groundPlane:overlap(self, nil, Object.separate)
		self.world.rooms:overlap(self, nil, Object.separate)
	end
end

