-- normal maxspeed is 40

local topSpeed = 40

local OllieHack = script.Parent:FindFirstChild("OllieThrust")
if (OllieHack == nil) then
	OllieHack = Instance.new("BodyThrust")
	OllieHack.Name = "OllieThrust"
	OllieHack.force = Vector3.new(0,0,0)
	OllieHack.location = Vector3.new(0,0,-2)
	OllieHack.Parent = Board
end

local lastaction = nil
local lasttime = nil
local jumpok = true

function didAction(action)
	lastaction = action
	lasttime = time()
end



function onAxisChanged(axis)
	Board.Steer = Controller.Steer
	Board.Throttle = Controller.Throttle

	if (Board.Steer == -1) then
		print("Left")
		--CoastingAnim:Stop(1)
		KickAnim:Stop()
		OllieAnim:Stop()
		RightAnim:Stop(.5)
		LeftAnim:Play(.5)
		didAction("left")
	end

	if (Board.Steer == 1) then
		print("Right")
		KickAnim:Stop()
		--CoastingAnim:Stop(1)
		LeftAnim:Stop(.5)
		OllieAnim:Stop()
		RightAnim:Play(.5)
		didAction("right")
	end

	if (Board.Steer == 0) then
		print("Straight")

		KickAnim:Stop()
		LeftAnim:Stop(.5)
		OllieAnim:Stop()
		RightAnim:Stop(.5)
		
		if (lastaction == "go" and time() - lasttime < .1) then
			
		end
		didAction("go")
		--CoastingAnim:Play(1)

		if (Board.CruiseLoop.IsPlaying == false) then Board.CruiseLoop:Play() end
		
	end
end

function enterAirState()
	-- gyrate towards vertical position for better landing
	Gyro.maxTorque = Vector3.new(10, 0, 10)
	jumpok = false
	Board.CruiseLoop:Stop()
	Board.BoardOllie:Play()
end

function exitAirState()
	-- turn off gyro
	Gyro.maxTorque = Vector3.new(0,0,0)
	jumpok = true
	Board.BoardDrop:Play()
	Board.CruiseLoop:Stop()
end

function enterPushingState()
	KickAnim:Play()
end

function enterCoastingState()
	if (Board.CruiseLoop.IsPlaying == false) then Board.CruiseLoop:Play() end
end

function enterStoppingState()
	Board.CruiseLoop:Stop()
	breakModifyAndPlay()
end

function onMoveStateChanged(newState, oldState)
	print(oldState)
	print(newState)
	-- do state exits here
	if oldState == Enum.MoveState.AirFree then exitAirState() end

	-- do state transitions here

	-- do state entries here
	if newState == Enum.MoveState.AirFree then enterAirState() end
	if newState == Enum.MoveState.Pushing then enterPushingState() end
	if newState == Enum.MoveState.Coasting then enterCoastingState() end
	if newState == Enum.MoveState.Stopping then enterStoppingState() end
end

