veloren/common/src/comp/character_state.rs

180 lines
5.3 KiB
Rust
Raw Normal View History

2019-12-26 14:43:59 +00:00
use self::ActionState::*;
use super::states::*;
use crate::{
comp::{Body, ControllerInputs, Ori, PhysicsState, Pos, Stats, Vel},
event::{EventBus, LocalEvent, ServerEvent},
state::DeltaTime,
};
2020-01-05 18:19:09 +00:00
use serde::Deserialize;
use serde::Serialize;
2019-12-26 14:43:59 +00:00
use specs::LazyUpdate;
use specs::{Component, Entity, FlaggedStorage, HashMapStorage, NullStorage};
use sphynx::Uid;
use std::time::Duration;
2019-12-20 13:30:37 +00:00
2019-12-28 16:10:39 +00:00
pub struct EcsStateData<'a> {
2019-12-26 14:43:59 +00:00
pub entity: &'a Entity,
pub uid: &'a Uid,
pub character: &'a CharacterState,
pub pos: &'a Pos,
pub vel: &'a Vel,
pub ori: &'a Ori,
pub dt: &'a DeltaTime,
pub inputs: &'a ControllerInputs,
pub stats: &'a Stats,
pub body: &'a Body,
pub physics: &'a PhysicsState,
pub updater: &'a LazyUpdate,
pub server_bus: &'a EventBus<ServerEvent>,
pub local_bus: &'a EventBus<LocalEvent>,
}
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,
}
2019-12-20 13:30:37 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
2019-12-26 14:43:59 +00:00
pub enum MoveState {
2020-01-08 16:56:36 +00:00
Stand(Option<stand::State>),
Run(Option<run::State>),
Sit(Option<sit::State>),
Jump(Option<jump::State>),
Fall(Option<fall::State>),
Glide(Option<glide::State>),
Swim(Option<swim::State>),
Climb(Option<climb::State>),
2019-12-26 14:43:59 +00:00
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
2019-12-26 14:43:59 +00:00
pub enum ActionState {
2020-01-08 16:56:36 +00:00
Idle(Option<idle::State>),
Wield(Option<wield::State>),
2019-12-26 14:43:59 +00:00
Attack(AttackKind),
Block(BlockKind),
Dodge(DodgeKind),
2019-12-28 16:10:39 +00:00
// Interact?,
2019-12-26 14:43:59 +00:00
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
2019-12-26 14:43:59 +00:00
pub enum AttackKind {
2020-01-08 16:56:36 +00:00
BasicAttack(Option<basic_attack::State>),
Charge(Option<charge_attack::State>),
2019-12-26 14:43:59 +00:00
}
2019-12-20 13:30:37 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
2019-12-26 14:43:59 +00:00
pub enum BlockKind {
2020-01-08 16:56:36 +00:00
BasicBlock(Option<basic_block::State>),
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
2019-12-26 14:43:59 +00:00
pub enum DodgeKind {
2020-01-08 16:56:36 +00:00
Roll(Option<roll::State>),
}
impl ActionState {
2019-12-26 14:43:59 +00:00
pub fn is_equip_finished(&self) -> bool {
match self {
2020-01-08 16:56:36 +00:00
Wield(Some(wield::State { equip_delay })) => *equip_delay == Duration::default(),
2019-12-26 14:43:59 +00:00
_ => true,
}
}
2019-12-29 16:36:59 +00:00
/// Returns the current `equip_delay` if in `WieldState`, otherwise `Duration::default()`
2019-12-26 14:43:59 +00:00
pub fn get_delay(&self) -> Duration {
match self {
2020-01-08 16:56:36 +00:00
Wield(Some(wield::State { equip_delay })) => *equip_delay,
2019-12-26 14:43:59 +00:00
_ => Duration::default(),
}
}
2019-12-26 14:43:59 +00:00
pub fn is_attacking(&self) -> bool {
match self {
2020-01-05 18:19:09 +00:00
Attack(_) => true,
2019-12-26 14:43:59 +00:00
_ => false,
}
}
2019-12-26 14:43:59 +00:00
pub fn is_blocking(&self) -> bool {
match self {
2020-01-05 18:19:09 +00:00
Block(_) => true,
2019-12-26 14:43:59 +00:00
_ => false,
}
}
2019-12-26 14:43:59 +00:00
pub fn is_dodging(&self) -> bool {
match self {
Dodge(_) => true,
_ => false,
}
}
2020-01-08 19:31:42 +00:00
/// Compares `action_state`s for shallow equality (does not check internal struct equality)
pub fn equals(&self, other: &Self) -> bool {
// Check if state is the same without looking at the inner data
std::mem::discriminant(&self) == std::mem::discriminant(&other)
}
2020-01-08 19:31:42 +00:00
}
impl MoveState {
/// Compares `move_state`s for shallow equality (does not check internal struct equality)
pub fn equals(&self, other: &Self) -> bool {
// Check if state is the same without looking at the inner data
std::mem::discriminant(&self) == std::mem::discriminant(&other)
}
}
2019-12-29 23:47:42 +00:00
/// __A concurrent state machine that allows for separate `ActionState`s and `MoveState`s.__
2020-01-05 18:19:09 +00:00
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct CharacterState {
2019-12-28 16:10:39 +00:00
/// __How the character is currently moving, e.g. Running, Standing, Falling.__
///
/// _Primarily `handle()`s updating `Pos`, `Vel`, `Ori`, and lower body animations.
2019-12-26 14:43:59 +00:00
pub move_state: MoveState,
2019-12-28 16:10:39 +00:00
/// __How the character is currently acting, e.g. Wielding, Attacking, Dodging.__
///
/// _Primarily `handle()`s how character interacts with world, and upper body animations.
2019-12-26 14:43:59 +00:00
pub action_state: ActionState,
}
impl CharacterState {
2019-12-31 13:19:23 +00:00
/// Compares both `move_state`s and `action_state`a for shallow equality
/// (does not check internal struct equality)
2020-01-08 19:31:42 +00:00
pub fn equals(&self, other: &Self) -> bool {
self.move_state.equals(&other.move_state) && self.action_state.equals(&other.action_state)
}
}
impl Default for CharacterState {
fn default() -> Self {
Self {
2020-01-08 16:56:36 +00:00
move_state: MoveState::Fall(None),
action_state: ActionState::Idle(None),
}
}
}
impl Component for CharacterState {
type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
}
2019-12-26 14:43:59 +00:00
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct OverrideState;
impl Component for OverrideState {
type Storage = FlaggedStorage<Self, NullStorage<Self>>;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct OverrideAction;
impl Component for OverrideAction {
type Storage = FlaggedStorage<Self, NullStorage<Self>>;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct OverrideMove;
impl Component for OverrideMove {
type Storage = FlaggedStorage<Self, NullStorage<Self>>;
}