veloren_common/states/
simple.rs

1use crate::{
2    comp::{CharacterState, StateUpdate, character_state::OutputEvents},
3    event::ComboChangeEvent,
4    states::{
5        behavior::{CharacterBehavior, JoinData},
6        utils::*,
7    },
8};
9use serde::{Deserialize, Serialize};
10use std::time::Duration;
11
12#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
13pub struct StaticData {
14    pub buildup_duration: Duration,
15    pub ability_info: AbilityInfo,
16}
17
18#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
19pub struct Data {
20    pub static_data: StaticData,
21    pub timer: Duration,
22    pub stage_section: StageSection,
23}
24
25impl CharacterBehavior for Data {
26    fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
27        let mut update = StateUpdate::from(data);
28
29        handle_move(data, &mut update, 1.0);
30        handle_jump(data, output_events, &mut update, 1.0);
31
32        match self.stage_section {
33            StageSection::Buildup => {
34                if self.timer < self.static_data.buildup_duration {
35                    if let Some(combo) = data.combo
36                        && combo.counter() > 0
37                    {
38                        output_events.emit_server(ComboChangeEvent {
39                            entity: data.entity,
40                            change: -(combo.counter() as i32),
41                        });
42                    }
43                    if let CharacterState::Simple(c) = &mut update.character {
44                        c.timer = tick_attack_or_default(data, self.timer, None);
45                    }
46                } else {
47                    end_ability(data, &mut update);
48                }
49            },
50            _ => {
51                // If it somehow ends up in an incorrect stage section
52                end_ability(data, &mut update);
53            },
54        }
55
56        // At end of state logic so an interrupt isn't overwritten
57        handle_interrupts(data, &mut update, output_events);
58
59        update
60    }
61}