1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use super::utils::*;
use crate::{
    combat::AttackSource,
    comp::{
        character_state::{AttackFilters, OutputEvents},
        CharacterState, StateUpdate,
    },
    states::behavior::{CharacterBehavior, JoinData},
};
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParryWindow {
    pub buildup: bool,
    pub recover: bool,
}

/// Separated out to condense update portions of character state
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct StaticData {
    /// How long until state should deal damage
    pub buildup_duration: Duration,
    /// How long the state has until exiting
    pub recover_duration: Duration,
    /// Max angle (45.0 will give you a 90.0 angle window)
    pub max_angle: f32,
    /// Base value that incoming damage is reduced by and converted to poise
    /// damage
    pub block_strength: f32,
    /// What durations are considered a parry
    pub parry_window: ParryWindow,
    /// What key is used to press ability
    pub ability_info: AbilityInfo,
    /// Energy consumed to initiate the block
    pub energy_cost: f32,
    /// Energy recovered upon successful parry
    pub energy_regen: f32,
    /// Whether block can be held
    pub can_hold: bool,
    /// What kinds of attacks the block applies to
    pub blocked_attacks: AttackFilters,
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Data {
    /// Struct containing data that does not change over the course of the
    /// character state
    pub static_data: StaticData,
    /// Timer for each stage
    pub timer: Duration,
    /// What section the character stage is in
    pub stage_section: StageSection,
    // Whether there was parry
    pub is_parry: bool,
}

impl CharacterBehavior for Data {
    fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
        let mut update = StateUpdate::from(data);

        handle_orientation(data, &mut update, 1.0, None);
        handle_move(data, &mut update, 0.4);

        match self.stage_section {
            StageSection::Buildup => {
                if self.timer < self.static_data.buildup_duration {
                    // Build up
                    update.character = CharacterState::BasicBlock(Data {
                        timer: tick_attack_or_default(data, self.timer, None),
                        ..*self
                    });
                } else {
                    // Transitions to swing section of stage
                    update.character = CharacterState::BasicBlock(Data {
                        timer: Duration::default(),
                        stage_section: if self.static_data.can_hold {
                            StageSection::Action
                        } else {
                            StageSection::Recover
                        },
                        ..*self
                    });
                }
            },
            StageSection::Action => {
                if self.static_data.can_hold
                    && input_is_pressed(data, self.static_data.ability_info.input)
                {
                    // Block
                    update.character = CharacterState::BasicBlock(Data {
                        timer: tick_attack_or_default(data, self.timer, None),
                        ..*self
                    });
                } else {
                    // Transitions to recover section of stage
                    update.character = CharacterState::BasicBlock(Data {
                        timer: Duration::default(),
                        stage_section: StageSection::Recover,
                        ..*self
                    });
                }
            },
            StageSection::Recover => {
                if (self.static_data.parry_window.recover || !self.is_parry)
                    && self.timer < self.static_data.recover_duration
                {
                    // Recovery
                    update.character = CharacterState::BasicBlock(Data {
                        timer: tick_attack_or_default(
                            data,
                            self.timer,
                            Some(data.stats.recovery_speed_modifier),
                        ),
                        ..*self
                    });
                } else {
                    // Done
                    end_ability(data, &mut update);
                }
            },
            _ => {
                // If it somehow ends up in an incorrect stage section
                end_ability(data, &mut update);
            },
        }

        // At end of state logic so an interrupt isn't overwritten
        handle_interrupts(data, &mut update, output_events);

        update
    }
}

impl Data {
    pub fn is_parry(&self, attack: AttackSource) -> bool {
        let could_block = self.static_data.blocked_attacks.applies(attack);
        let timed = match self.stage_section {
            StageSection::Buildup => self.static_data.parry_window.buildup,
            StageSection::Recover => self.static_data.parry_window.recover,
            _ => false,
        };
        could_block && timed
    }
}