forked from naia-lib/naia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.rs
104 lines (89 loc) · 3.43 KB
/
commands.rs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use bevy_ecs::{
entity::Entity,
system::{Command as BevyCommand, EntityCommands},
world::{Mut, World},
};
use naia_bevy_shared::{EntityAuthStatus, HostOwned, WorldProxyMut};
use naia_server::{ReplicationConfig, Server as NaiaServer, UserKey};
use crate::Server;
// Bevy Commands Extension
pub trait EntityCommandsExt<'a> {
fn enable_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a>;
fn disable_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a>;
fn configure_replication(&'a mut self, config: ReplicationConfig)
-> &'a mut EntityCommands<'a>;
fn replication_config(&'a self, server: &Server) -> Option<ReplicationConfig>;
fn give_authority(
&'a mut self,
server: &mut Server,
user_key: &UserKey,
) -> &'a mut EntityCommands<'a>;
fn take_authority(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a>;
fn authority(&'a self, server: &Server) -> Option<EntityAuthStatus>;
fn pause_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a>;
fn resume_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a>;
}
impl<'a> EntityCommandsExt<'a> for EntityCommands<'a> {
fn enable_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a> {
server.enable_replication(&self.id());
self.insert(HostOwned);
return self;
}
fn disable_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a> {
server.disable_replication(&self.id());
self.remove::<HostOwned>();
return self;
}
fn configure_replication(
&'a mut self,
config: ReplicationConfig,
) -> &'a mut EntityCommands<'a> {
let entity = self.id();
let mut commands = self.commands();
let command = ConfigureReplicationCommand::new(entity, config);
commands.add(command);
return self;
}
fn replication_config(&'a self, server: &Server) -> Option<ReplicationConfig> {
server.replication_config(&self.id())
}
fn give_authority(
&'a mut self,
_server: &mut Server,
_user_key: &UserKey,
) -> &'a mut EntityCommands<'a> {
todo!()
}
fn take_authority(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a> {
server.entity_take_authority(&self.id());
return self;
}
fn authority(&'a self, server: &Server) -> Option<EntityAuthStatus> {
server.entity_authority_status(&self.id())
}
fn pause_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a> {
server.pause_replication(&self.id());
return self;
}
fn resume_replication(&'a mut self, server: &mut Server) -> &'a mut EntityCommands<'a> {
server.resume_replication(&self.id());
return self;
}
}
//// ConfigureReplicationCommand Command ////
pub(crate) struct ConfigureReplicationCommand {
entity: Entity,
config: ReplicationConfig,
}
impl ConfigureReplicationCommand {
pub fn new(entity: Entity, config: ReplicationConfig) -> Self {
Self { entity, config }
}
}
impl BevyCommand for ConfigureReplicationCommand {
fn apply(self, world: &mut World) {
world.resource_scope(|world, mut server: Mut<NaiaServer<Entity>>| {
server.configure_entity_replication(&mut world.proxy_mut(), &self.entity, self.config);
});
}
}