veloren/common/src/states/behavior.rs

157 lines
5.5 KiB
Rust

use crate::{
comp::{
self, item::MaterialStatManifest, Beam, Body, CharacterState, Combo, ControlAction,
Controller, ControllerInputs, Density, Energy, Health, InputAttr, InputKind, Inventory,
InventoryAction, Mass, Melee, Ori, PhysicsState, Pos, SkillSet, StateUpdate, Stats, Vel,
},
resources::DeltaTime,
terrain::TerrainGrid,
uid::Uid,
};
use specs::{storage::FlaggedAccessMut, Entity, LazyUpdate};
use vek::*;
pub trait CharacterBehavior {
fn behavior(&self, data: &JoinData) -> StateUpdate;
// Impl these to provide behavior for these inputs
fn swap_equipped_weapons(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn manipulate_loadout(&self, data: &JoinData, _inv_action: InventoryAction) -> StateUpdate {
StateUpdate::from(data)
}
fn wield(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn glide_wield(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn unwield(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn sit(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn dance(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn sneak(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn stand(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn talk(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) }
fn start_input(
&self,
data: &JoinData,
input: InputKind,
target_entity: Option<Uid>,
select_pos: Option<Vec3<f32>>,
) -> StateUpdate {
let mut update = StateUpdate::from(data);
update.queued_inputs.insert(input, InputAttr {
select_pos,
target_entity,
});
update
}
fn cancel_input(&self, data: &JoinData, input: InputKind) -> StateUpdate {
let mut update = StateUpdate::from(data);
update.removed_inputs.push(input);
update
}
fn handle_event(&self, data: &JoinData, event: ControlAction) -> StateUpdate {
match event {
ControlAction::SwapEquippedWeapons => self.swap_equipped_weapons(data),
ControlAction::InventoryAction(inv_action) => self.manipulate_loadout(data, inv_action),
ControlAction::Wield => self.wield(data),
ControlAction::GlideWield => self.glide_wield(data),
ControlAction::Unwield => self.unwield(data),
ControlAction::Sit => self.sit(data),
ControlAction::Dance => self.dance(data),
ControlAction::Sneak => self.sneak(data),
ControlAction::Stand => self.stand(data),
ControlAction::Talk => self.talk(data),
ControlAction::StartInput {
input,
target_entity,
select_pos,
} => self.start_input(data, input, target_entity, select_pos),
ControlAction::CancelInput(input) => self.cancel_input(data, input),
}
}
}
/// Read-Only Data sent from Character Behavior System to behavior 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 mass: &'a Mass,
pub density: &'a Density,
pub dt: &'a DeltaTime,
pub controller: &'a Controller,
pub inputs: &'a ControllerInputs,
pub health: Option<&'a Health>,
pub energy: &'a Energy,
pub inventory: Option<&'a Inventory>,
pub body: &'a Body,
pub physics: &'a PhysicsState,
pub melee_attack: Option<&'a Melee>,
pub updater: &'a LazyUpdate,
pub stats: &'a Stats,
pub skill_set: &'a SkillSet,
pub msm: &'a MaterialStatManifest,
pub combo: &'a Combo,
pub alignment: Option<&'a comp::Alignment>,
pub terrain: &'a TerrainGrid,
}
pub struct JoinStruct<'a> {
pub entity: Entity,
pub uid: &'a Uid,
pub char_state: FlaggedAccessMut<'a, &'a mut CharacterState, CharacterState>,
pub pos: &'a mut Pos,
pub vel: &'a mut Vel,
pub ori: &'a mut Ori,
pub mass: &'a Mass,
pub density: &'a mut Density,
pub energy: FlaggedAccessMut<'a, &'a mut Energy, Energy>,
pub inventory: Option<&'a Inventory>,
pub controller: &'a mut Controller,
pub health: Option<&'a Health>,
pub body: &'a Body,
pub physics: &'a PhysicsState,
pub melee_attack: Option<&'a Melee>,
pub beam: Option<&'a Beam>,
pub stat: &'a Stats,
pub skill_set: &'a SkillSet,
pub combo: &'a Combo,
pub alignment: Option<&'a comp::Alignment>,
pub terrain: &'a TerrainGrid,
}
impl<'a> JoinData<'a> {
pub fn new(
j: &'a JoinStruct<'a>,
updater: &'a LazyUpdate,
dt: &'a DeltaTime,
msm: &'a MaterialStatManifest,
) -> Self {
Self {
entity: j.entity,
uid: j.uid,
character: &j.char_state,
pos: j.pos,
vel: j.vel,
ori: j.ori,
mass: j.mass,
density: j.density,
energy: &j.energy,
inventory: j.inventory,
controller: j.controller,
inputs: &j.controller.inputs,
health: j.health,
body: j.body,
physics: j.physics,
melee_attack: j.melee_attack,
stats: j.stat,
skill_set: j.skill_set,
updater,
dt,
msm,
combo: j.combo,
alignment: j.alignment,
terrain: j.terrain,
}
}
}