-- initialization/finalization stuff


function makeGyro(humanoid)
	Gyro = Instance.new("BodyGyro")
	Gyro.Name = "SkateboardGyro"
	Gyro.maxTorque = Vector3.new(0,0,0) -- start off
	Gyro.P = 65
	Gyro.D = 65
	Gyro.Parent = humanoid.Parent.Torso
end

function onButtonChanged(button)
	if button == Enum.Button.Dismount then
		Board.ControllingHumanoid.Jump = Controller:getButton(Enum.Button.Dismount)
	end
	if button == Enum.Button.Jump then
		if Controller:getButton(Enum.Button.Jump) and jumpok then
			jumpok = false
			KickAnim:Stop()
			LeftAnim:Stop(.5)

			RightAnim:Stop(.5)
			OllieAnim:Play(0,1,4)
			Board.StickyWheels = false
			Board:ApplySpecificImpulse(Vector3.new(0,50,0))
			OllieHack.force = Vector3.new(0,1400,0)
			didAction("jump")
			delay(.1, function() if (OllieHack ~= nil) then OllieHack.force = Vector3.new(0,0,0) end end)
		else
			Board.StickyWheels = true
		end
	end
end

function onEquip(humanoid, controller)
	print("Board equipped")
	print(controller)
	if not Controller then -- debounce
		Controller = controller
		
		makeGyro(humanoid)
		controller.AxisChanged:connect(onAxisChanged)
		Board.MoveStateChanged:connect(onMoveStateChanged)
		CoastingAnim = humanoid:loadAnimation(Board.coastingpose)	
		LeftAnim = humanoid:loadAnimation(Board.leftturn)
		RightAnim = humanoid:loadAnimation(Board.rightturn)
		OllieAnim = humanoid:loadAnimation(Board.ollie)
		--OllieAnim:AdjustSpeed(2)
		KickAnim = humanoid:loadAnimation(Board.boardkick)
		CoastingAnim:Play()

		Controller:bindButton(Enum.Button.Jump, "Ollie")
		Controller:bindButton(Enum.Button.Dismount, "Dismount")
		Controller.ButtonChanged:connect(onButtonChanged)
	end
end

function onUnequip(board)
	print("Board unequipped")
	Controller = nil
	Gyro.Parent = nil
	Gyro= nil
	CoastingAnim:Stop()
	KickAnim:Stop()
	CoastingAnim = nil
	KickAnim = nil
	LeftAnim:Stop()
	LeftAnim = nil
	RightAnim:Stop()
	RightAnim = nil
	OllieAnim:Stop()
	OllieAnim = nil

	if (OllieThrust ~= nil) then
		OllieThrust:Remove()
		OllieThrust = nil
	end

	Board.CruiseLoop:Stop()
end
