veloren_common/states/
equipping.rs

1use super::utils::*;
2use crate::{
3    comp::{CharacterState, StateUpdate, character_state::OutputEvents},
4    states::{
5        behavior::{CharacterBehavior, JoinData},
6        wielding,
7    },
8};
9use serde::{Deserialize, Serialize};
10use std::time::Duration;
11
12/// Separated out to condense update portions of character state
13#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
14pub struct StaticData {
15    /// Time required to draw weapon
16    pub buildup_duration: Duration,
17}
18
19#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
20pub struct Data {
21    /// Struct containing data that does not change over the course of the
22    /// character state
23    pub static_data: StaticData,
24    /// Timer for each stage
25    pub timer: Duration,
26    pub is_sneaking: bool,
27}
28
29impl CharacterBehavior for Data {
30    fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
31        let mut update = StateUpdate::from(data);
32
33        handle_orientation(data, &mut update, 1.0, None);
34        handle_move(data, &mut update, if self.is_sneaking { 0.4 } else { 1.0 });
35        handle_jump(data, output_events, &mut update, 1.0);
36
37        if self.timer < self.static_data.buildup_duration {
38            // Draw weapon
39            update.character = CharacterState::Equipping(Data {
40                timer: tick_attack_or_default(data, self.timer, None),
41                ..*self
42            });
43        } else {
44            // Done
45            update.character = CharacterState::Wielding(wielding::Data {
46                is_sneaking: self.is_sneaking,
47            });
48        }
49
50        update
51    }
52}