local W={}W.__index=W function W.new(S)local v=setmetatable({},W)v.CONTROL_ACTION_PRIORITY=S v.enabled=false v.vehicleSeat=nil v.throttle=0 v.steer=0 v.acceleration=0 v.decceleration=0 v.turningRight=0 v.turningLeft=0 v.vehicleMoveVector=ZERO_VECTOR3 return v end function W.BindContextActions(W)if useTriggersForThrottle then ContextActionService:BindActionAtPriority("\116\104\114\111\116\116\108\101\065\099\099\101\108",function(S,v,G)W:OnThrottleAccel(S,v,G)return Enum.ContextActionResult.Pass end,false,W.CONTROL_ACTION_PRIORITY,Enum.KeyCode.ButtonR2)ContextActionService:BindActionAtPriority("\116\104\114\111\116\116\108\101\068\101\099\099\101\108",function(S,v,G)W:OnThrottleDeccel(S,v,G)return Enum.ContextActionResult.Pass end,false,W.CONTROL_ACTION_PRIORITY,Enum.KeyCode.ButtonL2)end ContextActionService:BindActionAtPriority("\097\114\114\111\119\083\116\101\101\114\082\105\103\104\116",function(S,v,G)W:OnSteerRight(S,v,G)return Enum.ContextActionResult.Pass end,false,W.CONTROL_ACTION_PRIORITY,Enum.KeyCode.Right)ContextActionService:BindActionAtPriority("\097\114\114\111\119\083\116\101\101\114\076\101\102\116",function(S,v,G)W:OnSteerLeft(S,v,G)return Enum.ContextActionResult.Pass end,false,W.CONTROL_ACTION_PRIORITY,Enum.KeyCode.Left)end function W.Enable(v,W,S)if W==v.enabled and S==v.vehicleSeat then return end v.vehicleMoveVector=ZERO_VECTOR3 if W then if S then v.vehicleSeat=S if FFlagPlayerScriptsBindAtPriority then v:BindContextActions()else if useTriggersForThrottle then ContextActionService:BindAction("\116\104\114\111\116\116\108\101\065\099\099\101\108",function(W,S,G)v:OnThrottleAccel(W,S,G)return Enum.ContextActionResult.Pass end,false,Enum.KeyCode.ButtonR2)ContextActionService:BindAction("\116\104\114\111\116\116\108\101\068\101\099\099\101\108",function(W,S,G)v:OnThrottleDeccel(W,S,G)return Enum.ContextActionResult.Pass end,false,Enum.KeyCode.ButtonL2)end ContextActionService:BindAction("\097\114\114\111\119\083\116\101\101\114\082\105\103\104\116",function(W,S,G)v:OnSteerRight(W,S,G)return Enum.ContextActionResult.Pass end,false,Enum.KeyCode.Right)ContextActionService:BindAction("\097\114\114\111\119\083\116\101\101\114\076\101\102\116",function(W,S,G)v:OnSteerLeft(W,S,G)return Enum.ContextActionResult.Pass end,false,Enum.KeyCode.Left)end end else if useTriggersForThrottle then ContextActionService:UnbindAction("\116\104\114\111\116\116\108\101\065\099\099\101\108")ContextActionService:UnbindAction("\116\104\114\111\116\116\108\101\068\101\099\099\101\108")end ContextActionService:UnbindAction("\097\114\114\111\119\083\116\101\101\114\082\105\103\104\116")ContextActionService:UnbindAction("\097\114\114\111\119\083\116\101\101\114\076\101\102\116")v.vehicleSeat=nil end end function W.OnThrottleAccel(G,W,S,v)G.acceleration=S~=Enum.UserInputState.End and-1 or 0 G.throttle=G.acceleration+G.decceleration end function W.OnThrottleDeccel(G,W,S,v)G.decceleration=S~=Enum.UserInputState.End and 1 or 0 G.throttle=G.acceleration+G.decceleration end function W.OnSteerRight(G,W,S,v)G.turningRight=S~=Enum.UserInputState.End and 1 or 0 G.steer=G.turningRight+G.turningLeft end function W.OnSteerLeft(G,W,S,v)G.turningLeft=S~=Enum.UserInputState.End and-1 or 0 G.steer=G.turningRight+G.turningLeft end
-- Note that VehicleController does not derive from BaseCharacterController, it is a special case
local VehicleController = {}
VehicleController.__index = VehicleController

