Designing a turn-based combat controller

The following tutorial explains how to design a turn-based combat system for a dungeon crawler game using a finite state machine. This forms part of a bigger project aimed at developing a 2D top-down dungeon crawler using pygame. The finite state machine shows the different states that are possible during combat and the transitions that can be made between these states.

For this example we limit the design to a single player character completing a single turn of combat. The character can move and make melee or ranged attack actions.

Finite state machine

During each turn of combat player character can move and perform an action. Movement can occur before and/or after the action, provided the total movement does not exceed the character’s move distance. Because the screen needs to refresh each frame we need to also keep track other other states between movement and action. For example, when the player character elects to move, the combat controller needs to transition through several states to complete the move. This includes states for:

  • Initiating movement
  • Selecting the target location
  • Moving the character to the target location

These states are shown at the top of the following finite state machine diagram. States are shown as circles, with a title/description for each state. Transitions between the states
are shown as arcs (directed lines) that connect the states. These arcs are labelled with conditions that must be met for the transition to occur. For example, the combat controller will transition from the “pre move location” state to the “pre move” state when a valid target location is selected. If no selection is made, or the selection is invalid (e.g. too far away) then the combat controller remains in the same state.

To explain this further, we look demonstrate two main parts of the combat controller (movement and ranged attacks) in more detail using examples from the final game.

Pre action movement

In the video below, the selected player character begins in the “move or action state”. The player selects the move options (key “m”) at which point the character transitions into the “pre move location” state. In this state the player is able to select the target location that the character moves to. This is done by moving the crosshairs using mouse controls. The player then clicks on the target location – if this is a valid location then the character transitions to the “pre move” state. In this state the movement of the character occurs. When the target location is reached, the character transitions into either the “melee attack” or “ranged target” states.

Ranged attacks

A ranged attack is initiated when the player enters “r” either at the beginning of their turn, or after they have moved, at this point the character transitions to the “ranged target” state.
In this state the player moves the crosshairs to the target of their ranged attack. When they have selected a valid target and pressed the mouse button, the character transitions to the “ranged shoot” state. In this state the arrow travels towards the target enemy, moving a small distance each frame. When the arrow reaches the enemy, the character transitions to the “ranged attack” state. At this point it is determined whether the arrow hit the enemy, and if so how much damage is done. The combat controller moves the “show damage” state, either indicating a miss, or a hit with the amount of damage done.

print

Leave a Reply

Your email address will not be published. Required fields are marked *