veloren/common/src/comp/character_state.rs

125 lines
3.5 KiB
Rust
Raw Normal View History

2019-12-26 14:43:59 +00:00
use crate::{
2020-03-07 18:15:02 +00:00
comp::{Energy, Ori, Pos, ToolData, Vel},
2020-02-03 10:54:50 +00:00
event::{LocalEvent, ServerEvent},
states::*,
2019-12-26 14:43:59 +00:00
};
use serde::{Deserialize, Serialize};
2020-03-07 18:15:02 +00:00
use specs::{Component, FlaggedStorage, HashMapStorage, VecStorage};
use std::{collections::VecDeque, time::Duration};
2019-12-26 14:43:59 +00:00
2020-03-07 18:15:02 +00:00
/// Data returned from character behavior fn's to Character Behavior System.
2019-12-28 16:10:39 +00:00
pub struct StateUpdate {
2019-12-26 14:43:59 +00:00
pub character: CharacterState,
pub pos: Pos,
pub vel: Vel,
pub ori: Ori,
2020-02-24 18:17:16 +00:00
pub energy: Energy,
2020-02-03 10:54:50 +00:00
pub local_events: VecDeque<LocalEvent>,
pub server_events: VecDeque<ServerEvent>,
2019-12-26 14:43:59 +00:00
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum CharacterState {
2020-03-14 15:40:29 +00:00
Idle,
Climb,
2020-03-07 18:15:02 +00:00
Sit {},
Equipping {
/// The weapon being equipped
tool: ToolData,
/// Time left before next state
time_left: Duration,
},
Wielding {
/// The weapon being wielded
tool: ToolData,
},
2020-03-14 15:40:29 +00:00
Glide,
2020-03-07 18:15:02 +00:00
/// A basic blocking state
2020-03-14 15:40:29 +00:00
BasicBlock,
2020-03-08 17:04:26 +00:00
ChargeAttack {
/// How long the state has until exiting
remaining_duration: Duration,
},
2020-03-07 18:15:02 +00:00
Roll {
/// How long the state has until exiting
remaining_duration: Duration,
},
/// A basic attacking state
BasicAttack(basic_attack::State),
2020-03-08 19:37:17 +00:00
/// A three-stage attack where play must click at appropriate times
/// to continue attack chain.
TimedCombo(timed_combo::State),
2020-03-12 14:25:06 +00:00
/// A three-stage attack where each attack pushes player forward
/// and successive attacks increase in damage, while player holds button.
TripleStrike {
/// The tool this state will read to handle damage, etc.
tool: ToolData,
/// `int` denoting what stage (of 3) the attack is in.
stage: i8,
/// How long current stage has been active
stage_time_active: Duration,
/// Whether current stage has exhausted its attack
stage_exhausted: bool,
},
}
impl CharacterState {
pub fn is_wield(&self) -> bool {
match self {
CharacterState::Wielding { .. }
| CharacterState::BasicAttack(_)
| CharacterState::TimedCombo(_)
| CharacterState::BasicBlock { .. } => true,
_ => false,
}
}
pub fn is_attack(&self) -> bool {
match self {
CharacterState::BasicAttack(_)
| CharacterState::TimedCombo(_)
| CharacterState::ChargeAttack { .. } => true,
2019-12-26 14:43:59 +00:00
_ => false,
}
}
pub fn is_block(&self) -> bool {
2019-12-26 14:43:59 +00:00
match self {
2020-03-07 18:15:02 +00:00
CharacterState::BasicBlock { .. } => true,
2019-12-26 14:43:59 +00:00
_ => false,
}
}
pub fn is_dodge(&self) -> bool {
2019-12-26 14:43:59 +00:00
match self {
2020-03-07 18:15:02 +00:00
CharacterState::Roll { .. } => true,
2019-12-26 14:43:59 +00:00
_ => false,
}
}
2020-01-08 19:31:42 +00:00
2020-01-22 12:48:55 +00:00
/// Compares for shallow equality (does not check internal struct equality)
2020-01-08 19:31:42 +00:00
pub fn equals(&self, other: &Self) -> bool {
// Check if state is the same without looking at the inner data
2020-01-22 12:48:55 +00:00
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}
impl Default for CharacterState {
2020-03-07 18:15:02 +00:00
fn default() -> Self { Self::Idle {} }
}
impl Component for CharacterState {
type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
}
2020-02-11 15:42:17 +00:00
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Attacking {
pub weapon: Option<ToolData>,
pub applied: bool,
pub hit_count: u32,
2020-02-11 15:42:17 +00:00
}
impl Component for Attacking {
type Storage = VecStorage<Self>;
}