function VehicleController.new(CONTROL_ACTION_PRIORITY)
	local self = setmetatable({}, VehicleController)

	self.CONTROL_ACTION_PRIORITY = CONTROL_ACTION_PRIORITY

	self.enabled = false
	self.vehicleSeat = nil
	self.throttle = 0
	self.steer = 0

	self.acceleration = 0
	self.decceleration = 0
	self.turningRight = 0
	self.turningLeft = 0

	self.vehicleMoveVector = ZERO_VECTOR3

	return self
end

function VehicleController:BindContextActions()
	if useTriggersForThrottle then
		ContextActionService:BindActionAtPriority("throttleAccel", function(actionName, inputState, inputObject)
			self:OnThrottleAccel(actionName, inputState, inputObject)
			return Enum.ContextActionResult.Pass
		end, false, self.CONTROL_ACTION_PRIORITY, Enum.KeyCode.ButtonR2)
		ContextActionService:BindActionAtPriority("throttleDeccel", function(actionName, inputState, inputObject)
			self:OnThrottleDeccel(actionName, inputState, inputObject)
			return Enum.ContextActionResult.Pass
		end, false, self.CONTROL_ACTION_PRIORITY, Enum.KeyCode.ButtonL2)
	end
	ContextActionService:BindActionAtPriority("arrowSteerRight", function(actionName, inputState, inputObject)
		self:OnSteerRight(actionName, inputState, inputObject)
		return Enum.ContextActionResult.Pass
	end, false, self.CONTROL_ACTION_PRIORITY, Enum.KeyCode.Right)
	ContextActionService:BindActionAtPriority("arrowSteerLeft", function(actionName, inputState, inputObject)
		self:OnSteerLeft(actionName, inputState, inputObject)
		return Enum.ContextActionResult.Pass
	end, false, self.CONTROL_ACTION_PRIORITY, Enum.KeyCode.Left)
end

function VehicleController:Enable(enable, vehicleSeat)
	if enable == self.enabled and vehicleSeat == self.vehicleSeat then
		return
	end

	self.vehicleMoveVector = ZERO_VECTOR3

	if enable then
		if vehicleSeat then
			self.vehicleSeat = vehicleSeat
			if FFlagPlayerScriptsBindAtPriority then
				self:BindContextActions()
			else
				if useTriggersForThrottle then
					ContextActionService:BindAction("throttleAccel", function(actionName, inputState, inputObject)
						self:OnThrottleAccel(actionName, inputState, inputObject)
						return Enum.ContextActionResult.Pass
					end, false, Enum.KeyCode.ButtonR2)
					ContextActionService:BindAction("throttleDeccel", function(actionName, inputState, inputObject)
						self:OnThrottleDeccel(actionName, inputState, inputObject)
						return Enum.ContextActionResult.Pass
					end, false, Enum.KeyCode.ButtonL2)
				end
				ContextActionService:BindAction("arrowSteerRight", function(actionName, inputState, inputObject)
					self:OnSteerRight(actionName, inputState, inputObject)
					return Enum.ContextActionResult.Pass
				end, false, Enum.KeyCode.Right)
				ContextActionService:BindAction("arrowSteerLeft", function(actionName, inputState, inputObject)
					self:OnSteerLeft(actionName, inputState, inputObject)
					return Enum.ContextActionResult.Pass
				end, false, Enum.KeyCode.Left)
			end
		end
	else
		if useTriggersForThrottle then
			ContextActionService:UnbindAction("throttleAccel")
			ContextActionService:UnbindAction("throttleDeccel")
		end
		ContextActionService:UnbindAction("arrowSteerRight")
		ContextActionService:UnbindAction("arrowSteerLeft")
		self.vehicleSeat = nil
	end
end

function VehicleController:OnThrottleAccel(actionName, inputState, inputObject)
	self.acceleration = (inputState ~= Enum.UserInputState.End) and -1 or 0
	self.throttle = self.acceleration + self.decceleration
end

function VehicleController:OnThrottleDeccel(actionName, inputState, inputObject)
	self.decceleration = (inputState ~= Enum.UserInputState.End) and 1 or 0
	self.throttle = self.acceleration + self.decceleration
end

function VehicleController:OnSteerRight(actionName, inputState, inputObject)
	self.turningRight = (inputState ~= Enum.UserInputState.End) and 1 or 0
	self.steer = self.turningRight + self.turningLeft
end

function VehicleController:OnSteerLeft(actionName, inputState, inputObject)
	self.turningLeft = (inputState ~= Enum.UserInputState.End) and -1 or 0
	self.steer = self.turningRight + self.turningLeft
end
