--Disable script if car is removed from workspace

Car.AncestryChanged:Connect(function()
	if not Car:IsDescendantOf(Workspace) then
		unbindActions()
		
		LocalVehicleSeating.ExitSeat()
		LocalVehicleSeating.DisconnectFromSeatExitEvent(onExitSeat)
		-- stop seated anim
		print("car removed from workspace")
		
		script.Disabled = true
	end
end)

local function getInputValue(keyInfo, inputObj)
	for _, keyCodeInfo in ipairs(keyInfo) do
		if inputObj.KeyCode == keyCodeInfo.KeyCode then
			if keyCodeInfo.Axis then
				local inputVector = inputObj.Position * keyCodeInfo.Axis
				local sign = math.sign(inputVector.X + inputVector.Y + inputVector.Z)
				return inputVector.magnitude * sign
			else
				return 1
			end
		end
	end
	return 0
end

local function exitVehicle(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		LocalVehicleSeating.ExitSeat()
		-- stop seated anim
	end
end

ContextActionService:BindAction(
	EXIT_ACTION_NAME,
	exitVehicle,
	false,
	Keymap.EnterVehicleGamepad,
	Keymap.EnterVehicleKeyboard
)

local function steerLeftAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		SteerLeftValue = getInputValue(Keymap.SteerLeft, inputObj)
	else
		SteerLeftValue = 0
	end
	return Enum.ContextActionResult.Pass
end

ContextActionService:BindActionAtPriority(
	STEER_LEFT_ACTION_NAME,
	steerLeftAction,
	false,
	Enum.ContextActionPriority.High.Value,
	unpack(Keymap.KeysForAction("SteerLeft"))
)

local function steerRightAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		SteerRightValue = getInputValue(Keymap.SteerRight, inputObj)
	else
		SteerRightValue = 0
	end
	return Enum.ContextActionResult.Pass
end

ContextActionService:BindActionAtPriority(
	STEER_RIGHT_ACTION_NAME,
	steerRightAction,
	false,
	Enum.ContextActionPriority.High.Value,
	unpack(Keymap.KeysForAction("SteerRight"))
)

local function throttleAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		ThrottleValue = getInputValue(Keymap.Throttle, inputObj)
	else
		ThrottleValue = 0
	end
end

ContextActionService:BindAction(
	THROTTLE_ACTION_NAME,
	throttleAction,
	false,
	unpack(Keymap.KeysForAction("Throttle"))
)

local function brakeAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.Change then
		BrakeValue = getInputValue(Keymap.Brake, inputObj)
	else
		BrakeValue = 0
	end
end

ContextActionService:BindAction(
	BRAKE_ACTION_NAME,
	brakeAction,
	false,
	unpack(Keymap.KeysForAction("Brake"))
)

local function handBrakeAction(action, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		HandBrakeValue = 1
	else
		HandBrakeValue = 0
	end
end

ContextActionService:BindAction(
	HAND_BRAKE_ACTION_NAME,
	handBrakeAction,
	false,
	unpack(Keymap.KeysForAction("Handbrake"))
)
