forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.rs
More file actions
90 lines (79 loc) · 2.78 KB
/
Copy pathinit.rs
File metadata and controls
90 lines (79 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::entity::{EntityComponent, PlayerComponent};
use crate::joinhandler::SPAWN_POSITION;
use crate::network::PlayerPreJoinEvent;
use crate::player::{ChunkPendingComponent, InventoryComponent, LoadedChunksComponent};
use feather_core::Gamemode;
use hashbrown::HashSet;
use shrev::{EventChannel, ReaderId};
use specs::SystemData;
use specs::{Read, System, World, WriteStorage};
/// System for initializing the necessary components
/// when a player joins.
#[derive(Default)]
pub struct PlayerInitSystem {
join_event_reader: Option<ReaderId<PlayerPreJoinEvent>>,
}
impl PlayerInitSystem {
pub fn new() -> Self {
Self {
join_event_reader: None,
}
}
}
impl<'a> System<'a> for PlayerInitSystem {
type SystemData = (
Read<'a, EventChannel<PlayerPreJoinEvent>>,
WriteStorage<'a, PlayerComponent>,
WriteStorage<'a, EntityComponent>,
WriteStorage<'a, ChunkPendingComponent>,
WriteStorage<'a, LoadedChunksComponent>,
WriteStorage<'a, InventoryComponent>,
);
fn run(&mut self, data: Self::SystemData) {
let (
events,
mut player_comps,
mut entity_comps,
mut chunk_pending_comps,
mut loaded_chunk_comps,
mut inventory_comps,
) = data;
// Run through events
for event in events.read(&mut self.join_event_reader.as_mut().unwrap()) {
let player_comp = PlayerComponent {
profile_properties: event.profile_properties.clone(),
gamemode: Gamemode::Creative,
};
player_comps.insert(event.player, player_comp).unwrap();
let entity_comp = EntityComponent {
uuid: event.uuid,
display_name: event.username.clone(),
position: SPAWN_POSITION,
on_ground: true,
};
entity_comps.insert(event.player, entity_comp).unwrap();
let chunk_pending_comp = ChunkPendingComponent {
pending: HashSet::new(),
};
chunk_pending_comps
.insert(event.player, chunk_pending_comp)
.unwrap();
let loaded_chunk_comp = LoadedChunksComponent::default();
loaded_chunk_comps
.insert(event.player, loaded_chunk_comp)
.unwrap();
let inventory_comp = InventoryComponent::new();
inventory_comps
.insert(event.player, inventory_comp)
.unwrap();
}
}
fn setup(&mut self, world: &mut World) {
Self::SystemData::setup(world);
self.join_event_reader = Some(
world
.fetch_mut::<EventChannel<PlayerPreJoinEvent>>()
.register_reader(),
);
}
}