-- obtain goals based on Center part

center.BodyPosition.position = beacon.Center.Position
teamColor = center.Color
vel = center.BodyVelocity

healerIsResting = false

function restHealer()
	healerIsResting = true
	wait(5)
	healerIsResting = false
end

function joinTeam(humanoid)
	local team = Instance.new("Color3Value")
	team.Value = teamColor
	team.Name = "Team"
	team.Parent = humanoid
	humanoid.Torso.Color = teamColor
	humanoid.Torso.Reflectance = 0.2
	humanoid.LeftLeg.Color = teamColor
	humanoid.LeftLeg.Reflectance = 0.2
	humanoid.RightLeg.Color = teamColor
	humanoid.RightLeg.Reflectance = 0.2
end

function onTouchHumanoid(humanoid)
	local team = humanoid:findFirstChild("Team", false)
	if team==nil then
		joinTeam(humanoid)
	elseif team.Value~=teamColor then
		-- Harm the enemy!
		humanoid.Health = 0
		return
	end
	
	if not healerIsResting then
		-- Heal the player
		humanoid.Health = humanoid.Health + 50
		coroutine.resume(coroutine.create(restHealer))
	end
end

goingUp = false
function goUp(speed)
	if goingUp then
		return
	end
	goingUp = true
	vel.velocity = Vector3.new(0,speed,0)
	wait(20/speed)
	vel.velocity = Vector3.new(0,-2,0)
	goingUp = false
end

function onTouch(touchedPart)
	-- see if a character touched it
	local parent = touchedPart.Parent
		if parent~=nill then
		local humanoid = parent:findFirstChild("Humanoid", false);
		if humanoid ~= nill then
			onTouchHumanoid(humanoid)
			goUp(10)
			return
		end
	end

	-- change direction
	if touchedPart.Position.y > beacon.Center.Position.y then
		vel.velocity = Vector3.new(0,-2,0)
		goingUp = false
	elseif touchedPart.Position.y < beacon.Center.Position.y then
		goUp(2)
	end
end

function connectPart(part)
	part.Color = teamColor
	part.Touched:connect(onTouch)
end
