public void AsMouse_ShouldThrowException_WhenVariantIsNotMouse() { // Arrange var hardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Space); // Act // Assert Assert.That(() => hardwareInputVariant.AsMouse(), Throws.TypeOf <InvalidOperationForCurrentVariantException>()); }
public void AsKeyboard_ShouldReturnKeyboardKey_WhenVariantIsKeyboard() { // Arrange const Key key = Key.Space; var hardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(key); // Act // Assert Assert.That(hardwareInputVariant.AsKeyboard(), Is.EqualTo(key)); }
public void CreateKeyboardVariant_ShouldSetCurrentVariantToKeyboard() { // Arrange // Act var hardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Space); // Assert Assert.That(hardwareInputVariant.CurrentVariant, Is.EqualTo(HardwareInputVariant.Variant.Keyboard)); }
public Entity CreateBird() { var entity = new Entity { Name = "Bird" }; entity.AddComponent(new Transform2DComponent { Translation = Vector2.Zero, Rotation = 0, Scale = new Vector2(2, 2) }); entity.AddComponent(new SpriteRendererComponent { SortingLayerName = "Bird" }); entity.AddComponent(new InputComponent { InputMapping = new InputMapping { ActionMappings = { new ActionMapping { ActionName = "Flap", HardwareActions = { new HardwareAction { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Space) } } } } } }); entity.AddComponent(new BirdIdleFlyingComponent()); var spriteAnimationComponent = new SpriteAnimationComponent { PlayInLoop = true }; spriteAnimationComponent.AddAnimation("Flap", _assetStore.GetAsset <SpriteAnimation>(new AssetId(new Guid("FD8C8E61-A4B0-43C9-A89D-B22554B8A3F7")))); entity.AddComponent(spriteAnimationComponent); entity.AddComponent(new BirdFlapAnimationComponent()); entity.AddComponent(new RectangleColliderComponent { Dimension = new Vector2(32 - 2, 24 - 2) }); entity.AddComponent(new BirdSoundComponent( audioPlayer: _audioPlayer, wingSound: _assetStore.GetAsset <ISound>(new AssetId(new Guid("4ee1890b-3b92-45bb-9bee-a81e270f61d6"))), hitSound: _assetStore.GetAsset <ISound>(new AssetId(new Guid("7224f1b5-1471-4741-b720-0a10fc99ea53"))), dieSound: _assetStore.GetAsset <ISound>(new AssetId(new Guid("d1235819-13d0-419f-a50d-71478c1ad9bd"))) )); return(entity); }
public void GetAsset_ShouldLoadAndReturn_InputMapping() { // Arrange var assetsDirectoryPath = Utils.GetPathUnderTestDirectory("Assets"); SystemUnderTest.AssetStore.RegisterAssets(assetsDirectoryPath); // Act var inputMapping = SystemUnderTest.AssetStore.GetAsset <InputMapping>(AssetsIds.TestInputMapping); // Assert Assert.That(inputMapping, Is.Not.Null); Assert.That(inputMapping.ActionMappings.Count, Is.EqualTo(2)); // Action mapping Assert.That(inputMapping.ActionMappings[0].ActionName, Is.EqualTo("Jump")); Assert.That(inputMapping.ActionMappings[0].HardwareActions.Count, Is.EqualTo(1)); Assert.That(inputMapping.ActionMappings[0].HardwareActions[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.Space))); Assert.That(inputMapping.ActionMappings[1].ActionName, Is.EqualTo("Fire")); Assert.That(inputMapping.ActionMappings[1].HardwareActions.Count, Is.EqualTo(1)); Assert.That(inputMapping.ActionMappings[1].HardwareActions[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.LeftButton))); Assert.That(inputMapping.AxisMappings.Count, Is.EqualTo(4)); // Axis mapping 1 Assert.That(inputMapping.AxisMappings[0].AxisName, Is.EqualTo("MoveForward")); Assert.That(inputMapping.AxisMappings[0].HardwareAxes.Count, Is.EqualTo(2)); Assert.That(inputMapping.AxisMappings[0].HardwareAxes[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.W))); Assert.That(inputMapping.AxisMappings[0].HardwareAxes[0].Scale, Is.EqualTo(1)); Assert.That(inputMapping.AxisMappings[0].HardwareAxes[1].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.S))); Assert.That(inputMapping.AxisMappings[0].HardwareAxes[1].Scale, Is.EqualTo(-1)); // Axis mapping 2 Assert.That(inputMapping.AxisMappings[1].AxisName, Is.EqualTo("MoveRight")); Assert.That(inputMapping.AxisMappings[1].HardwareAxes.Count, Is.EqualTo(2)); Assert.That(inputMapping.AxisMappings[1].HardwareAxes[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.D))); Assert.That(inputMapping.AxisMappings[1].HardwareAxes[0].Scale, Is.EqualTo(1)); Assert.That(inputMapping.AxisMappings[1].HardwareAxes[1].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.A))); Assert.That(inputMapping.AxisMappings[1].HardwareAxes[1].Scale, Is.EqualTo(-1)); // Axis mapping 3 Assert.That(inputMapping.AxisMappings[2].AxisName, Is.EqualTo("LookUp")); Assert.That(inputMapping.AxisMappings[2].HardwareAxes.Count, Is.EqualTo(1)); Assert.That(inputMapping.AxisMappings[2].HardwareAxes[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.AxisX))); Assert.That(inputMapping.AxisMappings[2].HardwareAxes[0].Scale, Is.EqualTo(1)); // Axis mapping 4 Assert.That(inputMapping.AxisMappings[3].AxisName, Is.EqualTo("LookRight")); Assert.That(inputMapping.AxisMappings[3].HardwareAxes.Count, Is.EqualTo(1)); Assert.That(inputMapping.AxisMappings[3].HardwareAxes[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.AxisY))); Assert.That(inputMapping.AxisMappings[3].HardwareAxes[0].Scale, Is.EqualTo(1)); }
public void AddInputWithSampleKeyboardAxisMappings(out InputComponent inputComponent, out AxisMapping moveUp, out AxisMapping moveRight) { moveUp = new AxisMapping { AxisName = nameof(moveUp) }; moveUp.HardwareAxes.Add(new HardwareAxis { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Up), Scale = 1.0 }); moveUp.HardwareAxes.Add(new HardwareAxis { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Down), Scale = -1.0 }); moveUp.HardwareAxes.Add(new HardwareAxis { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Space), Scale = 5.0 }); moveRight = new AxisMapping { AxisName = nameof(moveRight) }; moveRight.HardwareAxes.Add(new HardwareAxis { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Right), Scale = 1.0 }); moveRight.HardwareAxes.Add(new HardwareAxis { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Left), Scale = -1.0 }); var inputMapping = new InputMapping(); inputMapping.AxisMappings.Add(moveUp); inputMapping.AxisMappings.Add(moveRight); inputComponent = new InputComponent { InputMapping = inputMapping }; var entity = new Entity(); entity.AddComponent(inputComponent); _scene.AddEntity(entity); }
public void AddInputWithSampleKeyboardActionMappings(out InputComponent inputComponent, out ActionMapping moveRight, out ActionMapping moveLeft, out ActionMapping jump) { moveRight = new ActionMapping { ActionName = nameof(moveRight) }; moveRight.HardwareActions.Add(new HardwareAction { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Right) }); moveLeft = new ActionMapping { ActionName = nameof(moveLeft) }; moveLeft.HardwareActions.Add(new HardwareAction { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Left) }); jump = new ActionMapping { ActionName = nameof(jump) }; jump.HardwareActions.Add(new HardwareAction { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Up) }); jump.HardwareActions.Add(new HardwareAction { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Space) }); var inputMapping = new InputMapping(); inputMapping.ActionMappings.Add(moveRight); inputMapping.ActionMappings.Add(moveLeft); inputMapping.ActionMappings.Add(jump); inputComponent = new InputComponent { InputMapping = inputMapping }; var entity = new Entity(); entity.AddComponent(inputComponent); _scene.AddEntity(entity); }
public object LoadAsset(AssetInfo assetInfo, IAssetStore assetStore) { using var fileStream = _fileSystem.GetFile(assetInfo.AssetFilePath).OpenRead(); var assetData = AssetData.Load(fileStream); var inputMappingAssetContent = assetData.ReadJsonContent <InputMappingAssetContent>(); if (inputMappingAssetContent.ActionMappings == null) { throw new InvalidOperationException($"{nameof(InputMappingAssetContent)}.{nameof(InputMappingAssetContent.ActionMappings)} cannot be null."); } if (inputMappingAssetContent.AxisMappings == null) { throw new InvalidOperationException($"{nameof(InputMappingAssetContent)}.{nameof(InputMappingAssetContent.AxisMappings)} cannot be null."); } var inputMapping = new InputMapping(); foreach (var(actionName, serializableHardwareActions) in inputMappingAssetContent.ActionMappings) { var actionMapping = new ActionMapping { ActionName = actionName }; foreach (var serializableHardwareAction in serializableHardwareActions) { if (serializableHardwareAction.Key != null && serializableHardwareAction.MouseButton == null) { actionMapping.HardwareActions.Add(new HardwareAction { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(serializableHardwareAction.Key.Value) }); } else if (serializableHardwareAction.Key == null && serializableHardwareAction.MouseButton != null) { var mouseVariant = serializableHardwareAction.MouseButton switch { MouseButton.LeftButton => HardwareInputVariant.MouseVariant.LeftButton, MouseButton.MiddleButton => HardwareInputVariant.MouseVariant.MiddleButton, MouseButton.RightButton => HardwareInputVariant.MouseVariant.RightButton, MouseButton.XButton1 => HardwareInputVariant.MouseVariant.XButton1, MouseButton.XButton2 => HardwareInputVariant.MouseVariant.XButton2, _ => throw new ArgumentOutOfRangeException(nameof(SerializableHardwareAction.MouseButton), serializableHardwareAction.MouseButton, "Unsupported mouse button found in input mapping.") }; actionMapping.HardwareActions.Add(new HardwareAction { HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(mouseVariant) }); } else { throw InvalidInputMappingAssetContentException.CreateForInvalidHardwareAction(inputMappingAssetContent); } } inputMapping.ActionMappings.Add(actionMapping); } foreach (var(axisName, serializableHardwareAxes) in inputMappingAssetContent.AxisMappings) { var axisMapping = new AxisMapping { AxisName = axisName }; foreach (var serializableHardwareAxis in serializableHardwareAxes) { if (serializableHardwareAxis.Key != null && serializableHardwareAxis.MouseAxis == null) { axisMapping.HardwareAxes.Add(new HardwareAxis { HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(serializableHardwareAxis.Key.Value), Scale = serializableHardwareAxis.Scale }); } else if (serializableHardwareAxis.Key == null && serializableHardwareAxis.MouseAxis != null) { var mouseVariant = serializableHardwareAxis.MouseAxis switch { MouseAxis.AxisX => HardwareInputVariant.MouseVariant.AxisX, MouseAxis.AxisY => HardwareInputVariant.MouseVariant.AxisY, _ => throw new ArgumentOutOfRangeException(nameof(SerializableHardwareAxis.MouseAxis), serializableHardwareAxis.MouseAxis, "Unsupported mouse axis found in input mapping.") }; axisMapping.HardwareAxes.Add(new HardwareAxis { HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(mouseVariant), Scale = serializableHardwareAxis.Scale }); } else { throw InvalidInputMappingAssetContentException.CreateForInvalidHardwareAxis(inputMappingAssetContent); } } inputMapping.AxisMappings.Add(axisMapping); } return(inputMapping); }