この質問にはもっとラムダが必要です。
これはWIPからのものであるため、未使用で暗黙的に実装されていない機能がいくつかあることに注意してください。あなたが() =>
追加を見るところinputDirection.X -= -1;
。これはゲームパッド用であり、キーボードを使用していることがわかりますが、グループ化の原則、または少なくともアクションマップの概念は引き続き適用されます。
これはおそらくやり過ぎですが、とても楽しいです!
コントロールをマップするセットアップ機能:
public void SetController(MyGamePad controller)
{
controller.ClearActions();
controller.BindAction(Buttons.Start, ButtonGroup.FullyExclusive, () => sine.play());
controller.BindAction(Buttons.A,
ButtonGroup.FaceButtons, () => showText("A!"));
controller.BindAction(Buttons.A | Buttons.B,
ButtonGroup.FaceButtons, () => showText("A and B!"));
controller.BindAction(Buttons.B,
ButtonGroup.FaceButtons, () => showText("B"));
controller.BindAction(Buttons.B | Buttons.Y,
ButtonGroup.FaceButtons, () => showText("B and Y!"));
controller.BindAction(Buttons.Y,
ButtonGroup.FaceButtons, () => showText("Y!"));
controller.BindAction(Buttons.Y | Buttons.X,
ButtonGroup.FaceButtons, () => showText("Y and X!"));
controller.BindAction(Buttons.X,
ButtonGroup.FaceButtons, () => showText("X!"));
controller.BindAction(Buttons.X | Buttons.A,
ButtonGroup.FaceButtons, () => showText("X n A, I made a funny!"));
controller.BindAction(0x0,
ButtonGroup.FaceButtons, () => showText("Nothing on the face buttons."));
controller.BindAction(0x0,
ButtonGroup.ShoulderButtons, () => showText("No shoulder buttons"));
controller.BindAction(Buttons.LeftShoulder,
ButtonGroup.ShoulderButtons, () => showText("Left shoulder button!"));
controller.BindAction(Buttons.RightShoulder,
ButtonGroup.ShoulderButtons, () => showText("Right shoulder button!"));
controller.BindAction(0x0,
ButtonGroup.DirectionalPad, () => showText("Nothin' on the dpad!"));
controller.BindAction(Buttons.DPadUp,
ButtonGroup.DirectionalPad, () => showText("DPAD UP!"));
controller.BindAction(Buttons.DPadDown,
ButtonGroup.DirectionalPad, () => showText("DPAD DOWN!"));
}
機能させるゲームコンポーネント(1人のプレイヤーにつき1つをインスタンス化):
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace MyGame
{
[Flags]
public enum ButtonGroup
{
NonExclusive = 0x00,
FaceButtons = 0x01,
DirectionalPad = 0x02,
ShoulderButtons = 0x04,
LeftSide = 0x08,
RightSide = 0x10,
FullyExclusive = 0xFF,
}
class MyGamePad : GameComponent
{
private Buttons FaceButtons;
private Buttons DirectionalButtons;
private Buttons ShoulderButtons;
private Buttons LeftSideButtons;
private Buttons RightSideButtons;
private Buttons AllButtons;
private PlayerIndex playerIndex;
private Dictionary<KeyValuePair<Buttons, ButtonGroup>, Action> actionMap = new Dictionary<KeyValuePair<Buttons, ButtonGroup>, Action>();
public InstrumentPad(Game game, PlayerIndex playerIndex) : base(game)
{
this.playerIndex = playerIndex;
}
public void ClearActions()
{
actionMap.Clear();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
updateButtons();
foreach (KeyValuePair<Buttons, ButtonGroup> pair in actionMap.Keys)
{
switch (pair.Value)
{
case ButtonGroup.DirectionalPad:
if (DirectionalButtons == pair.Key)
actionMap[pair].Invoke();
break;
case ButtonGroup.FaceButtons:
if (FaceButtons == pair.Key)
actionMap[pair].Invoke();
break;
case ButtonGroup.ShoulderButtons:
if (ShoulderButtons == pair.Key)
actionMap[pair].Invoke();
break;
case ButtonGroup.LeftSide:
if (LeftSideButtons == pair.Key)
actionMap[pair].Invoke();
break;
case ButtonGroup.RightSide:
if (RightSideButtons == pair.Key)
actionMap[pair].Invoke();
break;
case ButtonGroup.FullyExclusive:
if (AllButtons == pair.Key)
actionMap[pair].Invoke();
break;
case ButtonGroup.NonExclusive:
if ((AllButtons & pair.Key) == pair.Key)
actionMap[pair].Invoke();
break;
}
}
}
public void BindAction(Buttons buttonCombo, ButtonGroup buttonGrouping, Action action)
{
KeyValuePair<Buttons, ButtonGroup> pair = new KeyValuePair<Buttons, ButtonGroup>(buttonCombo, buttonGrouping);
if (!actionMap.ContainsKey(pair))
actionMap.Add(pair, action);
else
actionMap[pair] = action;
}
private void updateButtons()
{
GamePadState padState = GamePad.GetState(playerIndex);
LeftSideButtons = 0x0;
RightSideButtons = 0x0;
FaceButtons = 0x0;
if (padState.Buttons.A == ButtonState.Pressed)
FaceButtons |= Buttons.A;
if (padState.Buttons.B == ButtonState.Pressed)
FaceButtons |= Buttons.B;
if (padState.Buttons.X == ButtonState.Pressed)
FaceButtons |= Buttons.X;
if (padState.Buttons.Y == ButtonState.Pressed)
FaceButtons |= Buttons.Y;
RightSideButtons |= FaceButtons;
DirectionalButtons = 0x0;
if (padState.DPad.Up == ButtonState.Pressed)
DirectionalButtons |= Buttons.DPadUp;
if (padState.DPad.Down == ButtonState.Pressed)
DirectionalButtons |= Buttons.DPadDown;
if (padState.DPad.Left == ButtonState.Pressed)
DirectionalButtons |= Buttons.DPadLeft;
if (padState.DPad.Right == ButtonState.Pressed)
DirectionalButtons |= Buttons.DPadRight;
LeftSideButtons |= DirectionalButtons;
ShoulderButtons = 0x0;
if (padState.Buttons.LeftShoulder == ButtonState.Pressed)
{
ShoulderButtons |= Buttons.LeftShoulder;
LeftSideButtons |= Buttons.LeftShoulder;
}
if (padState.Buttons.RightShoulder == ButtonState.Pressed)
{
ShoulderButtons |= Buttons.RightShoulder;
RightSideButtons |= Buttons.RightShoulder;
}
AllButtons = LeftSideButtons | RightSideButtons;
if (padState.Buttons.Start == ButtonState.Pressed)
AllButtons |= Buttons.Start;
if (padState.Buttons.Back == ButtonState.Pressed)
AllButtons |= Buttons.Back;
}
}
}