Cleaned code

Former-commit-id: fd4ef550ff58f621a129901d142d80266fbbc534
This commit is contained in:
Forest 2019-05-12 15:57:39 -04:00
parent 49cdd95c84
commit 3d14d791cf
8 changed files with 59 additions and 70 deletions

View File

@ -1,4 +1,4 @@
use specs::{Component, VecStorage, Entity as EcsEntity};
use specs::{Component, Entity as EcsEntity, VecStorage};
use vek::*;
#[derive(Copy, Clone, Debug)]

View File

@ -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::<f32>() - 0.5,
rand::random::<f32>() - 0.5,
) * 0.1
*bearing += Vec2::new(rand::random::<f32>() - 0.5, rand::random::<f32>() - 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::<f32>() < 0.003 {
*offset = Vec2::new(
rand::random::<f32>() - 0.5,
rand::random::<f32>() - 0.5,
) * 10.0;
*offset =
Vec2::new(rand::random::<f32>() - 0.5, rand::random::<f32>() - 0.5)
* 10.0;
}
},
}
}
}
}

View File

@ -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())),
}
}

View File

@ -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);
}

View File

@ -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

View File

@ -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<SkyboxPipeline>,
@ -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) {

View File

@ -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::<comp::Character>()).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());
}
}

View File

@ -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;