From 3d14d791cf96bb2a79e6c6c7bcb72fa6a179cb1f Mon Sep 17 00:00:00 2001 From: Forest Date: Sun, 12 May 2019 15:57:39 -0400 Subject: [PATCH] Cleaned code Former-commit-id: fd4ef550ff58f621a129901d142d80266fbbc534 --- common/src/comp/agent.rs | 2 +- common/src/sys/agent.rs | 19 ++++------ server/src/cmd.rs | 24 +++++++----- server/src/lib.rs | 9 ++--- voxygen/src/menu/char_selection/mod.rs | 10 +++-- voxygen/src/menu/char_selection/scene.rs | 48 ++++++++---------------- voxygen/src/scene/figure.rs | 7 +++- voxygen/src/scene/mod.rs | 10 ++--- 8 files changed, 59 insertions(+), 70 deletions(-) diff --git a/common/src/comp/agent.rs b/common/src/comp/agent.rs index eafb9b8c68..f3ff742700 100644 --- a/common/src/comp/agent.rs +++ b/common/src/comp/agent.rs @@ -1,4 +1,4 @@ -use specs::{Component, VecStorage, Entity as EcsEntity}; +use specs::{Component, Entity as EcsEntity, VecStorage}; use vek::*; #[derive(Copy, Clone, Debug)] diff --git a/common/src/sys/agent.rs b/common/src/sys/agent.rs index f21e41904d..d13d6cc3df 100644 --- a/common/src/sys/agent.rs +++ b/common/src/sys/agent.rs @@ -19,17 +19,15 @@ impl<'a> System<'a> for Sys { for (mut agent, pos, mut control) in (&mut agents, &positions, &mut controls).join() { match agent { Agent::Wanderer(bearing) => { - *bearing += Vec2::new( - rand::random::() - 0.5, - rand::random::() - 0.5, - ) * 0.1 + *bearing += Vec2::new(rand::random::() - 0.5, rand::random::() - 0.5) + * 0.1 - *bearing * 0.01 - pos.0 * 0.0002; if bearing.magnitude_squared() != 0.0 { control.move_dir = bearing.normalized(); } - }, + } Agent::Pet { target, offset } => { // Run towards target match positions.get(*target) { @@ -48,18 +46,17 @@ impl<'a> System<'a> for Sys { } else { Vec2::zero() }; - }, + } _ => control.move_dir = Vec2::zero(), } // Change offset occasionally if rand::random::() < 0.003 { - *offset = Vec2::new( - rand::random::() - 0.5, - rand::random::() - 0.5, - ) * 10.0; + *offset = + Vec2::new(rand::random::() - 0.5, rand::random::() - 0.5) + * 10.0; } - }, + } } } } diff --git a/server/src/cmd.rs b/server/src/cmd.rs index c2b2642a4b..e585165d9b 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -4,11 +4,7 @@ use crate::Server; use common::{comp, msg::ServerMsg}; -use specs::{ - Join, - Entity as EcsEntity, - Builder, -}; +use specs::{Builder, Entity as EcsEntity, Join}; use vek::*; use lazy_static::lazy_static; @@ -198,15 +194,23 @@ fn handle_pet(server: &mut Server, entity: EcsEntity, args: String, action: &Cha let mut current = entity; for _ in 0..1 { - current = server.create_npc(comp::Character::random()) + current = server + .create_npc(comp::Character::random()) .with(comp::Control::default()) - .with(comp::Agent::Pet{ target: current, offset: Vec2::zero() }) + .with(comp::Agent::Pet { + target: current, + offset: Vec2::zero(), + }) .with(pos) .build(); } - server.clients.notify(entity, ServerMsg::Chat("Pet spawned!".to_owned())); - }, - None => server.clients.notify(entity, ServerMsg::Chat("You have no position!".to_owned())), + server + .clients + .notify(entity, ServerMsg::Chat("Pet spawned!".to_owned())); + } + None => server + .clients + .notify(entity, ServerMsg::Chat("You have no position!".to_owned())), } } diff --git a/server/src/lib.rs b/server/src/lib.rs index fe6a5ed5f1..55c755c26f 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -139,10 +139,7 @@ impl Server { state.write_component(entity, comp::phys::ForceUpdate); // Set initial animation - state.write_component( - entity, - comp::AnimationHistory::new(Animation::Idle), - ); + state.write_component(entity, comp::AnimationHistory::new(Animation::Idle)); // Tell the client his request was successful client.notify(ServerMsg::StateAnswer(Ok(ClientState::Character))); @@ -227,7 +224,9 @@ impl Server { .join() { let chunk_pos = self.state.terrain().pos_key(pos.0.map(|e| e as i32)); - let dist = Vec2::from(chunk_pos - key).map(|e: i32| e.abs()).reduce_max(); + let dist = Vec2::from(chunk_pos - key) + .map(|e: i32| e.abs()) + .reduce_max(); min_dist = min_dist.min(dist); } diff --git a/voxygen/src/menu/char_selection/mod.rs b/voxygen/src/menu/char_selection/mod.rs index b3d2ddb10c..13f430d082 100644 --- a/voxygen/src/menu/char_selection/mod.rs +++ b/voxygen/src/menu/char_selection/mod.rs @@ -1,5 +1,5 @@ -mod ui; mod scene; +mod ui; use crate::{ session::SessionState, @@ -8,9 +8,9 @@ use crate::{ }; use client::{self, Client}; use common::{clock::Clock, msg::ClientMsg}; +use scene::Scene; use std::{cell::RefCell, rc::Rc, time::Duration}; use ui::CharSelectionUi; -use scene::Scene; use vek::*; const FPS: u64 = 60; @@ -87,10 +87,12 @@ impl PlayState for CharSelectionState { } // Maintain the scene - self.scene.maintain(global_state.window.renderer_mut(), &self.client.borrow()); + self.scene + .maintain(global_state.window.renderer_mut(), &self.client.borrow()); // Render the scene - self.scene.render(global_state.window.renderer_mut(), &self.client.borrow()); + self.scene + .render(global_state.window.renderer_mut(), &self.client.borrow()); // Draw the UI to the screen self.char_selection_ui diff --git a/voxygen/src/menu/char_selection/scene.rs b/voxygen/src/menu/char_selection/scene.rs index ec6297ef0e..6c999afa4b 100644 --- a/voxygen/src/menu/char_selection/scene.rs +++ b/voxygen/src/menu/char_selection/scene.rs @@ -1,36 +1,20 @@ -use vek::*; -use client::Client; -use common::{ - comp::Character, - figure::Segment, -}; use crate::{ + anim::{ + character::{CharacterSkeleton, IdleAnimation}, + Animation, Skeleton, + }, render::{ - Renderer, - Consts, - Globals, - Model, - SkyboxLocals, - SkyboxPipeline, - create_skybox_mesh, - PostProcessLocals, - PostProcessPipeline, - create_pp_mesh, - FigurePipeline, + create_pp_mesh, create_skybox_mesh, Consts, FigurePipeline, Globals, Model, + PostProcessLocals, PostProcessPipeline, Renderer, SkyboxLocals, SkyboxPipeline, }, scene::{ camera::Camera, figure::{FigureModelCache, FigureState}, }, - anim::{ - Skeleton, - Animation, - character::{ - CharacterSkeleton, - IdleAnimation, - }, - }, }; +use client::Client; +use common::{comp::Character, figure::Segment}; +use vek::*; struct Skybox { model: Model, @@ -76,7 +60,9 @@ impl Scene { figure_model_cache: FigureModelCache::new(), figure_state: FigureState::new(renderer, CharacterSkeleton::new()), - backdrop_model: renderer.create_model(&FigureModelCache::load_mesh("knight.vox", Vec3::zero())).unwrap(), + backdrop_model: renderer + .create_model(&FigureModelCache::load_mesh("knight.vox", Vec3::zero())) + .unwrap(), backdrop_state: FigureState::new(renderer, CharacterSkeleton::new()), } } @@ -85,11 +71,8 @@ impl Scene { self.camera.set_focus_pos(Vec3::unit_z() * 1.75); self.camera.update(client.state().get_time()); self.camera.set_distance(4.0); - self.camera.set_orientation(Vec3::new( - client.state().get_time() as f32 * 0.2, - 0.3, - 0.0, - )); + self.camera + .set_orientation(Vec3::new(client.state().get_time() as f32 * 0.2, 0.3, 0.0)); let (view_mat, proj_mat, cam_pos) = self.camera.compute_dependents(client); @@ -116,7 +99,8 @@ impl Scene { ); self.figure_state.skeleton_mut().interpolate(&tgt_skeleton); - self.figure_state.update(renderer, Vec3::zero(), -Vec3::unit_y()); + self.figure_state + .update(renderer, Vec3::zero(), -Vec3::unit_y()); } pub fn render(&mut self, renderer: &mut Renderer, client: &Client) { diff --git a/voxygen/src/scene/figure.rs b/voxygen/src/scene/figure.rs index f6c4455a23..dae5f79f57 100644 --- a/voxygen/src/scene/figure.rs +++ b/voxygen/src/scene/figure.rs @@ -246,7 +246,8 @@ impl FigureMgr { state.update(renderer, pos.0, dir.0); } - self.states.retain(|entity, _| ecs.entities().is_alive(*entity)); + self.states + .retain(|entity, _| ecs.entities().is_alive(*entity)); } pub fn render( @@ -261,7 +262,9 @@ impl FigureMgr { for (entity, &character) in (&ecs.entities(), &ecs.read_storage::()).join() { if let Some(state) = self.states.get(&entity) { - let model = self.model_cache.get_or_create_model(renderer, character, tick); + let model = self + .model_cache + .get_or_create_model(renderer, character, tick); renderer.render_figure(model, globals, &state.locals(), state.bone_consts()); } } diff --git a/voxygen/src/scene/mod.rs b/voxygen/src/scene/mod.rs index b276425701..806c0d3df2 100644 --- a/voxygen/src/scene/mod.rs +++ b/voxygen/src/scene/mod.rs @@ -2,10 +2,7 @@ pub mod camera; pub mod figure; pub mod terrain; -use dot_vox; -use vek::*; -use common::{comp, figure::Segment}; -use client::Client; +use self::{camera::Camera, figure::FigureMgr, terrain::Terrain}; use crate::{ anim::{ character::{CharacterSkeleton, RunAnimation}, @@ -18,7 +15,10 @@ use crate::{ }, window::Event, }; -use self::{camera::Camera, figure::FigureMgr, terrain::Terrain}; +use client::Client; +use common::{comp, figure::Segment}; +use dot_vox; +use vek::*; // TODO: Don't hard-code this const CURSOR_PAN_SCALE: f32 = 0.005;