Updated sphynx

Former-commit-id: f81484571bda8c5e15ea2b6c0e8b9b6892b094a3
This commit is contained in:
Joshua Barretto 2019-05-17 00:05:46 +01:00
parent 6f984ca181
commit db4be8e492
6 changed files with 198 additions and 213 deletions

347
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,20 @@
use crate::comp;
use crate::{comp, state};
use serde_derive::{Deserialize, Serialize};
use std::marker::PhantomData;
// Automatically derive From<T> for Packet for each variant Packet::T(T)
// Automatically derive From<T> for EcsResPacket for each variant EcsResPacket::T(T)
sphynx::sum_type! {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EcsPacket {
pub enum EcsResPacket {
Time(state::Time),
TimeOfDay(state::TimeOfDay),
}
}
impl sphynx::ResPacket for EcsResPacket {}
// Automatically derive From<T> for EcsCompPacket for each variant EcsCompPacket::T(T)
sphynx::sum_type! {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EcsCompPacket {
Pos(comp::phys::Pos),
Vel(comp::phys::Vel),
Dir(comp::phys::Dir),
@ -14,10 +23,10 @@ sphynx::sum_type! {
Stats(comp::Stats),
}
}
// Automatically derive From<T> for Phantom for each variant Phantom::T(PhantomData<T>)
// Automatically derive From<T> for EcsCompPhantom for each variant EcsCompPhantom::T(PhantomData<T>)
sphynx::sum_type! {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EcsPhantom {
pub enum EcsCompPhantom {
Pos(PhantomData<comp::phys::Pos>),
Vel(PhantomData<comp::phys::Vel>),
Dir(PhantomData<comp::phys::Dir>),
@ -26,6 +35,6 @@ sphynx::sum_type! {
Stats(PhantomData<comp::Stats>),
}
}
impl sphynx::Packet for EcsPacket {
type Phantom = EcsPhantom;
impl sphynx::CompPacket for EcsCompPacket {
type Phantom = EcsCompPhantom;
}

View File

@ -4,7 +4,7 @@ pub mod server;
// Reexports
pub use self::client::ClientMsg;
pub use self::ecs_packet::EcsPacket;
pub use self::ecs_packet::{EcsCompPacket, EcsResPacket};
pub use self::server::{RequestStateError, ServerMsg};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]

View File

@ -1,4 +1,4 @@
use super::{ClientState, EcsPacket};
use super::{ClientState, EcsCompPacket, EcsResPacket};
use crate::{comp, terrain::TerrainChunk};
use vek::*;
@ -13,7 +13,7 @@ pub enum RequestStateError {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ServerMsg {
InitialSync {
ecs_state: sphynx::StatePackage<EcsPacket>,
ecs_state: sphynx::StatePackage<EcsCompPacket, EcsResPacket>,
entity_uid: u64,
},
StateAnswer(Result<ClientState, (RequestStateError, ClientState)>),
@ -22,7 +22,7 @@ pub enum ServerMsg {
Pong,
Chat(String),
SetPlayerEntity(u64),
EcsSync(sphynx::SyncPackage<EcsPacket>),
EcsSync(sphynx::SyncPackage<EcsCompPacket, EcsResPacket>),
EntityPhysics {
entity: u64,
pos: comp::phys::Pos,

View File

@ -3,11 +3,12 @@ pub use sphynx::Uid;
use crate::{
comp,
msg::EcsPacket,
msg::{EcsCompPacket, EcsResPacket},
sys,
terrain::{TerrainChunk, TerrainMap},
};
use rayon::{ThreadPool, ThreadPoolBuilder};
use serde_derive::{Deserialize, Serialize};
use specs::{
saveload::{MarkedBuilder, MarkerAllocator},
shred::{Fetch, FetchMut},
@ -23,10 +24,12 @@ use vek::*;
const DAY_CYCLE_FACTOR: f64 = 24.0 * 60.0;
/// A resource to store the time of day
struct TimeOfDay(f64);
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TimeOfDay(f64);
/// A resource to store the tick (i.e: physics) time
struct Time(f64);
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Time(f64);
/// A resource used to store the time since the last tick
#[derive(Default)]
@ -63,7 +66,7 @@ impl Changes {
/// A type used to represent game state stored on both the client and the server. This includes
/// things like entity components, terrain data, and global state like weather, time of day, etc.
pub struct State {
ecs: sphynx::World<EcsPacket>,
ecs: sphynx::World<EcsCompPacket, EcsResPacket>,
// Avoid lifetime annotation by storing a thread pool instead of the whole dispatcher
thread_pool: Arc<ThreadPool>,
changes: Changes,
@ -80,7 +83,9 @@ impl State {
}
/// Create a new `State` from an ECS state package
pub fn from_state_package(state_package: sphynx::StatePackage<EcsPacket>) -> Self {
pub fn from_state_package(
state_package: sphynx::StatePackage<EcsCompPacket, EcsResPacket>,
) -> Self {
Self {
ecs: sphynx::World::from_state_package(
specs::World::new(),
@ -93,7 +98,7 @@ impl State {
}
// Create a new Sphynx ECS world
fn setup_sphynx_world(ecs: &mut sphynx::World<EcsPacket>) {
fn setup_sphynx_world(ecs: &mut sphynx::World<EcsCompPacket, EcsResPacket>) {
// Register synced components
ecs.register_synced::<comp::Actor>();
ecs.register_synced::<comp::Player>();
@ -107,9 +112,11 @@ impl State {
ecs.register::<comp::Agent>();
ecs.register::<comp::Control>();
// Register resources used by the ECS
ecs.add_resource(TimeOfDay(0.0));
ecs.add_resource(Time(0.0));
// Register synced resources used by the ECS
ecs.add_resource_synced(TimeOfDay(0.0));
ecs.add_resource_synced(Time(0.0));
// Register unsynced resources used by the ECS
ecs.add_resource(DeltaTime(0.0));
ecs.add_resource(TerrainMap::new().unwrap());
}
@ -139,12 +146,12 @@ impl State {
}
/// Get a reference to the internal ECS world
pub fn ecs(&self) -> &sphynx::World<EcsPacket> {
pub fn ecs(&self) -> &sphynx::World<EcsCompPacket, EcsResPacket> {
&self.ecs
}
/// Get a mutable reference to the internal ECS world
pub fn ecs_mut(&mut self) -> &mut sphynx::World<EcsPacket> {
pub fn ecs_mut(&mut self) -> &mut sphynx::World<EcsCompPacket, EcsResPacket> {
&mut self.ecs
}

View File

@ -399,9 +399,7 @@ impl FigureMgr {
// Change in health as color!
let col = stats
.and_then(|stats| stats.hp.last_change)
.map(|(change_by, change_time)| {
Rgba::new(1.0, 0.7, 0.7, 1.0)
})
.map(|(change_by, change_time)| Rgba::new(1.0, 0.7, 0.7, 1.0))
.unwrap_or(Rgba::broadcast(1.0));
state.update(renderer, pos.0, dir.0, col);