diff --git a/Cargo.lock b/Cargo.lock index 0a4aa514c9..272df33c8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3626,7 +3626,6 @@ dependencies = [ "crossbeam 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "hibitset 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", - "indexmap 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "portpicker 0.1.0 (git+https://github.com/wusyong/portpicker-rs?branch=fix_ipv6)", diff --git a/client/src/lib.rs b/client/src/lib.rs index 73350e9bf2..1aeb7c7b31 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -8,7 +8,7 @@ pub use crate::error::Error; pub use specs::{join::Join, saveload::Marker, Entity as EcsEntity, ReadStorage}; use common::{ - comp, + comp::{self, ControlEvent, Controller, ControllerInputs, InventoryManip}, msg::{ validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg, MAX_BYTES_CHAT_MSG, @@ -201,22 +201,33 @@ impl Client { // Can't fail } - pub fn use_inventory_slot(&mut self, x: usize) { - self.postbox.send_message(ClientMsg::UseInventorySlot(x)) + pub fn use_inventory_slot(&mut self, slot: usize) { + self.postbox + .send_message(ClientMsg::ControlEvent(ControlEvent::InventoryManip( + InventoryManip::Use(slot), + ))); } pub fn swap_inventory_slots(&mut self, a: usize, b: usize) { self.postbox - .send_message(ClientMsg::SwapInventorySlots(a, b)) + .send_message(ClientMsg::ControlEvent(ControlEvent::InventoryManip( + InventoryManip::Swap(a, b), + ))); } - pub fn drop_inventory_slot(&mut self, x: usize) { - self.postbox.send_message(ClientMsg::DropInventorySlot(x)) + pub fn drop_inventory_slot(&mut self, slot: usize) { + self.postbox + .send_message(ClientMsg::ControlEvent(ControlEvent::InventoryManip( + InventoryManip::Drop(slot), + ))); } pub fn pick_up(&mut self, entity: EcsEntity) { if let Some(uid) = self.state.ecs().read_storage::().get(entity).copied() { - self.postbox.send_message(ClientMsg::PickUp(uid.id())); + self.postbox + .send_message(ClientMsg::ControlEvent(ControlEvent::InventoryManip( + InventoryManip::Pickup(uid), + ))); } } @@ -228,6 +239,18 @@ impl Client { .is_some() } + pub fn mount(&mut self, entity: EcsEntity) { + if let Some(uid) = self.state.ecs().read_storage::().get(entity).copied() { + self.postbox + .send_message(ClientMsg::ControlEvent(ControlEvent::Mount(uid))); + } + } + + pub fn unmount(&mut self) { + self.postbox + .send_message(ClientMsg::ControlEvent(ControlEvent::Unmount)); + } + pub fn view_distance(&self) -> Option { self.view_distance } @@ -283,16 +306,15 @@ impl Client { } pub fn collect_block(&mut self, pos: Vec3) { - self.postbox.send_message(ClientMsg::CollectBlock(pos)); + self.postbox + .send_message(ClientMsg::ControlEvent(ControlEvent::InventoryManip( + InventoryManip::Collect(pos), + ))); } /// Execute a single client tick, handle input and update the game state by the given duration. #[allow(dead_code)] - pub fn tick( - &mut self, - controller: comp::Controller, - dt: Duration, - ) -> Result, Error> { + pub fn tick(&mut self, inputs: ControllerInputs, dt: Duration) -> Result, Error> { // This tick function is the centre of the Veloren universe. Most client-side things are // managed from here, and as such it's important that it stays organised. Please consult // the core developers before making significant changes to this code. Here is the @@ -310,8 +332,15 @@ impl Client { // 1) Handle input from frontend. // Pass character actions from frontend input to the player's entity. if let ClientState::Character | ClientState::Dead = self.client_state { - self.state.write_component(self.entity, controller.clone()); - self.postbox.send_message(ClientMsg::Controller(controller)); + self.state.write_component( + self.entity, + Controller { + inputs: inputs.clone(), + events: Vec::new(), + }, + ); + self.postbox + .send_message(ClientMsg::ControllerInputs(inputs)); } // 2) Build up a list of events for this frame, to be passed to the frontend. @@ -319,7 +348,7 @@ impl Client { // Prepare for new events { - let ecs = self.state.ecs_mut(); + let ecs = self.state.ecs(); for (entity, _) in (&ecs.entities(), &ecs.read_storage::()).join() { let mut last_character_states = ecs.write_storage::>(); @@ -343,7 +372,7 @@ impl Client { // 3) Update client local data // 4) Tick the client's LocalState - self.state.tick(dt); + self.state.tick(dt, |_| {}); // 5) Terrain let pos = self diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index 178e5a628e..79de5e1d8f 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -7,14 +7,14 @@ use vek::*; pub enum ControlEvent { Mount(Uid), Unmount, + InventoryManip(InventoryManip), + //Respawn, } #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] -pub struct Controller { +pub struct ControllerInputs { pub primary: bool, pub secondary: bool, - pub move_dir: Vec2, - pub look_dir: Vec3, pub sit: bool, pub jump: bool, pub roll: bool, @@ -23,6 +23,13 @@ pub struct Controller { pub climb_down: bool, pub wall_leap: bool, pub respawn: bool, + pub move_dir: Vec2, + pub look_dir: Vec3, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct Controller { + pub inputs: ControllerInputs, pub events: Vec, } @@ -60,3 +67,12 @@ pub struct Mounting(pub Uid); impl Component for Mounting { type Storage = FlaggedStorage>; } + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum InventoryManip { + Pickup(Uid), + Collect(Vec3), + Use(usize), + Swap(usize, usize), + Drop(usize), +} diff --git a/common/src/comp/mod.rs b/common/src/comp/mod.rs index 438da87db6..5df88295b5 100644 --- a/common/src/comp/mod.rs +++ b/common/src/comp/mod.rs @@ -18,7 +18,9 @@ pub use admin::Admin; pub use agent::Agent; pub use body::{humanoid, object, quadruped, quadruped_medium, Body}; pub use character_state::{ActionState, CharacterState, MovementState}; -pub use controller::{ControlEvent, Controller, MountState, Mounting}; +pub use controller::{ + ControlEvent, Controller, ControllerInputs, InventoryManip, MountState, Mounting, +}; pub use inputs::CanBuild; pub use inventory::{item, Inventory, InventoryUpdate, Item}; pub use last::Last; diff --git a/common/src/event.rs b/common/src/event.rs index b2c771ab8a..8bc938d5e0 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -30,6 +30,7 @@ pub enum ServerEvent { entity: EcsEntity, cause: comp::HealthSource, }, + InventoryManip(EcsEntity, comp::InventoryManip), Respawn(EcsEntity), Shoot { entity: EcsEntity, @@ -46,6 +47,15 @@ pub enum ServerEvent { Mount(EcsEntity, EcsEntity), Unmount(EcsEntity), Possess(Uid, Uid), + CreatePlayer { + entity: EcsEntity, + name: String, + body: comp::Body, + main: Option, + }, + ClientDisconnect(EcsEntity), + ChunkRequest(EcsEntity, Vec2), + ChatCmd(EcsEntity, String), } pub struct EventBus { diff --git a/common/src/msg/client.rs b/common/src/msg/client.rs index 1e98732283..c699db93d1 100644 --- a/common/src/msg/client.rs +++ b/common/src/msg/client.rs @@ -14,12 +14,12 @@ pub enum ClientMsg { body: comp::Body, main: Option, }, - Controller(comp::Controller), + ControllerInputs(comp::ControllerInputs), + ControlEvent(comp::ControlEvent), RequestState(ClientState), SetViewDistance(u32), BreakBlock(Vec3), PlaceBlock(Vec3, Block), - CollectBlock(Vec3), Ping, Pong, ChatMsg { @@ -31,10 +31,6 @@ pub enum ClientMsg { vel: comp::Vel, ori: comp::Ori, }, - UseInventorySlot(usize), - SwapInventorySlots(usize, usize), - DropInventorySlot(usize), - PickUp(u64), TerrainChunkRequest { key: Vec2, }, diff --git a/common/src/region.rs b/common/src/region.rs index da8b2fa90a..1bbcc72eeb 100644 --- a/common/src/region.rs +++ b/common/src/region.rs @@ -195,9 +195,6 @@ impl RegionMap { } } } - pub fn add(&mut self, entity: EcsEntity, pos: Vec3) { - self.add_entity(entity.id(), pos.map(|e| e as i32), None); - } fn add_entity(&mut self, id: u32, pos: Vec3, from: Option>) { let key = Self::pos_key(pos); if let Some(region) = self.regions.get_mut(&key) { diff --git a/common/src/state.rs b/common/src/state.rs index 0859323d4d..640b283520 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -309,7 +309,7 @@ impl State { } /// Execute a single tick, simulating the game state by the given duration. - pub fn tick(&mut self, dt: Duration) { + pub fn tick(&mut self, dt: Duration, add_foreign_systems: impl Fn(&mut DispatcherBuilder)) { // Change the time accordingly. self.ecs.write_resource::().0 += dt.as_secs_f64() * DAY_CYCLE_FACTOR; self.ecs.write_resource::