Java – the best way to implement a large state machine?

Basically, I have a state machine that controls the attack of game characters, and its time is based on the length of animation

So for example:

I start with the default state. If the player encounters the attack button, he will launch an attack, switch the state and set the timer according to the attack length However, when I consider charging attacks that can be cancelled, attacks that can move to different states according to what they hit, and each state has a unique way to deal with the attacked role, the state machine becomes more complex At present, I have a big switch statement I thought of polymorphism, but for each state with many, a new class is needed (separate states are required for start attack, attack and sorting attack)

The switch statement works, but it's quite large and not as easy to modify as an inheritance based system

Any suggestions on the realization of good faith?

Edit: This is using Java

Solution

With a larger state machine, you must pay attention to the "transition explosion" phenomenon It turns out that the traditional finite state machine does not provide a mechanism to reuse the common transformations in many states, so you end up repeating too much, and your state machine "explodes" (which also violates the do not repeat yourself principle)

Therefore, I recommend using hierarchical state machines and implementation techniques to allow such state machines to be easily mapped to code For more information about hierarchical state machines, see Wikipedia articles http://en.wikipedia.org/wiki/UML_state_machine.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>