Skip to content

Commit 889da96

Browse files
committed
Complete refactoring of systems
1 parent 5412cf4 commit 889da96

14 files changed

Lines changed: 162 additions & 158 deletions

File tree

‎server/src/entity/broadcast.rs‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! range of a player.
33
44
use crate::entity::movement::degrees_to_stops;
5-
use crate::entity::Metadata;
6-
use crate::entity::{EntityComponent, EntityType, VelocityComponent};
5+
use crate::entity::{EntityType, VelocityComponent};
6+
use crate::entity::{Metadata, NamedComponent, PositionComponent};
77
use crate::network::{send_packet_to_all_players, NetworkComponent};
88
use crate::util::protocol_velocity;
99
use feather_core::network::packet::implementation::SpawnObject;
@@ -12,6 +12,7 @@ use shrev::EventChannel;
1212
use specs::{
1313
Entities, Entity, Read, ReadStorage, ReaderId, System, SystemData, World, WriteStorage,
1414
};
15+
use uuid::Uuid;
1516

1617
//const ITEM_OBJECT_ID: i8 = 2;
1718

@@ -38,7 +39,8 @@ pub struct EntityBroadcastSystem {
3839

3940
impl<'a> System<'a> for EntityBroadcastSystem {
4041
type SystemData = (
41-
ReadStorage<'a, EntityComponent>,
42+
ReadStorage<'a, PositionComponent>,
43+
ReadStorage<'a, NamedComponent>,
4244
ReadStorage<'a, NetworkComponent>,
4345
ReadStorage<'a, VelocityComponent>,
4446
WriteStorage<'a, Metadata>,
@@ -47,10 +49,10 @@ impl<'a> System<'a> for EntityBroadcastSystem {
4749
);
4850

4951
fn run(&mut self, data: Self::SystemData) {
50-
let (entity_comps, networks, velocities, mut metadatas, events, entities) = data;
52+
let (positions, nameds, networks, velocities, mut metadatas, events, entities) = data;
5153

5254
for event in events.read(&mut self.reader.as_mut().unwrap()) {
53-
let entity = entity_comps.get(event.entity).unwrap();
55+
let position = positions.get(event.entity).unwrap();
5456
let metadata = metadatas.get_mut(event.entity).unwrap();
5557
let velocity = velocities.get(event.entity).cloned().unwrap_or_default();
5658
let (velocity_x, velocity_y, velocity_z) = protocol_velocity(*velocity);
@@ -62,14 +64,15 @@ impl<'a> System<'a> for EntityBroadcastSystem {
6264
// The Player Info packet was already sent by `JoinBroadcastSystem`.
6365
match event.ty {
6466
EntityType::Player => {
67+
let named = nameds.get(event.entity).unwrap();
6568
let packet = SpawnPlayer {
6669
entity_id: event.entity.id() as i32,
67-
player_uuid: entity.uuid,
68-
x: entity.position.x,
69-
y: entity.position.y,
70-
z: entity.position.z,
71-
yaw: degrees_to_stops(entity.position.yaw),
72-
pitch: degrees_to_stops(entity.position.pitch),
70+
player_uuid: named.uuid,
71+
x: position.current.x,
72+
y: position.current.y,
73+
z: position.current.z,
74+
yaw: degrees_to_stops(position.current.yaw),
75+
pitch: degrees_to_stops(position.current.pitch),
7376
metadata: metadata.to_raw_metadata(),
7477
};
7578

@@ -78,13 +81,13 @@ impl<'a> System<'a> for EntityBroadcastSystem {
7881
EntityType::Item => {
7982
let packet = SpawnObject {
8083
entity_id: event.entity.id() as i32,
81-
object_uuid: entity.uuid,
84+
object_uuid: Uuid::new_v4(),
8285
ty: 2, // Type 2 for item stack
83-
x: entity.position.x,
84-
y: entity.position.y,
85-
z: entity.position.z,
86-
pitch: degrees_to_stops(entity.position.pitch),
87-
yaw: degrees_to_stops(entity.position.yaw),
86+
x: position.current.x,
87+
y: position.current.y,
88+
z: position.current.z,
89+
pitch: degrees_to_stops(position.current.pitch),
90+
yaw: degrees_to_stops(position.current.yaw),
8891
data: 1, // Has velocity
8992
velocity_x,
9093
velocity_y,

‎server/src/entity/chunk.rs‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
//! chunk, which allows for more efficient nearby
33
//! entity queries and packet broadcasting.
44
5-
use crate::entity::{EntityComponent, EntityDestroyEvent, EntityMoveEvent, EntitySpawnEvent};
5+
use crate::entity::{EntityDestroyEvent, EntitySpawnEvent, PositionComponent};
66
use feather_core::world::ChunkPosition;
77
use fnv::FnvHashMap;
88
use shrev::EventChannel;
9-
use specs::{Entity, Read, ReadStorage, ReaderId, System, Write};
9+
use specs::storage::ComponentEvent;
10+
use specs::{BitSet, Entities, Entity, Join, Read, ReadStorage, ReaderId, System, Write};
1011

1112
/// Keeps track of which entities are in which chunk.
1213
#[derive(Debug, Clone, Deref, DerefMut, Default)]
@@ -57,40 +58,51 @@ impl ChunkEntities {
5758
/// and `EntityDestroyEvent`s.
5859
#[derive(Default)]
5960
pub struct ChunkEntityUpdateSystem {
60-
move_reader: Option<ReaderId<EntityMoveEvent>>,
61+
dirty: BitSet,
62+
move_reader: Option<ReaderId<ComponentEvent>>,
6163
spawn_reader: Option<ReaderId<EntitySpawnEvent>>,
6264
destroy_reader: Option<ReaderId<EntityDestroyEvent>>,
6365
}
6466

6567
impl<'a> System<'a> for ChunkEntityUpdateSystem {
6668
type SystemData = (
67-
ReadStorage<'a, EntityComponent>,
69+
ReadStorage<'a, PositionComponent>,
6870
Write<'a, ChunkEntities>,
69-
Read<'a, EventChannel<EntityMoveEvent>>,
7071
Read<'a, EventChannel<EntitySpawnEvent>>,
7172
Read<'a, EventChannel<EntityDestroyEvent>>,
73+
Entities<'a>,
7274
);
7375

7476
fn run(&mut self, data: Self::SystemData) {
75-
let (entity_comps, mut entity_chunks, move_events, spawn_events, destroy_events) = data;
77+
let (positions, mut entity_chunks, spawn_events, destroy_events, entities) = data;
78+
79+
self.dirty.clear();
80+
for event in positions.channel().read(self.move_reader.as_mut().unwrap()) {
81+
match event {
82+
ComponentEvent::Inserted(id) | ComponentEvent::Modified(id) => {
83+
self.dirty.add(*id);
84+
}
85+
_ => (),
86+
}
87+
}
7688

77-
for event in move_events.read(self.move_reader.as_mut().unwrap()) {
78-
let new_pos = event.new_pos.chunk_pos();
79-
let old_pos = event.old_pos.chunk_pos();
89+
for (position, entity, _) in (&positions, &entities, &self.dirty).join() {
90+
let new_pos = position.current.chunk_pos();
91+
let old_pos = position.previous.chunk_pos();
8092

8193
if new_pos != old_pos {
82-
entity_chunks.remove_from_chunk(old_pos, event.entity);
83-
entity_chunks.add_to_chunk(new_pos, event.entity);
94+
entity_chunks.remove_from_chunk(old_pos, entity);
95+
entity_chunks.add_to_chunk(new_pos, entity);
8496
}
8597
}
8698

8799
for event in spawn_events.read(self.spawn_reader.as_mut().unwrap()) {
88-
let pos = entity_comps.get(event.entity).unwrap().position;
100+
let pos = positions.get(event.entity).unwrap().current;
89101
entity_chunks.add_to_chunk(pos.chunk_pos(), event.entity);
90102
}
91103

92104
for event in destroy_events.read(self.destroy_reader.as_mut().unwrap()) {
93-
let pos = entity_comps.get(event.entity).unwrap().position;
105+
let pos = positions.get(event.entity).unwrap().current;
94106
entity_chunks.remove_from_chunk(pos.chunk_pos(), event.entity);
95107
}
96108
}

‎server/src/entity/component.rs‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Component for PlayerComponent {
1616
type Storage = BTreeStorage<Self>;
1717
}
1818

19-
#[derive(Debug, PartialEq, Eq)]
19+
#[derive(Debug, PartialEq)]
2020
pub struct PositionComponent {
2121
/// The current position of this entity.
2222
pub current: Position,
@@ -42,7 +42,7 @@ impl Component for PositionComponent {
4242
///
4343
/// Entities without this component are assumed
4444
/// to have a velocity of 0.
45-
#[derive(Deref, DerefMut, Debug, PartialEq)]
45+
#[derive(Deref, DerefMut, Debug, PartialEq, Clone)]
4646
pub struct VelocityComponent(pub Vec3);
4747

4848
impl Component for VelocityComponent {
@@ -70,12 +70,10 @@ impl Component for NamedComponent {
7070
pub struct ComponentResetSystem;
7171

7272
impl<'a> System<'a> for ComponentResetSystem {
73-
type SystemData = (WriteStorage<'a, PositionComponent>,);
73+
type SystemData = WriteStorage<'a, PositionComponent>;
7474

75-
fn run(&mut self, data: Self::SystemData) {
76-
let (mut positions) = data;
77-
78-
for (position) in (&mut positions).join() {
75+
fn run(&mut self, mut positions: Self::SystemData) {
76+
for position in (&mut positions).join() {
7977
position.reset();
8078
}
8179
}

‎server/src/entity/item.rs‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Logic for working with item entities.
22
33
use crate::entity::metadata::{self, Metadata};
4-
use crate::entity::{EntityComponent, EntitySpawnEvent, EntityType, VelocityComponent};
4+
use crate::entity::{EntitySpawnEvent, EntityType, PositionComponent, VelocityComponent};
55
use crate::player::{PlayerItemDropEvent, PLAYER_EYE_HEIGHT};
66
use rand::Rng;
77
use shrev::EventChannel;
@@ -19,7 +19,7 @@ pub struct ItemSpawnSystem {
1919

2020
impl<'a> System<'a> for ItemSpawnSystem {
2121
type SystemData = (
22-
WriteStorage<'a, EntityComponent>,
22+
WriteStorage<'a, PositionComponent>,
2323
WriteStorage<'a, VelocityComponent>,
2424
WriteStorage<'a, EntityType>,
2525
WriteStorage<'a, Metadata>,
@@ -30,7 +30,7 @@ impl<'a> System<'a> for ItemSpawnSystem {
3030

3131
fn run(&mut self, data: Self::SystemData) {
3232
let (
33-
mut entity_comps,
33+
mut positions,
3434
mut velocities,
3535
mut types,
3636
mut metadatas,
@@ -52,19 +52,17 @@ impl<'a> System<'a> for ItemSpawnSystem {
5252
let entity = entities.create();
5353

5454
let pos = {
55-
let player_pos = entity_comps.get(event.player).unwrap().position
55+
let player_pos = positions.get(event.player).unwrap().current
5656
+ glm::vec3(0.0, PLAYER_EYE_HEIGHT, 0.0);
5757
player_pos - glm::vec3(0.0, 0.3, 0.0)
5858
};
5959

60-
let entity_comp = EntityComponent {
61-
uuid: Uuid::new_v4(),
62-
display_name: String::with_capacity(0),
63-
position: pos,
64-
on_ground: false,
60+
let position = PositionComponent {
61+
previous: pos,
62+
current: pos,
6563
};
6664

67-
entity_comps.insert(entity, entity_comp).unwrap();
65+
positions.insert(entity, position).unwrap();
6866
types.insert(entity, EntityType::Item).unwrap();
6967
metadatas.insert(entity, metadata).unwrap();
7068

‎server/src/entity/mod.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::systems::{
1717
};
1818
pub use broadcast::EntitySpawnEvent;
1919
pub use chunk::ChunkEntities;
20-
use component::ComponentResetSystem;
2120
pub use component::{NamedComponent, PlayerComponent, PositionComponent, VelocityComponent};
2221
pub use destroy::EntityDestroyEvent;
2322
pub use metadata::{EntityBitMask, Metadata};

‎server/src/entity/movement.rs‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use specs::{
66
};
77

88
use crate::entity::{PositionComponent, VelocityComponent};
9-
use crate::physics::EntityVelocityUpdateEvent;
109
use crate::util::protocol_velocity;
1110
use feather_core::network::packet::implementation::{
1211
EntityHeadLook, EntityLook, EntityLookAndRelativeMove, EntityRelativeMove, EntityVelocity,
@@ -36,13 +35,13 @@ impl<'a> System<'a> for EntityMoveBroadcastSystem {
3635
for event in positions.channel().read(&mut self.reader.as_mut().unwrap()) {
3736
match event {
3837
ComponentEvent::Modified(index) | ComponentEvent::Inserted(index) => {
39-
self.dirty.add(&index)
38+
self.dirty.add(*index);
4039
}
4140
_ => (),
4241
}
4342
}
4443

45-
for (entity, position) in (&entities, &positions, &self.dirty).join() {
44+
for (entity, position, _) in (&entities, &positions, &self.dirty).join() {
4645
broadcast_entity_movement(
4746
entity,
4847
position.previous,
@@ -53,7 +52,7 @@ impl<'a> System<'a> for EntityMoveBroadcastSystem {
5352
}
5453
}
5554

56-
flagged_setup_impl!(reader);
55+
flagged_setup_impl!(PositionComponent, reader);
5756
}
5857

5958
/// System for broadcasting when an entity's velocity
@@ -79,16 +78,16 @@ impl<'a> System<'a> for EntityVelocityBroadcastSystem {
7978
for event in velocities.channel().read(self.reader.as_mut().unwrap()) {
8079
match event {
8180
ComponentEvent::Modified(index) | ComponentEvent::Inserted(index) => {
82-
self.dirty.add(*index)
81+
self.dirty.add(*index);
8382
}
8483
_ => (),
8584
}
8685
}
8786

88-
for (velocity, entity) in (&velocities, &entities, &self.dirty).join() {
87+
for (velocity, entity, _) in (&velocities, &entities, &self.dirty).join() {
8988
let (velocity_x, velocity_y, velocity_z) = protocol_velocity(velocity.0);
9089
let packet = EntityVelocity {
91-
entity_id: event.entity.id() as i32,
90+
entity_id: entity.id() as i32,
9291
velocity_x,
9392
velocity_y,
9493
velocity_z,
@@ -98,7 +97,7 @@ impl<'a> System<'a> for EntityVelocityBroadcastSystem {
9897
}
9998
}
10099

101-
flagged_setup_impl!(reader);
100+
flagged_setup_impl!(VelocityComponent, reader);
102101
}
103102

104103
/// Broadcasts to all joined players that an entity has moved.

0 commit comments

Comments
 (0)