Turn-Based Combat

Currently, the combat in *Castle Storm* is turn-based. Each turn, the player picks an action for each of their party members to take. After they've chosen their actions, the player executes their turn, which will display all the animations and calculate the effect each action has on its target.

## Relevant Source Code

For the source code used to implement turn-based combat, see:

* Turn Based Fight Manager (Monobehavior)
* Turn Based Fight UI (Monobehavior)
* Arena (Class)

## Initializing the Fight

The Turn-Based-Fight Manager (TBFM) only needs to be passed 3D coordinates to be initialized. The TBFM will try to find a suitable spot (large enough and flat enough) close to those coordinates to create the arena. If it can't find a suitable spot, it will simply create a new terrain based on the biome it's in and choose a spot there. For example, if the fight is in a desert town, and there's no suitable spot to be found, then the TBFM will load a default Desert Area that is guaranteed to be flat.

Either way, the TBFM will fade the player's view to black, choose a spot to set up, and create the arena.

Once the TBFM has chosen a spot, it generates a set of Arena Positions in 3D space based off of how many party members each team has. There will always be two rows of positions for each team, with one spot for each member in the front row, and two spots less in the back row.

Once the arena positions are created, the party members of each team are teleported into the front rows. Then, the TBFM fades back in, brings up the Action Choosing UI, and begins the fight.

## Mechanics of Arena Positions

Party members in the back row cannot be attacked. However, party members without a neighbor are vulnerable to a flanking penalty if the enemy team has more members in the front row.

Parties with only two members will still have one position in the back row. A party with only one member will have no back row positions.

If all the party members in the front row are taken out, then (1) the members in the back row will receive a Retreat Penalty on the next turn, (2) the back row will become the front row, and (3) a new back row will be generated.

Party members in the back row can interact with members in the front row (for example, casting a healing spell on the party member in front of them).

## Phase 1: Choosing Actions

### User Interface

To select a party member, the player can simply click on them in 3D space. When a party member is selected, the UI will display these things:

1. The member's available actions and items (bottom left - Action Panel).
2. The member's Character Information (bottom center - Info Panel).
3. The member's Profile Information (bottom right - Profile Panel).

Once a party member is selected, the player can choose an action for them. Players can choose actions by navigating the Action Panel. When a player selects a Skill or Item, the Info Panel will display that Skill or Item's Description. (To return to the Character Information, the player can click on the Profile Panel).

After a party member has chosen an action, they will be highlighted in 3D space so the player can see which party members are good to go and which aren't. After each party member has chosen a move, the Execute Turn button will be enabled. The player can continue to choose actions, or execute their turn.

### Actions

Each party member must have an action chosen for them before the player can execute the turn. There are three different categories of actions.

#### (1) Default Actions

These actions are available to every party member (notable actions: Wait, Change Position).

#### (2) Items

Items can be used by any party member. However, some items might be restricted to certain skillsets. For example, while the item "Health Potion" could probably be used by any character, the item "Magic Scroll" could only be used by party members with a high Magic skill.

#### (3) Skills and Character-Specific Actions

Skill actions can be unlocked by levelling a certain skill for a character. Character-specific actions are available only to certain characters. Character-specific actions can also be tied to skill levels - for example, you could specify that Character X gained Ability Y after levelling up a skill enough, even though other characters won't get that ability.

### Disabling Actions

When it suits the story, any action might be disabled completely.

### Displaying Actions in Menu

The action menu (seen in the bottom left of the UI screenshot) displays every action a player can take. This menu is just a list of buttons. Buttons correspond to either an action or a submenu. If the player clicks on an action, the action is chosen for the currently selected party member, and its Description is displayed in the Info Panel. If they click on a submenu, the current list of buttons is erased and a new list is generated for the submenu (without changing the Info Panel).

* Skills
	+ Go Back
	+ [Skills and Character-Specific Actions]
* Items
	+ Go Back
	+ [Items]
* [Default Actions]

As you might guess, clicking the "Go Back" button will bring you back to the parent menu.

Whenever an action or item is selected, it's title and description show up in the Info Panel in the center of the screen (as seen in the screenshot in this section).

## Phase 2: Executing Friendly Actions

When the player chooses to execute their turn, the camera will focus on the center of the arena, and the action choosing UI will drop away. For every action that has an animation (basically every action besides "Wait"), the relevant party members will be moved to the center of the arena, and the animation will play. Let's look at an example round:

### Choosing Actions

* Party Member 1: Make action [Healing Spell] on [Party Member 2] (friendly)
* Party Member 2: Make action [Wait] on [Self] (friendly)
* Party Member 3: Make action [Wait] on [Self] (friendly)

### Executing Moves

* Hide Action Choosing UI
* Focus camera on arena center
* Bring [Party Member 1], [Party Member 2] to center
	+ Play [Healing Spell] [Casting] animation on [Party Member 1]
	+ Play [Healing Spell] [Receiving] animation on [Party Member 2]
* Return [Party Member 1], [Party Member 2] to their places
* (do nothing for Party Members 2 or 3)

## Phase 3: Calculating and Executing Enemy Actions

After the player's team has executed their moves, the game calculates what the enemy team should do next. (This can probably done mathematically - choosing which actions have the highest probability of creating the highest difference between enemy health and player health) Once the enemy actions are calculated, they are executed in the same way that the player's actions were executed.

Then, the game returns to Phase 1 and continues until one of the teams wins.

## Ending the Fight

After one of the teams wins, the Turn-Based-Fight Manager saves the winners' stats (current health, etc), and passes those and the result of the round (did friendly team win? yes/no) to the Game Manager. The Game Manager will use this to determine which mode to go into.

For example, let's say the Game Manager was in Travel Mode before the fight was initialized. If the player wins, it knows to restart Travel Mode at the current position (# days travelled). If not, it will go to the Game Over screen, and let the player continue from the last checkpoint or quit the game.