Remove .internal and .internal_mut

Former-commit-id: 261b656ec400f6780f2be2f5a4c331cc3b2a64a2
This commit is contained in:
timokoesters 2019-04-22 10:20:25 +02:00
parent 0dca245a53
commit 91057edfac
5 changed files with 41 additions and 46 deletions

View File

@ -97,41 +97,41 @@ impl State {
ecs.register_synced::<comp::Player>();
// Register unsynched (or synced by other means) components
ecs.internal_mut().register::<comp::phys::Pos>();
ecs.internal_mut().register::<comp::phys::Vel>();
ecs.internal_mut().register::<comp::phys::Dir>();
ecs.internal_mut().register::<comp::AnimationHistory>();
ecs.internal_mut().register::<comp::Agent>();
ecs.internal_mut().register::<comp::Control>();
ecs.register::<comp::phys::Pos>();
ecs.register::<comp::phys::Vel>();
ecs.register::<comp::phys::Dir>();
ecs.register::<comp::AnimationHistory>();
ecs.register::<comp::Agent>();
ecs.register::<comp::Control>();
// Register resources used by the ECS
ecs.internal_mut().add_resource(TimeOfDay(0.0));
ecs.internal_mut().add_resource(Time(0.0));
ecs.internal_mut().add_resource(DeltaTime(0.0));
ecs.internal_mut().add_resource(TerrainMap::new());
ecs.add_resource(TimeOfDay(0.0));
ecs.add_resource(Time(0.0));
ecs.add_resource(DeltaTime(0.0));
ecs.add_resource(TerrainMap::new());
}
/// Register a component with the state's ECS
pub fn with_component<T: Component>(mut self) -> Self
where <T as Component>::Storage: Default
{
self.ecs.internal_mut().register::<T>();
self.ecs.register::<T>();
self
}
/// Write a component attributed to a particular entity
pub fn write_component<C: Component>(&mut self, entity: EcsEntity, comp: C) {
let _ = self.ecs.internal_mut().write_storage().insert(entity, comp);
let _ = self.ecs.write_storage().insert(entity, comp);
}
/// Read a component attributed to a particular entity
pub fn read_component_cloned<C: Component + Clone>(&self, entity: EcsEntity) -> Option<C> {
self.ecs.internal().read_storage().get(entity).cloned()
self.ecs.read_storage().get(entity).cloned()
}
/// Get a read-only reference to the storage of a particular component type
pub fn read_storage<C: Component>(&self) -> EcsStorage<C, Fetch<EcsMaskedStorage<C>>> {
self.ecs.internal().read_storage::<C>()
self.ecs.read_storage::<C>()
}
/// Get a reference to the internal ECS world
@ -154,27 +154,25 @@ impl State {
///
/// Note that this should not be used for physics, animations or other such localised timings.
pub fn get_time_of_day(&self) -> f64 {
self.ecs.internal().read_resource::<TimeOfDay>().0
self.ecs.read_resource::<TimeOfDay>().0
}
/// Get the current in-game time.
///
/// Note that this does not correspond to the time of day.
pub fn get_time(&self) -> f64 {
self.ecs.internal().read_resource::<Time>().0
self.ecs.read_resource::<Time>().0
}
/// Get a reference to this state's terrain.
pub fn terrain(&self) -> Fetch<TerrainMap> {
self.ecs
.internal()
.read_resource::<TerrainMap>()
}
/// Insert the provided chunk into this state's terrain.
pub fn insert_chunk(&mut self, key: Vec3<i32>, chunk: TerrainChunk) {
if self.ecs
.internal_mut()
.write_resource::<TerrainMap>()
.insert(key, chunk)
.is_some()
@ -188,19 +186,19 @@ impl State {
/// Execute a single tick, simulating the game state by the given duration.
pub fn tick(&mut self, dt: Duration) {
// Change the time accordingly
self.ecs.internal_mut().write_resource::<TimeOfDay>().0 += dt.as_secs_f64() * DAY_CYCLE_FACTOR;
self.ecs.internal_mut().write_resource::<Time>().0 += dt.as_secs_f64();
self.ecs.write_resource::<TimeOfDay>().0 += dt.as_secs_f64() * DAY_CYCLE_FACTOR;
self.ecs.write_resource::<Time>().0 += dt.as_secs_f64();
// Run systems to update the world
self.ecs.internal_mut().write_resource::<DeltaTime>().0 = dt.as_secs_f64();
self.ecs.write_resource::<DeltaTime>().0 = dt.as_secs_f64();
// Create and run dispatcher for ecs systems
let mut dispatch_builder = DispatcherBuilder::new();
sys::add_local_systems(&mut dispatch_builder);
// This dispatches all the systems in parallel
dispatch_builder.build().dispatch(&self.ecs.internal_mut().res);
dispatch_builder.build().dispatch(&self.ecs.res);
self.ecs.internal_mut().maintain();
self.ecs.maintain();
}
/// Clean up the state after a tick

View File

@ -133,7 +133,7 @@ fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &Chat
let opt_alias = scan_fmt!(&args, action.arg_fmt, String);
match opt_alias {
Some(alias) => {
let ecs = server.state.ecs().internal();
let ecs = server.state.ecs();
let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::player::Player>())
.join()
.find(|(_, player)| player.alias == alias)

View File

@ -54,7 +54,7 @@ impl Server {
let (chunk_tx, chunk_rx) = mpsc::channel();
let mut state = State::new();
state.ecs_mut().internal_mut().register::<comp::phys::ForceUpdate>();
state.ecs_mut().register::<comp::phys::ForceUpdate>();
let mut this = Self {
state,
@ -154,12 +154,11 @@ impl Server {
for (key, chunk) in self.chunk_rx.try_iter() {
// Send the chunk to all nearby players
for (entity, player, pos) in (
&self.state.ecs().internal().entities(),
&self.state.ecs().internal().read_storage::<comp::Player>(),
&self.state.ecs().entities(),
&self.state.ecs().read_storage::<comp::Player>(),
&self
.state
.ecs()
.internal()
.read_storage::<comp::phys::Pos>(),
)
.join()
@ -293,7 +292,6 @@ impl Server {
match self
.state
.ecs()
.internal()
.read_storage::<comp::Player>()
.get(entity)
{
@ -366,9 +364,9 @@ impl Server {
// Sync logical information other players have authority over, not the server
for (other_entity, &uid, &animation_history) in (
&state.ecs().internal().entities(),
&state.ecs().internal().read_storage::<common::state::Uid>(),
&state.ecs().internal().read_storage::<comp::AnimationHistory>(),
&state.ecs().entities(),
&state.ecs().read_storage::<common::state::Uid>(),
&state.ecs().read_storage::<comp::AnimationHistory>(),
).join() {
// AnimationHistory
client.postbox.send_message(ServerMsg::EntityAnimation {
@ -385,12 +383,12 @@ impl Server {
// Sync 'physical' state
for (entity, &uid, &pos, &vel, &dir, force_update) in (
&self.state.ecs().internal().entities(),
&self.state.ecs().internal().read_storage::<Uid>(),
&self.state.ecs().internal().read_storage::<comp::phys::Pos>(),
&self.state.ecs().internal().read_storage::<comp::phys::Vel>(),
&self.state.ecs().internal().read_storage::<comp::phys::Dir>(),
self.state.ecs().internal().read_storage::<comp::phys::ForceUpdate>().maybe(),
&self.state.ecs().entities(),
&self.state.ecs().read_storage::<Uid>(),
&self.state.ecs().read_storage::<comp::phys::Pos>(),
&self.state.ecs().read_storage::<comp::phys::Vel>(),
&self.state.ecs().read_storage::<comp::phys::Dir>(),
self.state.ecs().read_storage::<comp::phys::ForceUpdate>().maybe(),
).join() {
let msg = ServerMsg::EntityPhysics {
entity: uid.into(),
@ -407,9 +405,9 @@ impl Server {
// Sync animation states
for (entity, &uid, &animation_history) in (
&self.state.ecs().internal().entities(),
&self.state.ecs().internal().read_storage::<Uid>(),
&self.state.ecs().internal().read_storage::<comp::AnimationHistory>(),
&self.state.ecs().entities(),
&self.state.ecs().read_storage::<Uid>(),
&self.state.ecs().read_storage::<comp::AnimationHistory>(),
).join() {
// Check if we need to sync
if Some(animation_history.current) == animation_history.last {
@ -424,14 +422,14 @@ impl Server {
// Update animation last/current state
for (entity, mut animation_history) in (
&self.state.ecs().internal().entities(),
&mut self.state.ecs().internal().write_storage::<comp::AnimationHistory>()
&self.state.ecs().entities(),
&mut self.state.ecs().write_storage::<comp::AnimationHistory>()
).join() {
animation_history.last = Some(animation_history.current);
}
// Remove all force flags
self.state.ecs_mut().internal_mut().write_storage::<comp::phys::ForceUpdate>().clear();
self.state.ecs_mut().write_storage::<comp::phys::ForceUpdate>().clear();
}
pub fn generate_chunk(&mut self, key: Vec3<i32>) {

View File

@ -89,7 +89,7 @@ impl Figures {
pub fn maintain(&mut self, renderer: &mut Renderer, client: &mut Client) {
let time = client.state().get_time();
let ecs = client.state_mut().ecs_mut().internal_mut();
let ecs = client.state_mut().ecs_mut();
for (entity, pos, dir, character, animation_history) in (
&ecs.entities(),
&ecs.read_storage::<comp::phys::Pos>(),

View File

@ -111,7 +111,6 @@ impl Scene {
let player_pos = client
.state()
.ecs()
.internal()
.read_storage::<comp::phys::Pos>()
.get(client.player())
.map(|pos| pos.0)