playBub = {
	color = { 0.7, 0.4, 0.5 },
	score = 0,
  push=0,
	radSt = 30,
	powerTime = 0,
}

function playBub:resetAt(x, y)
	--Player Bubble
	self.is_active = true
	self.score = 0
  self.push=0
	self.win = false
	self:setColl(x, y, self.radSt)
end
function playBub:setColl(x, y, r)
	if self.collider then
		self.collider:destroy()
		self.collider = nil
	end
	self.collider = world:newCircleCollider(x, y, r)
	self.collider:setRestitution(0.9)
	self.collider:setCollisionClass("Bubble")
end

function playBub:keyrls(key)
	if key == "space" and self.is_active then
		--apply fan
		local pwFct = 1.5 - math.cos(self.powerTime)
		local fanfx = fan.force * fan.dx * pwFct
		local fanfy = fan.force * fan.dy * pwFct
		self.collider:applyLinearImpulse(fanfx, fanfy)
    MoveSund:play()
    self.push=self.push+1
		self.powerTime = 0
	end
end

function playBub:upd(dt)
	local pX, pY = self.collider:getPosition()
	local pRad = self.collider:getRadius()
	--air friction
	local pVx, pVy = self.collider:getLinearVelocity()
	local aFc = 0.01
	self.collider:applyForce(-aFc * pRad ^ 2 * pVx, -aFc * pRad ^ 2 * pVy)
	--add powerTime
	if love.keyboard.isDown("space") and self.is_active then
		self.powerTime = self.powerTime + 10 * dt
	end

	--collider event
	--Spike
	if self.collider:enter("Spike") then
    DeadSund:play()
		self.is_active = false
		--dead
		--restart
		resetLevel()
	end
	--Petite Bubble
	if self.collider:enter("PetBubble") then
		local coll_data = self.collider:getEnterCollisionData("PetBubble")
		local pBub = coll_data.collider:getObject()
		pBub.collider:destroy()
		table.remove(petBubs, pBub.id)
		updTableId(petBubs)
		self.score = self.score + 1
		local vx, vy = self.collider:getLinearVelocity()
		self:setColl(pX, pY, pRad + 4)
		self.collider:setLinearVelocity(vx, vy)
	end
	--Turner
	coRun(turnAtrCo)
	if self.collider:exit("Turner") then
		turnAtrCo = nil
	end

	if self.collider:enter("Turner") then
		local coll_data = self.collider:getEnterCollisionData("Turner")
		local turner = coll_data.collider:getObject()
		if not turnAtrCo then
			turnAtrCo = coroutine.create(function()
				local prog = 0
				while prog < 2 do
          local x,y=self.collider:getPosition()
          local ox,oy=turner.cX-x,turner.cY-y
          local oRad=math.sqrt(ox^2+oy^2)
          prog = math.min(prog + dt, 2)
          self.collider:applyForce(5000*ox/oRad,5000*oy/oRad)

					coroutine.yield()
				end
        self.collider:setLinearVelocity(0,0)
        self.collider:setPosition(turner.cX,turner.cY)
				self.collider:applyLinearImpulse(turner.dirX * turner.force, turner.dirY * turner.force)
			end)
		end
	end
	--Active tube and win the game
	local tube = walls.Tube[1]
	if tube and tube.is_active then
		if self.collider:enter("Tube") then
			self.win = true
			self.is_active = false
      State="Fini"
		end
	end
end

function playBub:draw()
	love.graphics.setColor(1,1,1)
	local pX, pY = self.collider:getPosition()
	local pRad = self.collider:getRadius()

	--wind direction
	love.graphics.arc(
		"line",
		"open",
		pX,
		pY,
		(2.3 - math.cos(self.powerTime)) * pRad,
		fan.angle + math.pi - 0.5,
		fan.angle + math.pi + 0.5
	)
	--draw player Bubble
  local animId= math.ceil(AnimT%2+0.01)
  local img = PlayBubAnim[animId]
  if img then
    local sc= 3.5*pRad/img:getWidth()
  love.graphics.draw(img,pX,pY,nil,sc,sc,img:getWidth()/2,img:getHeight()/2)
  else
	love.graphics.setColor(self.color)
	love.graphics.circle("line", pX, pY, pRad)
  end
end
