将拇指输入添加到 XNA 中的队列

Adding Thumbstick Input to a Queue in XNA

我正在尝试使用 XNA 的 GamePadStates 将用户最近的控制器输入存储在队列中。我尝试这样做的方法是在当前帧中获取 GamePadState,将其与前一帧中的状态进行比较,将所有按钮按下和摇杆输入转换为字符,并将其全部添加到队列中。稍后,我检查队列中最新的输入是什么,并相应地更新游戏状态。当队列太满时,最早的输入将被删除。

但是,我 运行 遇到了摇杆输入的问题。我正在尝试以与按下按钮类似的方式处理每个定向输入,方法是将其转换为字符,并将其添加到队列中。这方面的一个例子是

if(newGamePadState.ThumbSticks.Left.X > thumbStickDeadZone && 
oldGamePadState.ThumbSticks.Left.X < thumbStickDeadZone &&
newGamePadState.ThumbSticks.Left.Y > -1.0f * thumbStickDeadZone &&
newGamePadState.ThumbSticks.Left.Y < thumbStickDeadZone)
{
     inputQueue.Add('9'); // 9 in this case is the character representation of "Right"
}

如您所见,这种逻辑非常混乱,而且很快就会失控,尤其是在引入序号方向检查时。

有没有更好的方法来正确编写此类内容,而不会在按住拇指杆上的方向时导致输入重复?我最接近的解决方案是检查添加到队列中的最后一组输入是否有任何重复项并将其删除,如果用户按拇指杆上的相同方向两次作为两个单独的输入,这会导致问题。

*注: 作为程序的要求,最近的输入 要存储,因为程序最终将能够检查是否输入了某些命令序列。

我假设您的摇杆可以有两种状态 "neutral" 和 "direction"。

因此,您的逻辑应如下所示(警告,大量伪代码):

bool thumbstickActive = false;

void CheckInput()
{
    //Pseudo-code, fix to match how you are getting the position
    Vector2 currentPostiion = thumbstick1.Position;

    if (ThumbstickInDeadZone(currentPosition))
    {
        thumbStickActive = false;
    }
    else if (!thumbStickActive)
    {
        ActionQueue.Enqueue(GetDirection(currentPosition));
        thumbStickActive = true;
    }
}

基本上,当摇杆返回 "neutral" 时设置一个标志,并且仅当我们从 中立时才向队列中添加一个动作。如果只想说 "wasn't the last direction",请将其更改为:

Direction lastDirection = Direction.None; //Pseudo-enum

void CheckInput()
{
    //Pseudo-code, fix to match how you are getting the position
    Vector2 currentPostiion = thumbstick1.Position;
    Direction currentDirection = GetDirection(currentPosition);

    //If GetDirection returns Direction.None for neutral,
    //The old check doesn't matter.
    if (currentDirection != lastDirection)
    {
        ActionQueue.Enqueue(currentDirection);
        lastDirection = currentDirection;
    }
}

还是一样的想法,设置存储"last"状态的变量,并检查该状态是否已经改变到你想要排队的动作。

它有两个您应该已经拥有的辅助函数。第一个 (ThumbstickInDeadZone) returns true 如果操纵杆处于中间位置,否则 false。第二个 (GetDirection) returns 由摇杆的当前位置表示的方向。