Skip to main content

veloren_common/states/
dash_melee.rs

1use crate::{
2    combat,
3    comp::{CharacterState, MeleeConstructor, StateUpdate, character_state::OutputEvents},
4    states::{
5        behavior::{CharacterBehavior, JoinData},
6        utils::*,
7    },
8};
9use serde::{Deserialize, Serialize};
10use std::time::Duration;
11
12/// Separated out to condense update portions of character state
13#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
14pub struct StaticData {
15    /// Rate of energy drain
16    pub energy_drain: f32,
17    /// How quickly dasher moves forward
18    pub forward_speed: f32,
19    /// How long until state should deal damage
20    pub buildup_duration: Duration,
21    /// How long the state charges for until it reaches max damage
22    pub charge_duration: Duration,
23    /// Duration of state spent in swing
24    pub swing_duration: Duration,
25    /// How long the state has until exiting
26    pub recover_duration: Duration,
27    /// Used to construct the Melee attack
28    pub melee_constructor: MeleeConstructor,
29    /// How fast can you turn during charge
30    pub ori_modifier: f32,
31    /// Controls whether charge should always go until end or enemy hit
32    pub auto_charge: bool,
33    /// If true, hitting an enemy does not stop the charge
34    pub charge_through: bool,
35    /// What key is used to press ability
36    pub ability_info: AbilityInfo,
37    //For particle effects
38    pub frontend_specifier: Option<FrontendSpecifier>,
39}
40
41#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
42pub struct Data {
43    /// Struct containing data that does not change over the course of the
44    /// character state
45    pub static_data: StaticData,
46    /// Whether the charge should last a default amount of time or until the
47    /// mouse is released
48    pub auto_charge: bool,
49    /// Timer for each stage
50    pub timer: Duration,
51    /// What section the character stage is in
52    pub stage_section: StageSection,
53}
54
55impl CharacterBehavior for Data {
56    fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
57        let mut update = StateUpdate::from(data);
58
59        handle_move(data, &mut update, 0.1);
60
61        let create_melee = |charge_frac: f32| {
62            let precision_mult = combat::compute_precision_mult(data.inventory, data.msm);
63            let mut melee = self
64                .static_data
65                .melee_constructor
66                .clone()
67                .handle_scaling(charge_frac)
68                .create_melee(precision_mult, self.static_data.ability_info);
69            if self.static_data.charge_through {
70                melee.sustained = true;
71            }
72            melee
73        };
74
75        match self.stage_section {
76            StageSection::Buildup => {
77                if self.timer < self.static_data.buildup_duration {
78                    handle_orientation(data, &mut update, 1.0, None);
79                    // Build up
80                    if let CharacterState::DashMelee(c) = &mut update.character {
81                        c.timer = tick_attack_or_default(data, self.timer, None);
82                    }
83                } else {
84                    // Transitions to charge section of stage
85                    if let CharacterState::DashMelee(c) = &mut update.character {
86                        c.auto_charge =
87                            !input_is_pressed(data, self.static_data.ability_info.input)
88                                || self.static_data.auto_charge;
89                        c.timer = Duration::default();
90                        c.stage_section = StageSection::Charge;
91                    }
92                }
93            },
94            StageSection::Charge => {
95                if self.timer < self.static_data.charge_duration
96                    && (input_is_pressed(data, self.static_data.ability_info.input)
97                        || self.auto_charge)
98                    && update.energy.current() >= 0.0
99                {
100                    // Forward movement
101                    let charge_frac = (self.timer.as_secs_f32()
102                        / self.static_data.charge_duration.as_secs_f32())
103                    .min(1.0);
104
105                    handle_orientation(data, &mut update, self.static_data.ori_modifier, None);
106                    handle_forced_movement(
107                        data,
108                        &mut update,
109                        ForcedMovement::Forward(
110                            self.static_data.forward_speed * charge_frac.sqrt(),
111                        ),
112                    );
113
114                    // Determines if charge ends by continually refreshing melee component until it
115                    // detects a hit, at which point the charge ends
116                    if let Some(melee) = data.melee_attack {
117                        if melee.sustained || !melee.applied {
118                            // If melee attack has not applied, or is sustained, just tick duration
119                            if let CharacterState::DashMelee(c) = &mut update.character {
120                                c.timer = tick_attack_or_default(data, self.timer, None);
121                            }
122                        } else if melee.hit_entities.is_empty() {
123                            // If melee attack has applied, but not hit anything, reset melee attack
124                            data.updater.insert(data.entity, create_melee(charge_frac));
125                            if let CharacterState::DashMelee(c) = &mut update.character {
126                                c.timer = tick_attack_or_default(data, self.timer, None);
127                            }
128                        } else {
129                            // Stop charging now and go to swing stage section; unless sustained
130                            if let CharacterState::DashMelee(c) = &mut update.character {
131                                c.timer = Duration::default();
132                                c.stage_section = StageSection::Action;
133                            }
134                        }
135                    } else {
136                        // If no melee attack, add it and tick duration
137                        data.updater.insert(data.entity, create_melee(charge_frac));
138
139                        if let CharacterState::DashMelee(c) = &mut update.character {
140                            c.timer = tick_attack_or_default(data, self.timer, None);
141                        }
142                    }
143
144                    // Consumes energy if there's enough left and charge has not stopped
145                    update
146                        .energy
147                        .change_by(-self.static_data.energy_drain * data.dt.0);
148                } else {
149                    // Transitions to swing section of stage
150                    if let CharacterState::DashMelee(c) = &mut update.character {
151                        c.timer = Duration::default();
152                        c.stage_section = StageSection::Action;
153                    }
154                }
155            },
156            StageSection::Action => {
157                if self.timer < self.static_data.swing_duration {
158                    // Swings
159                    if let CharacterState::DashMelee(c) = &mut update.character {
160                        c.timer = tick_attack_or_default(data, self.timer, None);
161                    }
162                } else {
163                    // Transitions to recover section of stage
164                    if let CharacterState::DashMelee(c) = &mut update.character {
165                        c.timer = Duration::default();
166                        c.stage_section = StageSection::Recover;
167                    }
168                }
169            },
170            StageSection::Recover => {
171                if self.timer < self.static_data.recover_duration {
172                    // Recover
173                    if let CharacterState::DashMelee(c) = &mut update.character {
174                        c.timer = tick_attack_or_default(data, self.timer, None);
175                    }
176                } else {
177                    // Done
178                    end_melee_ability(data, &mut update);
179                }
180            },
181            _ => {
182                // If it somehow ends up in an incorrect stage section
183                end_melee_ability(data, &mut update);
184            },
185        }
186
187        // At end of state logic so an interrupt isn't overwritten
188        handle_interrupts(data, &mut update, output_events);
189
190        update
191    }
192}
193
194#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
195pub enum FrontendSpecifier {
196    FireDash,
197}