Optimized uses of emitters, cleanup

This commit is contained in:
Imbris 2020-03-22 00:49:32 -04:00
parent eafd761d5a
commit 41c424ac13
16 changed files with 36 additions and 38 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -192,7 +192,7 @@ Enjoy your stay in the World of Veloren."#,
// Inventory
"hud.bag.inventory": "'s Inventory",
"hud.bag.inventory": "'s Inventory",
"hud.bag.stats": "'s Stats",
"hud.bag.exp": "Exp",
// Settings

View File

@ -692,8 +692,7 @@ impl Client {
self.state
.ecs()
.read_resource::<EventBus<SfxEventItem>>()
.emitter()
.emit(SfxEventItem::at_player_position(SfxEvent::Inventory(event)));
.emit_now(SfxEventItem::at_player_position(SfxEvent::Inventory(event)));
},
ServerMsg::TerrainChunkUpdate { key, chunk } => {
if let Ok(chunk) = chunk {

View File

@ -3,7 +3,7 @@ use crate::{
states::*,
sys::character_behavior::JoinData,
};
use specs::{Component, FlaggedStorage, HashMapStorage};
use specs::{Component, FlaggedStorage};
use specs_idvs::IDVStorage;
use std::time::Duration;

View File

@ -10,7 +10,7 @@ use crate::{
use rand::seq::SliceRandom;
use specs::{Component, FlaggedStorage};
use specs_idvs::IDVStorage;
use std::{fs::File, io::BufReader, time::Duration, vec::Vec};
use std::{fs::File, io::BufReader, time::Duration};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SwordKind {

View File

@ -144,7 +144,7 @@ impl<E> EventBus<E> {
}
}
pub fn emit(&self, event: E) { self.queue.lock().push_front(event); }
pub fn emit_now(&self, event: E) { self.queue.lock().push_back(event); }
pub fn recv_all(&self) -> impl ExactSizeIterator<Item = E> {
std::mem::replace(self.queue.lock().deref_mut(), VecDeque::new()).into_iter()
@ -157,7 +157,7 @@ pub struct Emitter<'a, E> {
}
impl<'a, E> Emitter<'a, E> {
pub fn emit(&mut self, event: E) { self.events.push_front(event); }
pub fn emit(&mut self, event: E) { self.events.push_back(event); }
pub fn append(&mut self, other: &mut VecDeque<E>) { self.events.append(other) }
}

View File

@ -21,9 +21,7 @@ sum_type! {
Mass(comp::Mass),
Gravity(comp::Gravity),
Sticky(comp::Sticky),
CharacterAbility(comp::CharacterAbility),
Loadout(comp::Loadout),
Attacking(comp::Attacking),
CharacterState(comp::CharacterState),
Pos(comp::Pos),
Vel(comp::Vel),
@ -48,9 +46,7 @@ sum_type! {
Mass(PhantomData<comp::Mass>),
Gravity(PhantomData<comp::Gravity>),
Sticky(PhantomData<comp::Sticky>),
CharacterAbility(PhantomData<comp::CharacterAbility>),
Loadout(PhantomData<comp::Loadout>),
Attacking(PhantomData<comp::Attacking>),
CharacterState(PhantomData<comp::CharacterState>),
Pos(PhantomData<comp::Pos>),
Vel(PhantomData<comp::Vel>),
@ -75,9 +71,7 @@ impl sync::CompPacket for EcsCompPacket {
EcsCompPacket::Mass(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Gravity(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Sticky(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::CharacterAbility(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Loadout(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Attacking(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::CharacterState(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Pos(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Vel(comp) => sync::handle_insert(comp, entity, world),
@ -100,9 +94,7 @@ impl sync::CompPacket for EcsCompPacket {
EcsCompPacket::Mass(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Gravity(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Sticky(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::CharacterAbility(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Loadout(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Attacking(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::CharacterState(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Pos(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Vel(comp) => sync::handle_modify(comp, entity, world),
@ -127,11 +119,7 @@ impl sync::CompPacket for EcsCompPacket {
EcsCompPhantom::Mass(_) => sync::handle_remove::<comp::Mass>(entity, world),
EcsCompPhantom::Gravity(_) => sync::handle_remove::<comp::Gravity>(entity, world),
EcsCompPhantom::Sticky(_) => sync::handle_remove::<comp::Sticky>(entity, world),
EcsCompPhantom::CharacterAbility(_) => {
sync::handle_remove::<comp::CharacterAbility>(entity, world)
},
EcsCompPhantom::Loadout(_) => sync::handle_remove::<comp::Loadout>(entity, world),
EcsCompPhantom::Attacking(_) => sync::handle_remove::<comp::Attacking>(entity, world),
EcsCompPhantom::CharacterState(_) => {
sync::handle_remove::<comp::CharacterState>(entity, world)
},

View File

@ -107,7 +107,6 @@ impl State {
ecs.register_sync_marker();
// Register server -> all clients synced components.
ecs.register::<comp::Loadout>();
ecs.register::<comp::Projectile>();
ecs.register::<comp::Body>();
ecs.register::<comp::Player>();
ecs.register::<comp::Stats>();
@ -121,7 +120,6 @@ impl State {
ecs.register::<comp::Mass>();
ecs.register::<comp::Sticky>();
ecs.register::<comp::Gravity>();
ecs.register::<comp::Attacking>();
// Register components send from clients -> server
ecs.register::<comp::Controller>();
@ -150,6 +148,7 @@ impl State {
ecs.register::<comp::Admin>();
ecs.register::<comp::Waypoint>();
ecs.register::<comp::Projectile>();
ecs.register::<comp::Attacking>();
// Register synced resources used by the ECS.
ecs.insert(TimeOfDay(0.0));

View File

@ -129,6 +129,9 @@ impl<'a> System<'a> for Sys {
mountings,
): Self::SystemData,
) {
let mut server_emitter = server_bus.emitter();
let mut local_emitter = local_bus.emitter();
let mut join_iter = (
&entities,
&uids,
@ -191,8 +194,8 @@ impl<'a> System<'a> for Sys {
*tuple.5 = state_update.ori;
*tuple.6 = state_update.energy;
*tuple.7 = state_update.loadout;
local_bus.emitter().append(&mut state_update.local_events);
server_bus.emitter().append(&mut state_update.server_events);
local_emitter.append(&mut state_update.local_events);
server_emitter.append(&mut state_update.server_events);
}
}
}

View File

@ -48,6 +48,7 @@ impl<'a> System<'a> for Sys {
character_states,
): Self::SystemData,
) {
let mut server_emitter = server_bus.emitter();
// Attacks
for (entity, uid, pos, ori, scale_maybe, _, _attacker_stats, attack) in (
&entities,
@ -81,7 +82,7 @@ impl<'a> System<'a> for Sys {
{
// 2D versions
let pos2 = Vec2::from(pos.0);
let pos_b2: Vec2<f32> = Vec2::from(pos_b.0);
let pos_b2 = Vec2::<f32>::from(pos_b.0);
let ori2 = Vec2::from(ori.0);
// Scales
@ -106,7 +107,7 @@ impl<'a> System<'a> for Sys {
dmg = (dmg as f32 * (1.0 - BLOCK_EFFICIENCY)) as u32
}
server_bus.emitter().emit(ServerEvent::Damage {
server_emitter.emit(ServerEvent::Damage {
uid: *uid_b,
change: HealthChange {
amount: -(dmg as i32),

View File

@ -836,7 +836,7 @@ fn handle_explosion(server: &mut Server, entity: EcsEntity, args: String, action
.state
.ecs()
.read_resource::<EventBus<ServerEvent>>()
.emit(ServerEvent::Explosion { pos: pos.0, radius }),
.emit_now(ServerEvent::Explosion { pos: pos.0, radius }),
None => server.notify_client(
entity,
ServerMsg::private(String::from("You have no position!")),

View File

@ -45,7 +45,7 @@ impl<'a> System<'a> for Sys {
&mut self,
(
entities,
server_emitter,
server_event_bus,
time,
terrain,
mut timer,
@ -68,6 +68,8 @@ impl<'a> System<'a> for Sys {
let time = time.0;
let mut server_emitter = server_event_bus.emitter();
let mut new_chat_msgs = Vec::new();
// Player list to send new players.

View File

@ -38,7 +38,7 @@ impl<'a> System<'a> for Sys {
fn run(
&mut self,
(
server_emitter,
server_event_bus,
tick,
mut timer,
mut chunk_generator,
@ -51,6 +51,8 @@ impl<'a> System<'a> for Sys {
) {
timer.start();
let mut server_emitter = server_event_bus.emitter();
// Fetch any generated `TerrainChunk`s and insert them into the terrain.
// Also, send the chunk data to anybody that is close by.
'insert_terrain_chunks: while let Some((key, res)) = chunk_generator.recv_new_chunk() {

View File

@ -47,6 +47,9 @@ impl MovementEventMapper {
const SFX_DIST_LIMIT_SQR: f32 = 20000.0;
let ecs = state.ecs();
let sfx_event_bus = ecs.read_resource::<EventBus<SfxEventItem>>();
let mut sfx_emitter = sfx_event_bus.emitter();
let player_position = ecs
.read_storage::<Pos>()
.get(player_entity)
@ -86,13 +89,11 @@ impl MovementEventMapper {
// Check for SFX config entry for this movement
if Self::should_emit(state, triggers.get_key_value(&mapped_event)) {
ecs.read_resource::<EventBus<SfxEventItem>>()
.emitter()
.emit(SfxEventItem::new(
mapped_event,
Some(pos.0),
Some(Self::get_volume_for_body_type(body)),
));
sfx_emitter.emit(SfxEventItem::new(
mapped_event,
Some(pos.0),
Some(Self::get_volume_for_body_type(body)),
));
// Set the new previous entity state
state.event = mapped_event;

View File

@ -53,8 +53,7 @@ impl ProgressionEventMapper {
if sfx_trigger_item.is_some() {
ecs.read_resource::<EventBus<SfxEventItem>>()
.emitter()
.emit(SfxEventItem::at_player_position(mapped_event));
.emit_now(SfxEventItem::at_player_position(mapped_event));
}
}

View File

@ -35,6 +35,10 @@ const LIGHT_DIST_RADIUS: f32 = 64.0; // The distance beyond which lights may not
const SHADOW_DIST_RADIUS: f32 = 8.0;
const SHADOW_MAX_DIST: f32 = 96.0; // The distance beyond which shadows may not be visible
/// Above this speed is considered running
/// Used for first person camera effects
const RUNNING_THRESHOLD: f32 = 0.7;
struct Skybox {
model: Model<SkyboxPipeline>,
locals: Consts<SkyboxLocals>,
@ -191,7 +195,7 @@ impl Scene {
let is_running = ecs
.read_storage::<comp::Vel>()
.get(scene_data.player_entity)
.map(|v| v.0.magnitude_squared() > 0.5);
.map(|v| v.0.magnitude_squared() > RUNNING_THRESHOLD.powi(2));
let on_ground = ecs
.read_storage::<comp::PhysicsState>()