将输入传递给状态机 (c#)

发布于 2024-07-07 15:34:40 字数 427 浏览 5 评论 0原文

我会尽力解释我的情况;

在每个应用程序中,我都会查询键盘和鼠标的当前状态,并将它们包装在单独的类和数据结构中。 对于键盘,它是我的 Keys 枚举数组(当前按下的每个键对应一个项目),对于鼠标,它是一个类,其中包含按下的每个按钮的坐标增量和布尔值。

我还有一个通过状态管理器类管理的基本状态机,它维护堆栈并编组状态。

我想知道的是,如何最好地将输入(快照)传递到我的应用程序在任何时候可能处于的各个状态?

我想处理尽可能多的远离各个状态的输入,以减少状态内的重复逻辑。

或者,最好保持输入快照尽可能纯净并将它们传递给状态,以便它们可以隐藏特定于输入的逻辑?

注意
这种结构类似于我想象的游戏的工作方式,尽管这个应用程序不是游戏,但它确实需要尽快处理。

I'll try to explain my scenario as best i can;

At each application tick I query the current state of the keyboard and mouse and wrap them in individual classes and data structures. For the keyboard it's an array of my Keys enum (one item for each of the keys that are currently pressed) and for the mouse it's a class containing coordinate delta's and bools for each buttons pressed.

I also have a rudimentary state machine managed via a state manager class which maintains the stack and marshalls the states.

All I want to know is, how best to pass the input (snapshots) to the individual states my app can be in at any one time?

I would like to process as much of the input as possible away from the individual states, so as to reduce repeating logic within the states.

Or would it be better to keep the input snapshots as pure as possible and pass them to the states so they can keep they're input-specific-logic hidden?

Note
This structure is similiar to how I imagine a game would work, and although this application is not a game it does need to be processed as quickly as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜深人未静 2024-07-14 15:34:40

为什么每次勾选都要查询键盘和鼠标的状态? 更好的传统解决方案是捕获从键盘和鼠标触发的事件。 这样你只需要在必要时更新状态。

Why are you querying the state of the keyboard and mouse with each tick? A much better and traditional solution would be to capture events fired from the keyboard and mouse. That way you only need to update the state when you HAVE to.

ぃ双果 2024-07-14 15:34:40

如果您只是在每次点击时查询键盘和鼠标,我可以保证您会遇到问题。 当您查询每个刻度时,您会发现错过了快速发生的输入(在单个刻度的时域内)。 想象一下用户在两次滴答之间按下并释放按键/按钮的情况(这种情况发生的次数比您想象的要多) - 您将完全错过输入。

特别是在 C# 中,它完全适合这样的输入事件,您应该使用事件。

If you simply query your keyboard and mouse every tick, I can guarantee you'll run into problems. When you query every tick, you'll find that you miss inputs that occur quickly (within the time domain of a single tick). Imagine a situation where the user presses and releases a key/button between two ticks (it will happen more than you think) - you'll completely miss the input.

Particularly in C#, which is completely geared for input events like this, you should be using events.

半步萧音过轻尘 2024-07-14 15:34:40

在 XNA 中,他们通过将键盘类设为静态来实现这一点。

KeyboardState state = Keyboard.GetState();

然后您可以使用上面的代码行访问当前的按键状态。

In XNA, they do this by making the keyboard class static.

KeyboardState state = Keyboard.GetState();

you can then access the current key state with the above line of code.

廻憶裏菂餘溫 2024-07-14 15:34:40

您应该触发事件并捕获参数。 这将是处理这个问题最简单、最有效的方法。

class YourClass
{    
    //data members ...

    public void OnKeyboardPress(object sender, EventArgs args)
    {
        //handle your logic capturing the state here
    }
}

//elsewhere
someControl.KeyPress += new KeyPressDelegate(yourClassInstance.OnKeyboardPress);

You should fire events and capture the arguments. That would be the easiest, and most efficient way to handle this.

class YourClass
{    
    //data members ...

    public void OnKeyboardPress(object sender, EventArgs args)
    {
        //handle your logic capturing the state here
    }
}

//elsewhere
someControl.KeyPress += new KeyPressDelegate(yourClassInstance.OnKeyboardPress);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文