veloren_common/states/
shockwave.rs

1use crate::{
2    combat::{
3        self, Attack, AttackDamage, AttackEffect, CombatEffect, CombatRequirement, Damage,
4        DamageKind, DamageSource, GroupTarget, Knockback,
5    },
6    comp::{
7        CharacterState, StateUpdate, ability::Dodgeable, character_state::OutputEvents, shockwave,
8    },
9    event::{LocalEvent, ShockwaveEvent},
10    outcome::Outcome,
11    states::{
12        behavior::{CharacterBehavior, JoinData},
13        utils::*,
14    },
15};
16use serde::{Deserialize, Serialize};
17use std::time::Duration;
18
19/// Separated out to condense update portions of character state
20#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
21pub struct StaticData {
22    /// How long until state should deal damage
23    pub buildup_duration: Duration,
24    /// How long the state is swinging for
25    pub swing_duration: Duration,
26    /// How long the state has until exiting
27    pub recover_duration: Duration,
28    /// Base damage
29    pub damage: f32,
30    /// Base poise damage
31    pub poise_damage: f32,
32    /// Knockback
33    pub knockback: Knockback,
34    /// Angle of the shockwave
35    pub shockwave_angle: f32,
36    /// Vertical angle of the shockwave
37    pub shockwave_vertical_angle: f32,
38    /// Speed of the shockwave
39    pub shockwave_speed: f32,
40    /// How long the shockwave travels for
41    pub shockwave_duration: Duration,
42    /// If the shockwave can be dodged, and in what way
43    pub dodgeable: Dodgeable,
44    /// Movement speed efficiency
45    pub move_efficiency: f32,
46    /// What key is used to press ability
47    pub ability_info: AbilityInfo,
48    /// Adds an effect onto the main damage of the attack
49    pub damage_effect: Option<CombatEffect>,
50    /// What kind of damage the attack does
51    pub damage_kind: DamageKind,
52    /// Used to specify the shockwave to the frontend
53    pub specifier: shockwave::FrontendSpecifier,
54    /// Controls outcome emission
55    pub emit_outcome: bool,
56    /// How fast enemy can rotate
57    pub ori_rate: f32,
58    /// Timing of shockwave
59    pub timing: Timing,
60    pub minimum_combo: Option<u32>,
61    pub combo_on_use: u32,
62    pub combo_consumption: ComboConsumption,
63}
64
65#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
66pub struct Data {
67    /// Struct containing data that does not change over the course of the
68    /// character state
69    pub static_data: StaticData,
70    /// Timer for each stage
71    pub timer: Duration,
72    /// What section the character stage is in
73    pub stage_section: StageSection,
74}
75
76impl CharacterBehavior for Data {
77    fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
78        let mut update = StateUpdate::from(data);
79
80        handle_orientation(data, &mut update, self.static_data.ori_rate, None);
81        handle_move(data, &mut update, self.static_data.move_efficiency);
82
83        match self.stage_section {
84            StageSection::Buildup => {
85                if self.timer < self.static_data.buildup_duration {
86                    // Build up
87                    update.character = CharacterState::Shockwave(Data {
88                        timer: tick_attack_or_default(data, self.timer, None),
89                        ..*self
90                    });
91                } else {
92                    // Attack
93                    if matches!(self.static_data.timing, Timing::PostBuildup) {
94                        self.attack(data, output_events);
95                    }
96
97                    // Transitions to swing
98                    update.character = CharacterState::Shockwave(Data {
99                        timer: Duration::default(),
100                        stage_section: StageSection::Action,
101                        ..*self
102                    });
103                }
104            },
105            StageSection::Action => {
106                if self.timer < self.static_data.swing_duration {
107                    // Swings
108                    update.character = CharacterState::Shockwave(Data {
109                        timer: tick_attack_or_default(data, self.timer, None),
110                        ..*self
111                    });
112                    // Send local event used for frontend shenanigans
113                    if self.static_data.emit_outcome {
114                        match self.static_data.specifier {
115                            shockwave::FrontendSpecifier::IceSpikes => {
116                                output_events.emit_local(LocalEvent::CreateOutcome(
117                                    Outcome::FlashFreeze {
118                                        pos: data.pos.0
119                                            + *data.ori.look_dir() * (data.body.max_radius()),
120                                    },
121                                ));
122                            },
123                            shockwave::FrontendSpecifier::Ground => {
124                                output_events.emit_local(LocalEvent::CreateOutcome(
125                                    Outcome::GroundSlam {
126                                        pos: data.pos.0
127                                            + *data.ori.look_dir() * (data.body.max_radius()),
128                                    },
129                                ));
130                            },
131                            shockwave::FrontendSpecifier::Steam => {
132                                output_events.emit_local(LocalEvent::CreateOutcome(
133                                    Outcome::Steam {
134                                        pos: data.pos.0
135                                            + *data.ori.look_dir() * (data.body.max_radius()),
136                                    },
137                                ));
138                            },
139                            shockwave::FrontendSpecifier::Fire => {
140                                output_events.emit_local(LocalEvent::CreateOutcome(
141                                    Outcome::FireShockwave {
142                                        pos: data.pos.0
143                                            + *data.ori.look_dir() * (data.body.max_radius()),
144                                    },
145                                ));
146                            },
147                            _ => {
148                                output_events.emit_local(LocalEvent::CreateOutcome(
149                                    Outcome::Swoosh {
150                                        pos: data.pos.0
151                                            + *data.ori.look_dir() * (data.body.max_radius()),
152                                    },
153                                ));
154                            },
155                        }
156                    }
157                } else {
158                    // Attack
159                    if matches!(self.static_data.timing, Timing::PostAction) {
160                        self.attack(data, output_events);
161                    }
162
163                    // Transitions to recover
164                    update.character = CharacterState::Shockwave(Data {
165                        timer: Duration::default(),
166                        stage_section: StageSection::Recover,
167                        ..*self
168                    });
169                }
170            },
171            StageSection::Recover => {
172                if self.timer < self.static_data.recover_duration {
173                    // Recovers
174                    update.character = CharacterState::Shockwave(Data {
175                        timer: tick_attack_or_default(
176                            data,
177                            self.timer,
178                            Some(data.stats.recovery_speed_modifier),
179                        ),
180                        ..*self
181                    });
182                } else {
183                    // Done
184                    end_ability(data, &mut update);
185                }
186            },
187            _ => {
188                // If it somehow ends up in an incorrect stage section
189                end_ability(data, &mut update);
190            },
191        }
192
193        // At end of state logic so an interrupt isn't overwritten
194        handle_interrupts(data, &mut update, output_events);
195
196        update
197    }
198}
199
200impl Data {
201    fn attack(&self, data: &JoinData, output_events: &mut OutputEvents) {
202        if let Some(min_combo) = self.static_data.minimum_combo {
203            self.static_data
204                .combo_consumption
205                .consume(data, output_events, min_combo);
206        }
207
208        let poise = AttackEffect::new(
209            Some(GroupTarget::OutOfGroup),
210            CombatEffect::Poise(self.static_data.poise_damage),
211        )
212        .with_requirement(CombatRequirement::AnyDamage);
213        let knockback = AttackEffect::new(
214            Some(GroupTarget::OutOfGroup),
215            CombatEffect::Knockback(self.static_data.knockback),
216        )
217        .with_requirement(CombatRequirement::AnyDamage);
218        let mut damage = AttackDamage::new(
219            Damage {
220                source: DamageSource::Shockwave,
221                kind: self.static_data.damage_kind,
222                value: self.static_data.damage,
223            },
224            Some(GroupTarget::OutOfGroup),
225            rand::random(),
226        );
227        if let Some(effect) = self.static_data.damage_effect {
228            damage = damage.with_effect(effect);
229        }
230        let precision_mult = combat::compute_precision_mult(data.inventory, data.msm);
231        let attack = Attack::default()
232            .with_damage(damage)
233            .with_precision(precision_mult)
234            .with_effect(poise)
235            .with_effect(knockback)
236            .with_combo_increment();
237        let properties = shockwave::Properties {
238            angle: self.static_data.shockwave_angle,
239            vertical_angle: self.static_data.shockwave_vertical_angle,
240            speed: self.static_data.shockwave_speed,
241            duration: self.static_data.shockwave_duration,
242            attack,
243            dodgeable: self.static_data.dodgeable,
244            owner: Some(*data.uid),
245            specifier: self.static_data.specifier,
246        };
247        output_events.emit_server(ShockwaveEvent {
248            properties,
249            pos: *data.pos,
250            ori: *data.ori,
251        });
252    }
253}
254
255#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
256pub enum Timing {
257    PostBuildup,
258    PostAction,
259}