veloren/common/src/comp/controller.rs

199 lines
5.1 KiB
Rust
Raw Normal View History

use crate::{
comp::{
inventory::slot::{EquipSlot, InvSlotId, Slot},
invite::{InviteKind, InviteResponse},
BuffKind,
},
trade::{TradeAction, TradeId},
uid::Uid,
util::Dir,
};
use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage};
use specs_idvs::IdvStorage;
2021-03-14 18:42:39 +00:00
use std::collections::BTreeSet;
2019-07-29 19:54:58 +00:00
use vek::*;
2019-06-09 14:20:20 +00:00
2020-04-04 17:51:41 +00:00
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum InventoryEvent {
2020-04-04 17:51:41 +00:00
Pickup(Uid),
Collect(Vec3<i32>),
Swap(InvSlotId, InvSlotId),
2021-03-02 00:08:46 +00:00
SplitSwap(InvSlotId, InvSlotId),
Drop(InvSlotId),
2021-03-02 00:08:46 +00:00
SplitDrop(InvSlotId),
2020-07-14 20:11:39 +00:00
CraftRecipe(String),
2020-04-04 17:51:41 +00:00
}
2021-02-07 16:57:41 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum InventoryAction {
Swap(EquipSlot, Slot),
Drop(EquipSlot),
Use(Slot),
2021-02-07 16:57:41 +00:00
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum InventoryManip {
Pickup(Uid),
Collect(Vec3<i32>),
Use(Slot),
Swap(Slot, Slot),
2021-03-02 00:08:46 +00:00
SplitSwap(Slot, Slot),
Drop(Slot),
2021-03-02 00:08:46 +00:00
SplitDrop(Slot),
CraftRecipe(String),
}
impl From<InventoryAction> for InventoryManip {
fn from(inv_action: InventoryAction) -> Self {
match inv_action {
InventoryAction::Use(slot) => Self::Use(slot),
InventoryAction::Swap(equip, slot) => Self::Swap(Slot::Equip(equip), slot),
InventoryAction::Drop(equip) => Self::Drop(Slot::Equip(equip)),
2021-02-07 16:57:41 +00:00
}
}
}
impl From<InventoryEvent> for InventoryManip {
fn from(inv_event: InventoryEvent) -> Self {
match inv_event {
InventoryEvent::Pickup(pickup) => Self::Pickup(pickup),
InventoryEvent::Collect(collect) => Self::Collect(collect),
InventoryEvent::Swap(inv1, inv2) => {
Self::Swap(Slot::Inventory(inv1), Slot::Inventory(inv2))
},
InventoryEvent::SplitSwap(inv1, inv2) => {
2021-03-02 00:08:46 +00:00
Self::SplitSwap(Slot::Inventory(inv1), Slot::Inventory(inv2))
},
InventoryEvent::Drop(inv) => Self::Drop(Slot::Inventory(inv)),
InventoryEvent::SplitDrop(inv) => Self::SplitDrop(Slot::Inventory(inv)),
InventoryEvent::CraftRecipe(recipe) => Self::CraftRecipe(recipe),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum GroupManip {
Leave,
Kick(Uid),
AssignLeader(Uid),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ControlEvent {
2020-10-07 02:23:20 +00:00
//ToggleLantern,
EnableLantern,
DisableLantern,
Interact(Uid),
InitiateInvite(Uid, InviteKind),
InviteResponse(InviteResponse),
PerformTradeAction(TradeId, TradeAction),
Mount(Uid),
Unmount,
InventoryEvent(InventoryEvent),
GroupManip(GroupManip),
RemoveBuff(BuffKind),
2020-03-24 07:38:16 +00:00
Respawn,
}
2021-02-07 16:57:41 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
2020-03-24 07:38:16 +00:00
pub enum ControlAction {
SwapEquippedWeapons,
InventoryAction(InventoryAction),
2020-03-26 15:05:17 +00:00
Wield,
GlideWield,
2020-03-26 15:05:17 +00:00
Unwield,
Sit,
2020-05-27 06:41:55 +00:00
Dance,
2020-08-02 05:09:11 +00:00
Sneak,
2020-03-26 15:05:17 +00:00
Stand,
Talk,
StartInput {
2021-03-12 21:01:30 +00:00
input: InputKind,
target: Option<Uid>,
},
CancelInput(InputKind),
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Ord, PartialOrd)]
#[repr(u32)]
pub enum InputKind {
Primary = 0,
2021-03-13 00:38:20 +00:00
Secondary = 1,
2021-03-14 18:42:39 +00:00
Ability(usize) = 2,
Roll = 3,
Jump = 4,
Fly = 5,
}
impl InputKind {
pub fn is_ability(self) -> bool {
matches!(self, Self::Primary | Self::Secondary | Self::Ability(_))
}
2019-11-29 15:20:35 +00:00
}
2020-03-24 07:38:16 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum Climb {
Up,
Down,
Hold,
}
2019-06-09 14:20:20 +00:00
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ControllerInputs {
2020-03-24 07:38:16 +00:00
pub climb: Option<Climb>,
pub move_dir: Vec2<f32>,
2020-11-03 22:46:07 +00:00
pub move_z: f32, /* z axis (not combined with move_dir because they may have independent
* limits) */
pub look_dir: Dir,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Controller {
pub inputs: ControllerInputs,
pub queued_inputs: BTreeSet<InputKind>,
// TODO: consider SmallVec
pub events: Vec<ControlEvent>,
2020-03-24 07:38:16 +00:00
pub actions: Vec<ControlAction>,
}
2019-11-29 15:20:35 +00:00
impl ControllerInputs {
2020-03-24 07:38:16 +00:00
/// Updates Controller inputs with new version received from the client
pub fn update_with_new(&mut self, new: Self) {
self.climb = new.climb;
self.move_dir = new.move_dir;
2020-11-03 22:46:07 +00:00
self.move_z = new.move_z;
2020-03-24 07:38:16 +00:00
self.look_dir = new.look_dir;
}
2019-11-29 15:20:35 +00:00
}
impl Controller {
2019-11-29 15:20:35 +00:00
/// Sets all inputs to default
pub fn reset(&mut self) { *self = Self::default(); }
pub fn clear_events(&mut self) { self.events.clear(); }
pub fn push_event(&mut self, event: ControlEvent) { self.events.push(event); }
2019-06-09 14:20:20 +00:00
}
impl Component for Controller {
type Storage = IdvStorage<Self>;
2019-06-09 14:20:20 +00:00
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum MountState {
Unmounted,
MountedBy(Uid),
}
impl Component for MountState {
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Mounting(pub Uid);
impl Component for Mounting {
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
}