veloren/common/src/sys/character_behavior.rs

210 lines
6.6 KiB
Rust
Raw Normal View History

2019-12-20 13:30:37 +00:00
use crate::{
comp::{
AbilityPool, Attacking, Body, CharacterState, Controller, ControllerInputs, Energy,
Mounting, Ori, PhysicsState, Pos, StateUpdate, Stats, Vel,
2019-12-20 13:30:37 +00:00
},
2019-12-26 14:43:59 +00:00
event::{EventBus, LocalEvent, ServerEvent},
2019-12-20 13:30:37 +00:00
state::DeltaTime,
2020-03-07 18:15:02 +00:00
states,
2020-01-16 13:28:45 +00:00
sync::{Uid, UidAllocator},
2019-12-20 13:30:37 +00:00
};
2020-03-07 18:15:02 +00:00
use specs::{Entities, Entity, Join, LazyUpdate, Read, ReadStorage, System, WriteStorage};
2020-03-07 21:03:10 +00:00
// use std::collections::VecDeque;
2020-03-07 18:15:02 +00:00
pub trait CharacterBehavior {
fn behavior(&self, data: &JoinData) -> StateUpdate;
// fn init(data: &JoinData) -> CharacterState;
}
2020-03-07 18:15:02 +00:00
/// Read-Only Data sent from Character Behavior System to bahvior fn's
pub struct JoinData<'a> {
pub entity: 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 controller: &'a Controller,
pub inputs: &'a ControllerInputs,
pub stats: &'a Stats,
pub energy: &'a Energy,
pub body: &'a Body,
pub physics: &'a PhysicsState,
pub ability_pool: &'a AbilityPool,
pub attacking: Option<&'a Attacking>,
2020-03-07 18:15:02 +00:00
pub updater: &'a LazyUpdate,
}
pub type JoinTuple<'a> = (
Entity,
&'a Uid,
&'a mut CharacterState,
&'a mut Pos,
&'a mut Vel,
&'a mut Ori,
&'a mut Energy,
&'a Controller,
&'a Stats,
&'a Body,
&'a PhysicsState,
&'a AbilityPool,
Option<&'a Attacking>,
2020-03-07 18:15:02 +00:00
);
impl<'a> JoinData<'a> {
fn new(j: &'a JoinTuple<'a>, updater: &'a LazyUpdate, dt: &'a DeltaTime) -> Self {
Self {
entity: j.0,
uid: j.1,
character: j.2,
pos: j.3,
vel: j.4,
ori: j.5,
energy: j.6,
controller: j.7,
inputs: &j.7.inputs,
stats: j.8,
body: j.9,
physics: j.10,
ability_pool: j.11,
attacking: j.12,
2020-03-07 18:15:02 +00:00
updater,
dt,
}
}
}
/// /// ## Character State System
2020-02-24 18:17:16 +00:00
/// #### Calls updates to `CharacterState`s. Acts on tuples of (
/// `CharacterState`, `Pos`, `Vel`, and `Ori` ).
2019-12-29 16:36:59 +00:00
///
2020-03-07 18:15:02 +00:00
/// _System forms `CharacterEntityData` tuples and passes those to `ActionState`
2020-02-24 18:17:16 +00:00
/// `update()` fn, then does the same for `MoveState` `update`_
2019-12-20 13:30:37 +00:00
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
Read<'a, UidAllocator>,
Read<'a, EventBus<ServerEvent>>,
Read<'a, EventBus<LocalEvent>>,
Read<'a, DeltaTime>,
2019-12-26 14:43:59 +00:00
Read<'a, LazyUpdate>,
2019-12-20 13:30:37 +00:00
WriteStorage<'a, CharacterState>,
2019-12-21 15:57:15 +00:00
WriteStorage<'a, Pos>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Ori>,
2020-02-24 18:17:16 +00:00
WriteStorage<'a, Energy>,
2019-12-20 13:30:37 +00:00
ReadStorage<'a, Controller>,
ReadStorage<'a, Stats>,
ReadStorage<'a, Body>,
ReadStorage<'a, PhysicsState>,
2020-02-03 10:54:50 +00:00
ReadStorage<'a, AbilityPool>,
ReadStorage<'a, Attacking>,
2019-12-20 13:30:37 +00:00
ReadStorage<'a, Uid>,
ReadStorage<'a, Mounting>,
);
2020-02-24 18:17:16 +00:00
2019-12-20 13:30:37 +00:00
fn run(
&mut self,
(
entities,
2019-12-26 18:01:19 +00:00
_uid_allocator,
2019-12-20 13:30:37 +00:00
server_bus,
local_bus,
dt,
2019-12-26 14:43:59 +00:00
updater,
2019-12-20 13:30:37 +00:00
mut character_states,
2019-12-21 15:57:15 +00:00
mut positions,
mut velocities,
mut orientations,
2020-02-24 18:17:16 +00:00
mut energies,
2019-12-20 13:30:37 +00:00
controllers,
stats,
bodies,
physics_states,
2020-02-03 10:54:50 +00:00
ability_pools,
attacking_storage,
2019-12-20 13:30:37 +00:00
uids,
mountings,
): Self::SystemData,
) {
2020-03-07 18:15:02 +00:00
let mut join_iter = (
2019-12-20 13:30:37 +00:00
&entities,
&uids,
&mut character_states,
2019-12-21 15:57:15 +00:00
&mut positions,
&mut velocities,
&mut orientations,
2020-02-24 18:17:16 +00:00
&mut energies,
2019-12-20 13:30:37 +00:00
&controllers,
&stats,
&bodies,
&physics_states,
2020-02-03 10:54:50 +00:00
&ability_pools,
attacking_storage.maybe(),
2019-12-20 13:30:37 +00:00
)
2020-03-07 18:15:02 +00:00
.join();
while let Some(tuple) = join_iter.next() {
let j = JoinData::new(&tuple, &updater, &dt);
let inputs = &j.inputs;
2019-12-29 23:47:42 +00:00
// Being dead overrides all other states
2020-03-07 18:15:02 +00:00
if j.stats.is_dead {
2019-12-29 23:47:42 +00:00
// Only options: click respawn
// prevent instant-respawns (i.e. player was holding attack)
// by disallowing while input is held down
if inputs.respawn.is_pressed() && !inputs.respawn.is_held_down() {
2020-03-07 18:15:02 +00:00
server_bus.emitter().emit(ServerEvent::Respawn(j.entity));
2019-12-29 23:47:42 +00:00
}
// Or do nothing
return;
}
// If mounted, character state is controlled by mount
// TODO: Make mounting a state
2020-03-07 18:15:02 +00:00
if let Some(Mounting(_)) = mountings.get(j.entity) {
*tuple.2 = CharacterState::Sit {};
2019-12-29 23:47:42 +00:00
return;
}
2020-03-07 18:15:02 +00:00
let mut state_update = match j.character {
CharacterState::Idle { .. } => states::idle::behavior(&j),
CharacterState::Climb { .. } => states::climb::behavior(&j),
CharacterState::Glide { .. } => states::glide::behavior(&j),
CharacterState::Roll { .. } => states::roll::behavior(&j),
CharacterState::Wielding { .. } => states::wielding::behavior(&j),
CharacterState::Equipping { .. } => states::equipping::behavior(&j),
2020-03-07 19:55:15 +00:00
CharacterState::BasicBlock { .. } => states::basic_block::behavior(&j),
2020-03-08 17:04:26 +00:00
CharacterState::ChargeAttack { .. } => states::charge_attack::behavior(&j),
2020-03-07 18:15:02 +00:00
CharacterState::Sit { .. } => states::sit::behavior(&j),
CharacterState::TripleStrike { .. } => states::triple_strike::behavior(&j),
CharacterState::BasicAttack (state) => state.behavior(&j),
CharacterState::TimedCombo(state) => state.behavior(&j),
2020-03-07 18:15:02 +00:00
2020-03-08 19:37:17 +00:00
// Do not use default match.
2020-03-07 21:03:10 +00:00
// _ => StateUpdate {
// character: *j.character,
// pos: *j.pos,
// vel: *j.vel,
// ori: *j.ori,
// energy: *j.energy,
// local_events: VecDeque::new(),
// server_events: VecDeque::new(),
// },
2020-03-07 18:15:02 +00:00
};
2019-12-26 14:43:59 +00:00
2020-03-07 18:15:02 +00:00
*tuple.2 = state_update.character;
*tuple.3 = state_update.pos;
*tuple.4 = state_update.vel;
*tuple.5 = state_update.ori;
*tuple.6 = state_update.energy;
2020-02-03 10:54:50 +00:00
local_bus.emitter().append(&mut state_update.local_events);
server_bus.emitter().append(&mut state_update.server_events);
2019-12-29 23:47:42 +00:00
}
}
}