public static SpectatorActionMap Create(ActionMapConfig <SpectatorAction> actionMapConfig, IClock clock) { var inputSettings = actionMapConfig.InputSettings; var inputMapping = actionMapConfig.InputMapping; var controllerPeripheral = Peripherals.Controller.GetPeripheral(actionMapConfig.ControllerId, inputSettings); var sourceMap = ImperoCore.MergeAll <InputSource, float>( Adapters.MergeAxes, Peripherals.Mouse.PolarizedAxes .Adapt <InputSource, float, float>(input => input * inputSettings.WingsuitMouseSensitivity), (controllerPeripheral.Axes), Peripherals.Keyboard.Adapt <InputSource, ButtonState, float>(Adapters.Button2Axis), Peripherals.Mouse.Buttons.Adapt <InputSource, ButtonState, float>(Adapters.Button2Axis), controllerPeripheral.Buttons.Adapt <InputSource, ButtonState, float>(Adapters.Button2Axis)); var inputMap = inputMapping.ApplyMapping(sourceMap, Adapters.MergeAxes); var moveForward = inputMap.Source.GetValueOrDefault(SpectatorAction.MoveForward, () => 0.0f); var moveBackward = inputMap.Source.GetValueOrDefault(SpectatorAction.MoveBackward, () => 0.0f); var moveLeft = inputMap.Source.GetValueOrDefault(SpectatorAction.MoveLeft, () => 0.0f); var moveRight = inputMap.Source.GetValueOrDefault(SpectatorAction.MoveRight, () => 0.0f); var moveUp = inputMap.Source.GetValueOrDefault(SpectatorAction.MoveUp, () => 0.0f); var moveDown = inputMap.Source.GetValueOrDefault(SpectatorAction.MoveDown, () => 0.0f); Func <Vector3> movement = () => new Vector3 { x = moveRight() - moveLeft(), y = moveUp() - moveDown(), z = moveForward() - moveBackward() }; var lookUp = inputMap.Source.GetValueOrDefault(SpectatorAction.LookUp, () => 0.0f); var lookDown = inputMap.Source.GetValueOrDefault(SpectatorAction.LookDown, () => 0.0f); var lookLeft = inputMap.Source.GetValueOrDefault(SpectatorAction.LookLeft, () => 0.0f); var lookRight = inputMap.Source.GetValueOrDefault(SpectatorAction.LookRight, () => 0.0f); Func <Vector2> lookDirection = () => new Vector2 { x = lookRight() - lookLeft(), y = lookDown() - lookUp() }; var isSpeedUp = inputMap.Source.GetValueOrDefault(SpectatorAction.SpeedUp, () => 0.0f); return(new SpectatorActionMap(lookDirection, movement, isSpeedUp)); }
public static ParachuteActionMap Create(ActionMapConfig <ParachuteAction> actionMapConfig, float axisButtonThreshold = 0.5f) { var inputSettings = actionMapConfig.InputSettings; var inputMapping = actionMapConfig.InputMapping; var controllerPeripheral = Peripherals.Controller.GetPeripheral(actionMapConfig.ControllerId, inputSettings); var buttonInputSource = ImperoCore.MergeAll( Adapters.MergeButtons, Peripherals.Keyboard, Peripherals.Mouse.Buttons, controllerPeripheral.Buttons); var inputWithAxisSource = inputMapping.ApplyMapping(controllerPeripheral.Axes, Adapters.MergeAxes); var inputWithMouseAxisSource = inputMapping.ApplyMapping(Peripherals.Mouse.PolarizedAxes, Adapters.MergeAxes); var inputWithButtonSource = inputMapping.ApplyMapping(buttonInputSource, Adapters.MergeButtons); bool isMouseInput = inputWithMouseAxisSource.Source.ContainsKey(ParachuteAction.PullBothLines) || inputWithMouseAxisSource.Source.ContainsKey(ParachuteAction.PullLeftLines) || inputWithMouseAxisSource.Source.ContainsKey(ParachuteAction.PullRightLines); // Finally merge all input maps Debug.Log("parachute sensitivity: " + inputSettings.ParachuteMouseSensitivity); var axisInput = ImperoCore.MergeAll( Adapters.MergeAxes, inputWithAxisSource, inputWithMouseAxisSource .Adapt(Adapters.ApplySensitivity(inputSettings.ParachuteMouseSensitivity)) .Adapt(ParachuteAction.PullBothLines, Adapters.Deadzone(deadzone: 0.1f)), // TODO Instead of button2axis use gradual button2axis inputWithButtonSource.Adapt <ParachuteAction, ButtonState, float>(Adapters.Button2Axis)); var buttonInput = ImperoCore.MergeAll( Adapters.MergeButtons, inputWithButtonSource, inputWithMouseAxisSource.Adapt(Adapters.Axis2Button(3f)), inputWithAxisSource.Adapt <ParachuteAction, float, ButtonState>(Adapters.Axis2Button(axisButtonThreshold))); axisInput = axisInput.FillEmptyValues(ParachuteActionDetails.MappableActions, () => 0.0f); buttonInput = buttonInput.FillEmptyValues(ParachuteActionDetails.MappableActions, () => ButtonState.Released); Func <Vector2> weightShift; { var horizontalShiftLeft = axisInput.Source[ParachuteAction.WeightShiftLeft]; var horizontalShiftRight = axisInput.Source[ParachuteAction.WeightShiftRight]; var verticalShiftFront = axisInput.Source[ParachuteAction.WeightShiftFront]; var verticalShiftBack = axisInput.Source[ParachuteAction.WeightShiftBack]; weightShift = () => new Vector2( horizontalShiftRight() - horizontalShiftLeft(), verticalShiftFront() - verticalShiftBack()); } Func <ParachuteLine> pollSelectedLine; Func <Vector2> pullBrakeLines; Func <Vector2> pullRearLines; Func <Vector2> pullFrontLines; { var pullLeft = axisInput.Source[ParachuteAction.PullLeftLines]; var pullRight = axisInput.Source[ParachuteAction.PullRightLines]; var pullBoth = axisInput.Source[ParachuteAction.PullBothLines]; Func <Vector2> pullLines = () => { var linePull = pullBoth(); var leftPull = Mathf.Max(linePull, pullLeft()); var rightPull = Mathf.Max(linePull, pullRight()); return(new Vector2(leftPull, rightPull)); }; var toggleFrontlines = buttonInput.Source[ParachuteAction.HoldFrontLines]; var toggleRearlines = buttonInput.Source[ParachuteAction.HoldRearLines]; pollSelectedLine = () => { if (toggleFrontlines() == ButtonState.Pressed) { return(ParachuteLine.Front); } if (toggleRearlines() == ButtonState.Pressed) { return(ParachuteLine.Rear); } return(ParachuteLine.Brake); }; pullBrakeLines = ImperoCore.MergePollFns( Adapters.MergeAxes, FilterInput(pullLines, v => pollSelectedLine() == ParachuteLine.Brake), CombineAxes(axisInput, ParachuteAction.PullBrakeLineLeft, ParachuteAction.PullBrakeLineRight)); pullRearLines = ImperoCore.MergePollFns( Adapters.MergeAxes, FilterInput(pullLines, v => pollSelectedLine() == ParachuteLine.Rear), CombineAxes(axisInput, ParachuteAction.PullRearLineLeft, ParachuteAction.PullRearLineRight)); pullFrontLines = ImperoCore.MergePollFns( Adapters.MergeAxes, FilterInput(pullLines, v => pollSelectedLine() == ParachuteLine.Front), CombineAxes(axisInput, ParachuteAction.PullFrontLineLeft, ParachuteAction.PullFrontLineRight)); } // var configToggle = buttonInput.Source[ParachuteAction.ParachuteConfigToggle] // .Adapt(Adapters.ButtonEvents(() => Time.frameCount)); Func <ButtonEvent> configToggle = () => ButtonEvent.Nothing; Func <ParachuteInput> pollParachuteInput = () => { var selectedLine = pollSelectedLine(); Vector2 brakes = pullBrakeLines(); Vector2 rearLines = pullRearLines(); Vector2 frontLines = pullFrontLines(); return(new ParachuteInput(selectedLine, brakes, rearLines, frontLines, weightShift(), isMouseInput)); }; return(new ParachuteActionMap(pollParachuteInput, configToggle)); }
public static PilotActionMap Create(ActionMapConfig <WingsuitAction> actionMapConfig, IClock clock) { var inputSettings = actionMapConfig.InputSettings; var inputMapping = actionMapConfig.InputMapping; var controllerPeripheral = Peripherals.Controller.GetPeripheral(actionMapConfig.ControllerId, inputSettings); var buttonSourceMap = ImperoCore.MergeAll( Adapters.MergeButtons, Peripherals.Keyboard, Peripherals.Mouse.Buttons, controllerPeripheral.Buttons); var inputWithButtonSource = inputMapping.ApplyMapping(buttonSourceMap, Adapters.MergeButtons); var inputWithControllerAxisSource = CreateControllerAxisInput(inputMapping, inputSettings, controllerPeripheral); var mouseAxisSourceMap = Peripherals.Mouse.PolarizedAxes; var inputWithMouseAxisSource = inputMapping.ApplyMapping(mouseAxisSourceMap, Adapters.MergeAxes); inputWithMouseAxisSource = AddMouseLook( inputWithButtonSource.Source.GetValueOrDefault(WingsuitAction.ActivateMouseLook, () => ButtonState.Released) .Adapt <ButtonState, bool>(Adapters.ButtonState2Bool), inputWithMouseAxisSource, inputSettings); inputWithMouseAxisSource = AddMousePlayerControls( inputWithButtonSource.Source.GetValueOrDefault(WingsuitAction.ActivateMouseLook, () => ButtonState.Released) .Adapt <ButtonState, bool>(Adapters.ButtonState2Bool) .Adapt <bool, bool>(Adapters.Invert), inputWithMouseAxisSource, inputSettings); // Separate this from all other input, since we have no logical way to convert a mouse axis to // a stick-like axis (an axis with a pivot point) var allMouseInput = ImperoCore.MergeAll( Adapters.MergeAxes, inputWithMouseAxisSource, DerivableAxisInput(inputWithMouseAxisSource)); inputWithMouseAxisSource = inputWithMouseAxisSource.Filter(pilotAction => !PilotBodyMovementActions.Contains(pilotAction)); var inputWithAxisSource = ImperoCore.MergeAll(Adapters.MergeAxes, inputWithControllerAxisSource, inputWithMouseAxisSource); var allAxisInput = ImperoCore.MergeAll( Adapters.MergeAxes, inputWithAxisSource, ButtonsAsAxes(inputWithButtonSource, clock)); var derivedInput = DerivableAxisInput(allAxisInput); // Add the derived input allAxisInput = ImperoCore.MergeAll(Adapters.MergeAxes, allAxisInput, derivedInput); var allButtonInput = ImperoCore.MergeAll( Adapters.MergeButtons, inputWithButtonSource, inputWithAxisSource.Adapt(Adapters.Axis2Button(threshold: 0.5f)), ImperoCore.Adapt(derivedInput, Adapters.Axis2Button(threshold: 0.5f))); allAxisInput = allAxisInput.FillEmptyValues(PilotActionDetails.MappableActions, () => 0.0f); allMouseInput = allMouseInput.FillEmptyValues(PilotActionDetails.MappableActions, () => 0.0f); allButtonInput = allButtonInput.FillEmptyValues(PilotActionDetails.MappableActions, () => ButtonState.Released); // var respawnHoldDuration = (0.5f).Seconds(); // Func<int> currentFrame = () => (int)clock.FrameCount; // var respawn = allButtonInput.Source[PilotAction.Respawn]; // allButtonInput = allButtonInput.Update( // PilotAction.Respawn, // respawn.Adapt(Adapters.MinHoldDuration(new Func<TimeSpan>(clock.PollDeltaTime), respawnHoldDuration)).Cache(currentFrame)); // // Func<TimeSpan> currentTime = () => TimeSpan.FromSeconds(clock.CurrentTime); // allButtonInput = allButtonInput.Update( // PilotAction.SwitchVehicleType, // respawn.Adapt(Adapters.MaxHoldDuration(currentTime, respawnHoldDuration)).Cache(currentFrame)); return(new PilotActionMap(allButtonInput, allAxisInput, allMouseInput)); }
public static MenuActionMap Create(ActionMapConfig <MenuAction> actionMapConfig, float axisButtonThreshold = 0.5f) { var inputSettings = actionMapConfig.InputSettings; var combinedInputMapping = actionMapConfig.InputMapping.Merge(DefaultMenuMapping); var controllerPeripheral = Peripherals.Controller.GetPeripheral(actionMapConfig.ControllerId, inputSettings); var buttonInputSource = ImperoCore.MergeAll( Adapters.MergeButtons, Peripherals.Keyboard, Peripherals.Mouse.Buttons, controllerPeripheral.Buttons); var axisInputSource = ImperoCore.MergeAll( Adapters.MergeAxes, Peripherals.Mouse.PolarizedAxes, controllerPeripheral.Axes); var inputWithAxisSource = combinedInputMapping.ApplyMapping(axisInputSource, Adapters.MergeAxes); var inputWithButtonSource = combinedInputMapping.ApplyMapping(buttonInputSource, Adapters.MergeButtons); // Finally merge all input maps var axisInput = ImperoCore.MergeAll( Adapters.MergeAxes, inputWithAxisSource, inputWithButtonSource.Adapt <MenuAction, ButtonState, float>(Adapters.Button2Axis)); var buttonInput = ImperoCore.MergeAll( Adapters.MergeButtons, inputWithButtonSource, inputWithAxisSource.Adapt <MenuAction, float, ButtonState>(Adapters.Axis2Button(axisButtonThreshold))); buttonInput = buttonInput.FillEmptyValues(MenuActionDetails.MenuActionButtons, () => ButtonState.Released); var moveCursor = axisInput.Adapt <MenuAction, float, Vector2>((menuAction, pollFn) => { Func <Vector2> adaptedFn; if (menuAction == MenuAction.Up) { adaptedFn = () => new Vector2(0, pollFn()); } else if (menuAction == MenuAction.Down) { adaptedFn = () => new Vector2(0, -pollFn()); } else if (menuAction == MenuAction.Left) { adaptedFn = () => new Vector2(-pollFn(), 0); } else if (menuAction == MenuAction.Right) { adaptedFn = () => new Vector2(pollFn(), 0); } else { adaptedFn = () => Vector2.zero; } return(adaptedFn); }); var pollCursor = moveCursor.Merge(Adapters.CombineAxes, MenuAction.Up, MenuAction.Down, MenuAction.Left, MenuAction.Right); var pollDiscreteCursor = pollCursor.Adapt(Adapters.DiscreteAxisInput()); return(new MenuActionMap(pollCursor, pollDiscreteCursor, buttonInput)); }