From bf748ae742343f8fb977ed67ae09686bbb9af113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Tue, 2 Jul 2019 20:19:16 +0200 Subject: [PATCH 01/10] Add CanBuild component, Block placing client messages and build mode command --- client/src/lib.rs | 10 +++++++++- common/src/comp/inputs.rs | 7 +++++++ common/src/comp/mod.rs | 4 +++- common/src/msg/client.rs | 3 +++ common/src/state.rs | 3 ++- server/src/cmd.rs | 35 ++++++++++++++++++++++++++++++++++- server/src/lib.rs | 29 +++++++++++++++++++++++++++-- 7 files changed, 85 insertions(+), 6 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 748612aa87..28de1ea266 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -12,7 +12,7 @@ use common::{ msg::{ClientMsg, ClientState, ServerInfo, ServerMsg}, net::PostBox, state::State, - terrain::{chonk::ChonkMetrics, TerrainChunk, TerrainChunkSize}, + terrain::{block::Block, chonk::ChonkMetrics, TerrainChunk, TerrainChunkSize}, vol::VolSize, }; use log::{info, log_enabled, warn}; @@ -185,6 +185,14 @@ impl Client { self.pending_chunks.clear(); } + pub fn place_block(&mut self, pos: Vec3, block: Block) { + self.postbox.send_message(ClientMsg::PlaceBlock(pos, block)); + } + + pub fn remove_block(&mut self, pos: Vec3) { + self.postbox.send_message(ClientMsg::BreakBlock(pos)); + } + /// Execute a single client tick, handle input and update the game state by the given duration. #[allow(dead_code)] pub fn tick( diff --git a/common/src/comp/inputs.rs b/common/src/comp/inputs.rs index f03a5706b6..104e52187d 100644 --- a/common/src/comp/inputs.rs +++ b/common/src/comp/inputs.rs @@ -28,6 +28,9 @@ pub struct Rolling { #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct OnGround; +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct CanBuild; + #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct Jumping; @@ -85,6 +88,10 @@ impl Component for OnGround { type Storage = NullStorage; } +impl Component for CanBuild { + type Storage = NullStorage; +} + impl Component for Jumping { type Storage = NullStorage; } diff --git a/common/src/comp/mod.rs b/common/src/comp/mod.rs index 2ec6fdb8aa..d7d5c211dd 100644 --- a/common/src/comp/mod.rs +++ b/common/src/comp/mod.rs @@ -15,7 +15,9 @@ pub use agent::Agent; pub use animation::{Animation, AnimationInfo}; pub use body::{humanoid, quadruped, quadruped_medium, Body}; pub use controller::Controller; -pub use inputs::{Attacking, Gliding, Jumping, MoveDir, OnGround, Respawning, Rolling, Wielding}; +pub use inputs::{ + Attacking, CanBuild, Gliding, Jumping, MoveDir, OnGround, Respawning, Rolling, Wielding, +}; pub use inventory::{item, Inventory}; pub use phys::{ForceUpdate, Ori, Pos, Vel}; pub use player::Player; diff --git a/common/src/msg/client.rs b/common/src/msg/client.rs index db1c40d4c7..1da55e7857 100644 --- a/common/src/msg/client.rs +++ b/common/src/msg/client.rs @@ -1,5 +1,6 @@ use super::ClientState; use crate::comp; +use crate::terrain::block::Block; use vek::*; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -14,6 +15,8 @@ pub enum ClientMsg { Controller(comp::Controller), RequestState(ClientState), SetViewDistance(u32), + BreakBlock(Vec3), + PlaceBlock(Vec3, Block), Ping, Pong, Chat(String), diff --git a/common/src/state.rs b/common/src/state.rs index 5e7d3ef45c..943875a039 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -46,7 +46,7 @@ pub struct DeltaTime(pub f32); const MAX_DELTA_TIME: f32 = 1.0; pub struct TerrainChange { - blocks: HashMap, Block>, + pub blocks: HashMap, Block>, } impl Default for TerrainChange { @@ -136,6 +136,7 @@ impl State { ecs.register::(); ecs.register::(); ecs.register::(); + ecs.register::(); ecs.register::(); ecs.register::(); ecs.register::(); diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 90fe706299..9565d80c38 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -125,7 +125,13 @@ lazy_static! { "{}", "/health : Set your current health", handle_health, - ) + ), + ChatCommand::new( + "build", + "", + "/build : Toggles build mode on and off", + handle_build, + ), ]; } @@ -397,6 +403,33 @@ fn handle_empty(server: &mut Server, entity: EcsEntity, _args: String, _action: } } +fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { + match server.state.read_component_cloned::(entity) { + Some(_build_perms) => { + server + .state + .ecs() + .write_storage::() + .remove(entity); + server.clients.notify( + entity, + ServerMsg::Chat(String::from("Toggled on build mode!")), + ); + } + None => { + let _ = server + .state + .ecs() + .write_storage::() + .insert(entity, comp::CanBuild); + server.clients.notify( + entity, + ServerMsg::Chat(String::from("Toggled off build mode!")), + ); + } + } +} + fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { for cmd in CHAT_COMMANDS.iter() { server diff --git a/server/src/lib.rs b/server/src/lib.rs index f32530cbf6..7c63f3eef3 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -16,9 +16,10 @@ use common::{ comp, msg::{ClientMsg, ClientState, RequestStateError, ServerInfo, ServerMsg}, net::PostOffice, - state::{State, Uid}, - terrain::{TerrainChunk, TerrainChunkSize, TerrainMap}, + state::{State, TerrainChange, Uid}, + terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainMap}, vol::VolSize, + vol::Vox, }; use log::debug; use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity}; @@ -500,6 +501,30 @@ impl Server { // Only characters can send positions. _ => client.error_state(RequestStateError::Impossible), }, + ClientMsg::BreakBlock(pos) => { + if state + .ecs_mut() + .read_storage::() + .get(entity) + .is_some() + { + let mut terrain_change = + state.ecs().write_resource::(); + terrain_change.set(pos, Block::empty()); + } + } + ClientMsg::PlaceBlock(pos, block) => { + if state + .ecs_mut() + .read_storage::() + .get(entity) + .is_some() + { + let mut terrain_change = + state.ecs().write_resource::(); + terrain_change.set(pos, block); + } + } ClientMsg::TerrainChunkRequest { key } => match client.client_state { ClientState::Connected | ClientState::Registered From 7f08377001043812acc88ecf85b1f735aa3e3aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Tue, 2 Jul 2019 20:52:44 +0200 Subject: [PATCH 02/10] Sync CanBuild component, fix /build messages --- common/src/comp/inputs.rs | 2 +- common/src/msg/ecs_packet.rs | 2 ++ common/src/state.rs | 2 +- server/src/cmd.rs | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/common/src/comp/inputs.rs b/common/src/comp/inputs.rs index 104e52187d..576d23c490 100644 --- a/common/src/comp/inputs.rs +++ b/common/src/comp/inputs.rs @@ -89,7 +89,7 @@ impl Component for OnGround { } impl Component for CanBuild { - type Storage = NullStorage; + type Storage = FlaggedStorage>; } impl Component for Jumping { diff --git a/common/src/msg/ecs_packet.rs b/common/src/msg/ecs_packet.rs index 76557b2e73..d1d7d25dae 100644 --- a/common/src/msg/ecs_packet.rs +++ b/common/src/msg/ecs_packet.rs @@ -22,6 +22,7 @@ sphynx::sum_type! { Ori(comp::Ori), Body(comp::Body), Player(comp::Player), + CanBuild(comp::CanBuild), Stats(comp::Stats), } } @@ -35,6 +36,7 @@ sphynx::sum_type! { Ori(PhantomData), Body(PhantomData), Player(PhantomData), + CanBuild(PhantomData), Stats(PhantomData), } } diff --git a/common/src/state.rs b/common/src/state.rs index 943875a039..82ceadeed9 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -129,6 +129,7 @@ impl State { ecs.register_synced::(); ecs.register_synced::(); ecs.register_synced::(); + ecs.register_synced::(); // Register components synced by other means ecs.register::(); @@ -136,7 +137,6 @@ impl State { ecs.register::(); ecs.register::(); ecs.register::(); - ecs.register::(); ecs.register::(); ecs.register::(); ecs.register::(); diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 9565d80c38..557858aad1 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -413,7 +413,7 @@ fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: .remove(entity); server.clients.notify( entity, - ServerMsg::Chat(String::from("Toggled on build mode!")), + ServerMsg::Chat(String::from("Toggled off build mode!")), ); } None => { @@ -424,7 +424,7 @@ fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: .insert(entity, comp::CanBuild); server.clients.notify( entity, - ServerMsg::Chat(String::from("Toggled off build mode!")), + ServerMsg::Chat(String::from("Toggled on build mode!")), ); } } From 7cf299503d8bce8f355e41fced19da1e0d6a0af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Tue, 2 Jul 2019 21:54:38 +0200 Subject: [PATCH 03/10] Add crosshair --- assets/voxygen/element/misc_bg/crosshair.vox | 4 ++-- voxygen/src/hud/img_ids.rs | 6 +++--- voxygen/src/hud/mod.rs | 10 ++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/assets/voxygen/element/misc_bg/crosshair.vox b/assets/voxygen/element/misc_bg/crosshair.vox index 37f0d80d5b..6031040ab2 100644 --- a/assets/voxygen/element/misc_bg/crosshair.vox +++ b/assets/voxygen/element/misc_bg/crosshair.vox @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4ebf4d14b5deaf1e4a5645ebe143024522d66991026328400245f7d4f3e07580 -size 5352 +oid sha256:0776f8ad7b9a0f252335c5b75fe08b75a7f39ca5a30571a52bff934e7b83d366 +size 55912 diff --git a/voxygen/src/hud/img_ids.rs b/voxygen/src/hud/img_ids.rs index e67a8140c6..1262a16c86 100644 --- a/voxygen/src/hud/img_ids.rs +++ b/voxygen/src/hud/img_ids.rs @@ -49,9 +49,6 @@ image_ids! { chat_arrow_mo: "voxygen/element/buttons/arrow_down_hover.vox", chat_arrow_press: "voxygen/element/buttons/arrow_down_press.vox", - // Crosshair - crosshair: "voxygen/element/misc_bg/crosshair.vox", - //////////////////////////////////////////////////////////////////////// @@ -122,6 +119,9 @@ image_ids! { button_hover: "voxygen/element/buttons/button_hover.vox", button_press: "voxygen/element/buttons/button_press.vox", + // Other + crosshair: "voxygen/element/misc_bg/crosshair.vox", + ////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index a096c61b3d..cc5a55dcef 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -48,6 +48,9 @@ const MANA_COLOR: Color = Color::Rgba(0.42, 0.41, 0.66, 1.0); widget_ids! { struct Ids { + // Crosshair + crosshair, + // Character Names name_tags[], // Health Bars @@ -330,6 +333,13 @@ impl Hud { let mut health_id_walker = self.ids.health_bars.walk(); let mut health_back_id_walker = self.ids.health_bar_backs.walk(); + // Crosshair + Image::new(self.imgs.crosshair) + .w_h(21.0 * 2.0, 21.0 * 2.0) + .middle_of(ui_widgets.window) + .color(Some(Color::Rgba(1.0, 1.0, 1.0, 1.0))) + .set(self.ids.crosshair, ui_widgets); + // Render Name Tags for (pos, name) in (&entities, &pos, &stats, player.maybe()) .join() From e212d4d6f9084d508cf447394eeced83464f7091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Tue, 2 Jul 2019 22:24:32 +0200 Subject: [PATCH 04/10] Fix comment --- voxygen/src/hud/img_ids.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voxygen/src/hud/img_ids.rs b/voxygen/src/hud/img_ids.rs index 1262a16c86..fd335641ea 100644 --- a/voxygen/src/hud/img_ids.rs +++ b/voxygen/src/hud/img_ids.rs @@ -119,7 +119,7 @@ image_ids! { button_hover: "voxygen/element/buttons/button_hover.vox", button_press: "voxygen/element/buttons/button_press.vox", - // Other + // Crosshair crosshair: "voxygen/element/misc_bg/crosshair.vox", ////////////////////////////////////////////////////////////////////////////////////////////////////// From fbdc1d954a4d467a6d5f2f8562c1aba3e6f5290c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Wed, 3 Jul 2019 19:55:56 +0200 Subject: [PATCH 05/10] Handle block placing. --- voxygen/src/session.rs | 57 ++++++++++++++++++++++++++++++++++------- voxygen/src/settings.rs | 2 ++ voxygen/src/window.rs | 2 ++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 6592f19588..b58dc142d4 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -8,7 +8,7 @@ use crate::{ Direction, Error, GlobalState, PlayState, PlayStateResult, }; use client::{self, Client}; -use common::{clock::Clock, comp, comp::Pos, msg::ClientState}; +use common::{terrain::block::Block, ray::Ray, vol::ReadVol, clock::Clock, comp, comp::Pos, msg::ClientState}; use log::{error, warn}; use std::{cell::RefCell, rc::Rc, time::Duration}; use vek::*; @@ -108,15 +108,54 @@ impl PlayState for SessionState { return PlayStateResult::Shutdown; } Event::InputUpdate(GameInput::Attack, state) => { - if state { - if let ClientState::Character = current_client_state { - self.controller.attack = state; - } else { - self.controller.respawn = state; // TODO: Don't do both + // Check the existence of CanBuild component. If it's here, use LMB to + // place blocks, if not, use it to attack + match self.client.borrow().state().read_component_cloned::(self.client.borrow().entity()) { + Some(_build_perms) => { + println!("Placing block"); + let (view_mat, _, dist) = self.scene.camera().compute_dependents(&self.client.borrow()); + let cam_pos = self.scene.camera().compute_dependents(&self.client.borrow()).2; + let cam_dir = (view_mat.inverted() * Vec4::unit_z()).normalized(); + match self.client + .borrow() + .state() + .terrain() + .ray(cam_pos, cam_pos + cam_dir * 100.0) + .cast() + { + (d, Ok(Some((_)))) => { + let cam_pos = self.scene.camera().compute_dependents(&self.client.borrow()).2; + let cam_dir = (view_mat.inverted() * Vec4::unit_z()).normalized(); + let pos = (cam_pos + cam_dir * d).map(|e| e.floor() as i32); + self.client.borrow_mut().place_block(pos, Block::new(1, Rgb::broadcast(255)));// TODO: Handle block color with a command + }, + (_, Err(_)) => {}, + (_, Ok(None)) => {}, + }; + + }, + None => { + if state { + if let ClientState::Character = current_client_state { + self.controller.attack = state + } else { + self.controller.respawn = state; // TODO: Don't do both + } + } else { + self.controller.attack = state; + self.controller.respawn = state; + } + } + } + } + Event::InputUpdate(GameInput::SecondAttack, state) => { + match self.client.borrow().state().read_component_cloned::(self.client.borrow().entity()) { + Some(_build_perms) => { + + } + None => { + // TODO: Handle secondary attack here } - } else { - self.controller.attack = state; - self.controller.respawn = state; } } Event::InputUpdate(GameInput::Roll, state) => { diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 17f08105cd..251e904453 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -31,6 +31,7 @@ pub struct ControlSettings { pub screenshot: KeyMouse, pub toggle_ingame_ui: KeyMouse, pub attack: KeyMouse, + pub second_attack: KeyMouse, pub roll: KeyMouse, } @@ -60,6 +61,7 @@ impl Default for ControlSettings { screenshot: KeyMouse::Key(VirtualKeyCode::F4), toggle_ingame_ui: KeyMouse::Key(VirtualKeyCode::F6), attack: KeyMouse::Mouse(MouseButton::Left), + second_attack: KeyMouse::Mouse(MouseButton::Right), roll: KeyMouse::Mouse(MouseButton::Middle), } } diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index 7259b0b72f..2a6673bcc3 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -34,6 +34,7 @@ pub enum GameInput { Screenshot, ToggleIngameUi, Attack, + SecondAttack, Roll, Respawn, } @@ -138,6 +139,7 @@ impl Window { GameInput::ToggleIngameUi, ); key_map.insert(settings.controls.attack, GameInput::Attack); + key_map.insert(settings.controls.second_attack, GameInput::SecondAttack); key_map.insert(settings.controls.roll, GameInput::Roll); let keypress_map = HashMap::new(); From a702f7258a8b033c21adc20fae9ebd7a02d8e45c Mon Sep 17 00:00:00 2001 From: timokoesters Date: Wed, 3 Jul 2019 21:25:26 +0200 Subject: [PATCH 06/10] Fix building --- common/src/volumes/dyna.rs | 2 +- common/src/volumes/vol_map_2d.rs | 2 +- voxygen/src/session.rs | 62 +++++++++++++++++--------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/common/src/volumes/dyna.rs b/common/src/volumes/dyna.rs index 4e7f4982d1..e88ccd3de7 100644 --- a/common/src/volumes/dyna.rs +++ b/common/src/volumes/dyna.rs @@ -2,7 +2,7 @@ use crate::vol::{BaseVol, ReadVol, SizedVol, Vox, WriteVol}; use serde_derive::{Deserialize, Serialize}; use vek::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum DynaErr { OutOfBounds, } diff --git a/common/src/volumes/vol_map_2d.rs b/common/src/volumes/vol_map_2d.rs index c77b98d0ea..28b29fdcbb 100644 --- a/common/src/volumes/vol_map_2d.rs +++ b/common/src/volumes/vol_map_2d.rs @@ -6,7 +6,7 @@ use fxhash::FxHashMap; use std::{collections::hash_map, fmt::Debug, marker::PhantomData, sync::Arc}; use vek::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum VolMap2dErr { NoSuchChunk, ChunkErr(V::Err), diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index b58dc142d4..289d39e30f 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -8,7 +8,9 @@ use crate::{ Direction, Error, GlobalState, PlayState, PlayStateResult, }; use client::{self, Client}; -use common::{terrain::block::Block, ray::Ray, vol::ReadVol, clock::Clock, comp, comp::Pos, msg::ClientState}; +use common::{ + clock::Clock, comp, comp::Pos, msg::ClientState, ray::Ray, terrain::block::Block, vol::ReadVol, +}; use log::{error, warn}; use std::{cell::RefCell, rc::Rc, time::Duration}; use vek::*; @@ -110,30 +112,31 @@ impl PlayState for SessionState { Event::InputUpdate(GameInput::Attack, state) => { // Check the existence of CanBuild component. If it's here, use LMB to // place blocks, if not, use it to attack - match self.client.borrow().state().read_component_cloned::(self.client.borrow().entity()) { - Some(_build_perms) => { - println!("Placing block"); - let (view_mat, _, dist) = self.scene.camera().compute_dependents(&self.client.borrow()); - let cam_pos = self.scene.camera().compute_dependents(&self.client.borrow()).2; - let cam_dir = (view_mat.inverted() * Vec4::unit_z()).normalized(); - match self.client - .borrow() - .state() - .terrain() - .ray(cam_pos, cam_pos + cam_dir * 100.0) - .cast() - { - (d, Ok(Some((_)))) => { - let cam_pos = self.scene.camera().compute_dependents(&self.client.borrow()).2; - let cam_dir = (view_mat.inverted() * Vec4::unit_z()).normalized(); - let pos = (cam_pos + cam_dir * d).map(|e| e.floor() as i32); - self.client.borrow_mut().place_block(pos, Block::new(1, Rgb::broadcast(255)));// TODO: Handle block color with a command - }, - (_, Err(_)) => {}, - (_, Ok(None)) => {}, - }; + let mut client = self.client.borrow_mut(); + match client + .state() + .read_component_cloned::(client.entity()) + { + Some(_build_perms) => { + println!("Placing block"); - }, + let (view_mat, _, cam_pos) = + self.scene.camera().compute_dependents(&client); + let cam_dir = + (self.scene.camera().get_focus_pos() - cam_pos).normalized(); + + let (d, b) = { + let terrain = client.state().terrain(); + let ray = + terrain.ray(cam_pos, cam_pos + cam_dir * 100.0).cast(); + (ray.0, if let Ok(Some(_)) = ray.1 { true } else { false }) + }; + + if b { + let pos = (cam_pos + cam_dir * d).map(|e| e.floor() as i32); + client.place_block(pos, Block::new(1, Rgb::broadcast(255))); // TODO: Handle block color with a command + } + } None => { if state { if let ClientState::Character = current_client_state { @@ -149,10 +152,13 @@ impl PlayState for SessionState { } } Event::InputUpdate(GameInput::SecondAttack, state) => { - match self.client.borrow().state().read_component_cloned::(self.client.borrow().entity()) { - Some(_build_perms) => { - - } + match self + .client + .borrow() + .state() + .read_component_cloned::(self.client.borrow().entity()) + { + Some(_build_perms) => {} None => { // TODO: Handle secondary attack here } From 42a4cc8095dad1cb50653e266fea15e07617e6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Wed, 3 Jul 2019 21:56:54 +0200 Subject: [PATCH 07/10] Add block breaking --- server/src/cmd.rs | 54 +----------------------------------------- voxygen/src/session.rs | 39 ++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 63 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 557858aad1..515a6982f3 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -7,9 +7,7 @@ use common::{ comp, msg::ServerMsg, npc::{get_npc_name, NpcKind}, - state::{TerrainChange, TimeOfDay}, - terrain::Block, - vol::Vox, + state::TimeOfDay, }; use specs::{Builder, Entity as EcsEntity, Join}; use vek::*; @@ -106,18 +104,6 @@ lazy_static! { "/players : Show the online players list", handle_players, ), - ChatCommand::new( - "solid", - "{}", - "/solid : Make the blocks around you solid", - handle_solid, - ), - ChatCommand::new( - "empty", - "{}", - "/empty : Make the blocks around you empty", - handle_empty, - ), ChatCommand::new( "help", "", "/help: Display this message", handle_help), ChatCommand::new( @@ -365,44 +351,6 @@ fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action } } -fn handle_solid(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { - match server.state.read_component_cloned::(entity) { - Some(current_pos) => { - server.state.ecs().write_resource::().set( - current_pos.0.map(|e| e.floor() as i32), - Block::new(1, Rgb::broadcast(255)), - ); - } - None => server.clients.notify( - entity, - ServerMsg::Chat(String::from("You have no position!")), - ), - } -} - -fn handle_empty(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { - match server.state.read_component_cloned::(entity) { - Some(current_pos) => { - let mut terrain_change = server.state.ecs().write_resource::(); - - for i in -1..2 { - for j in -1..2 { - for k in -2..1 { - terrain_change.set( - current_pos.0.map(|e| e.floor() as i32) + Vec3::new(i, j, k), - Block::empty(), - ); - } - } - } - } - None => server.clients.notify( - entity, - ServerMsg::Chat(String::from("You have no position!")), - ), - } -} - fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { match server.state.read_component_cloned::(entity) { Some(_build_perms) => { diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 289d39e30f..afa52093d0 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -118,8 +118,6 @@ impl PlayState for SessionState { .read_component_cloned::(client.entity()) { Some(_build_perms) => { - println!("Placing block"); - let (view_mat, _, cam_pos) = self.scene.camera().compute_dependents(&client); let cam_dir = @@ -132,9 +130,12 @@ impl PlayState for SessionState { (ray.0, if let Ok(Some(_)) = ray.1 { true } else { false }) }; - if b { - let pos = (cam_pos + cam_dir * d).map(|e| e.floor() as i32); - client.place_block(pos, Block::new(1, Rgb::broadcast(255))); // TODO: Handle block color with a command + if state { + if b { + let pos = (cam_pos + cam_dir * (d - 0.01)) + .map(|e| e.floor() as i32); + client.place_block(pos, Block::new(1, Rgb::broadcast(255))); // TODO: Handle block color with a command + } } } None => { @@ -152,13 +153,31 @@ impl PlayState for SessionState { } } Event::InputUpdate(GameInput::SecondAttack, state) => { - match self - .client - .borrow() + let mut client = self.client.borrow_mut(); + match client .state() - .read_component_cloned::(self.client.borrow().entity()) + .read_component_cloned::(client.entity()) { - Some(_build_perms) => {} + Some(_build_perms) => { + let (view_mat, _, cam_pos) = + self.scene.camera().compute_dependents(&client); + let cam_dir = + (self.scene.camera().get_focus_pos() - cam_pos).normalized(); + + let (d, b) = { + let terrain = client.state().terrain(); + let ray = + terrain.ray(cam_pos, cam_pos + cam_dir * 100.0).cast(); + (ray.0, if let Ok(Some(_)) = ray.1 { true } else { false }) + }; + + if state { + if b { + let pos = (cam_pos + cam_dir * d).map(|e| e.floor() as i32); + client.remove_block(pos); // TODO: Handle block color with a command + } + } + } None => { // TODO: Handle secondary attack here } From e79f763226ffb2a65ae10eb00f8ee7f710c00611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Wed, 3 Jul 2019 22:28:13 +0200 Subject: [PATCH 08/10] Small cleanup to block placing and breaking --- voxygen/src/session.rs | 72 +++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index afa52093d0..6e9f3f1fce 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -112,12 +112,16 @@ impl PlayState for SessionState { Event::InputUpdate(GameInput::Attack, state) => { // Check the existence of CanBuild component. If it's here, use LMB to // place blocks, if not, use it to attack - let mut client = self.client.borrow_mut(); - match client - .state() - .read_component_cloned::(client.entity()) - { - Some(_build_perms) => { + if state { + let mut client = self.client.borrow_mut(); + if client + .state() + .read_storage::() + .get(client.entity()) + .is_some() + { + println!("Placing block"); + let (view_mat, _, cam_pos) = self.scene.camera().compute_dependents(&client); let cam_dir = @@ -130,35 +134,34 @@ impl PlayState for SessionState { (ray.0, if let Ok(Some(_)) = ray.1 { true } else { false }) }; - if state { - if b { - let pos = (cam_pos + cam_dir * (d - 0.01)) - .map(|e| e.floor() as i32); - client.place_block(pos, Block::new(1, Rgb::broadcast(255))); // TODO: Handle block color with a command - } + if b { + let pos = + (cam_pos + cam_dir * (d - 0.01)).map(|e| e.floor() as i32); + client.place_block(pos, Block::new(1, Rgb::broadcast(255))); // TODO: Handle block color with a command } - } - None => { - if state { - if let ClientState::Character = current_client_state { - self.controller.attack = state - } else { - self.controller.respawn = state; // TODO: Don't do both - } + } else { + if let ClientState::Character = current_client_state { + self.controller.attack = state } else { - self.controller.attack = state; - self.controller.respawn = state; + self.controller.respawn = state; // TODO: Don't do both } } + } else { + self.controller.attack = state; + self.controller.respawn = state; } } Event::InputUpdate(GameInput::SecondAttack, state) => { - let mut client = self.client.borrow_mut(); - match client - .state() - .read_component_cloned::(client.entity()) - { - Some(_build_perms) => { + if state { + let mut client = self.client.borrow_mut(); + if client + .state() + .read_storage::() + .get(client.entity()) + .is_some() + { + println!("Placing block"); + let (view_mat, _, cam_pos) = self.scene.camera().compute_dependents(&client); let cam_dir = @@ -171,15 +174,12 @@ impl PlayState for SessionState { (ray.0, if let Ok(Some(_)) = ray.1 { true } else { false }) }; - if state { - if b { - let pos = (cam_pos + cam_dir * d).map(|e| e.floor() as i32); - client.remove_block(pos); // TODO: Handle block color with a command - } + if b { + let pos = (cam_pos + cam_dir * d).map(|e| e.floor() as i32); + client.remove_block(pos); } - } - None => { - // TODO: Handle secondary attack here + } else { + // TODO: Handle secondary attack } } } From 04d3c077d0bb3423e09794ce8546028f6278516d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Wed, 3 Jul 2019 22:38:28 +0200 Subject: [PATCH 09/10] Remove printlns --- common/src/state.rs | 2 +- voxygen/src/session.rs | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/common/src/state.rs b/common/src/state.rs index 82ceadeed9..2898d78097 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -46,7 +46,7 @@ pub struct DeltaTime(pub f32); const MAX_DELTA_TIME: f32 = 1.0; pub struct TerrainChange { - pub blocks: HashMap, Block>, + blocks: HashMap, Block>, } impl Default for TerrainChange { diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 6e9f3f1fce..4acac6be1e 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -120,8 +120,6 @@ impl PlayState for SessionState { .get(client.entity()) .is_some() { - println!("Placing block"); - let (view_mat, _, cam_pos) = self.scene.camera().compute_dependents(&client); let cam_dir = @@ -160,8 +158,6 @@ impl PlayState for SessionState { .get(client.entity()) .is_some() { - println!("Placing block"); - let (view_mat, _, cam_pos) = self.scene.camera().compute_dependents(&client); let cam_dir = From 1f381cd99bea8b1772e50a10cbd1080051ff44d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Wed, 3 Jul 2019 22:46:43 +0200 Subject: [PATCH 10/10] Even more cleanups --- server/src/cmd.rs | 48 ++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 515a6982f3..9ed396771b 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -352,29 +352,31 @@ fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action } fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { - match server.state.read_component_cloned::(entity) { - Some(_build_perms) => { - server - .state - .ecs() - .write_storage::() - .remove(entity); - server.clients.notify( - entity, - ServerMsg::Chat(String::from("Toggled off build mode!")), - ); - } - None => { - let _ = server - .state - .ecs() - .write_storage::() - .insert(entity, comp::CanBuild); - server.clients.notify( - entity, - ServerMsg::Chat(String::from("Toggled on build mode!")), - ); - } + if server + .state + .read_storage::() + .get(entity) + .is_some() + { + server + .state + .ecs() + .write_storage::() + .remove(entity); + server.clients.notify( + entity, + ServerMsg::Chat(String::from("Toggled off build mode!")), + ); + } else { + let _ = server + .state + .ecs() + .write_storage::() + .insert(entity, comp::CanBuild); + server.clients.notify( + entity, + ServerMsg::Chat(String::from("Toggled on build mode!")), + ); } }