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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::{collections::HashMap, hash::Hash};
use crate::world::local_world_manager::LocalWorldManager;
use crate::{
messages::channels::receivers::indexed_message_reader::IndexedMessageReader,
world::remote::entity_event::EntityEvent, BitReader, ComponentKind, ComponentKinds,
EntityAction, EntityActionReceiver, EntityActionType, EntityConverter, EntityHandleConverter,
MessageIndex, NetEntity, NetEntityHandleConverter, Protocol, Replicate, Serde, SerdeErr, Tick,
UnsignedVariableInteger, WorldMutType,
};
pub struct RemoteWorldManager {
receiver: EntityActionReceiver<NetEntity>,
received_components: HashMap<(NetEntity, ComponentKind), Box<dyn Replicate>>,
}
impl RemoteWorldManager {
pub fn new() -> Self {
Self {
receiver: EntityActionReceiver::new(),
received_components: HashMap::default(),
}
}
pub fn read_world_events<E: Copy + Eq + Hash, W: WorldMutType<E>>(
&mut self,
converter: &dyn EntityHandleConverter<E>,
local_world_manager: &mut LocalWorldManager<E>,
protocol: &Protocol,
world: &mut W,
tick: Tick,
reader: &mut BitReader,
) -> Result<Vec<EntityEvent<E>>, SerdeErr> {
let mut events = Vec::new();
self.read_updates(
converter,
local_world_manager,
&protocol.component_kinds,
world,
tick,
reader,
&mut events,
)?;
self.read_actions(
converter,
local_world_manager,
&protocol.component_kinds,
world,
reader,
&mut events,
)?;
Ok(events)
}
fn read_message_index(
reader: &mut BitReader,
last_index_opt: &mut Option<MessageIndex>,
) -> Result<MessageIndex, SerdeErr> {
let current_index = IndexedMessageReader::read_message_index(reader, last_index_opt)?;
*last_index_opt = Some(current_index);
Ok(current_index)
}
pub fn read_actions<E: Copy + Eq + Hash, W: WorldMutType<E>>(
&mut self,
handle_converter: &dyn EntityHandleConverter<E>,
local_world_manager: &mut LocalWorldManager<E>,
component_kinds: &ComponentKinds,
world: &mut W,
reader: &mut BitReader,
events: &mut Vec<EntityEvent<E>>,
) -> Result<(), SerdeErr> {
let mut last_read_id: Option<MessageIndex> = None;
{
let converter = EntityConverter::new(handle_converter, local_world_manager);
loop {
let action_continue = bool::de(reader)?;
if !action_continue {
break;
}
self.read_action(&converter, component_kinds, reader, &mut last_read_id)?;
}
}
self.process_incoming_actions(local_world_manager, world, events);
Ok(())
}
fn read_action(
&mut self,
converter: &dyn NetEntityHandleConverter,
component_kinds: &ComponentKinds,
reader: &mut BitReader,
last_read_id: &mut Option<MessageIndex>,
) -> Result<(), SerdeErr> {
let action_id = Self::read_message_index(reader, last_read_id)?;
let action_type = EntityActionType::de(reader)?;
match action_type {
EntityActionType::SpawnEntity => {
let net_entity = NetEntity::de(reader)?;
let components_num = UnsignedVariableInteger::<3>::de(reader)?.get();
let mut component_kind_list = Vec::new();
for _ in 0..components_num {
let new_component = component_kinds.read(reader, converter)?;
let new_component_kind = new_component.kind();
self.received_components
.insert((net_entity, new_component_kind), new_component);
component_kind_list.push(new_component_kind);
}
self.receiver.buffer_action(
action_id,
EntityAction::SpawnEntity(net_entity, component_kind_list),
);
}
EntityActionType::DespawnEntity => {
let net_entity = NetEntity::de(reader)?;
self.receiver
.buffer_action(action_id, EntityAction::DespawnEntity(net_entity));
}
EntityActionType::InsertComponent => {
let net_entity = NetEntity::de(reader)?;
let new_component = component_kinds.read(reader, converter)?;
let new_component_kind = new_component.kind();
self.receiver.buffer_action(
action_id,
EntityAction::InsertComponent(net_entity, new_component_kind),
);
self.received_components
.insert((net_entity, new_component_kind), new_component);
}
EntityActionType::RemoveComponent => {
let net_entity = NetEntity::de(reader)?;
let component_kind = ComponentKind::de(component_kinds, reader)?;
self.receiver.buffer_action(
action_id,
EntityAction::RemoveComponent(net_entity, component_kind),
);
}
EntityActionType::Noop => {
self.receiver.buffer_action(action_id, EntityAction::Noop);
}
}
Ok(())
}
fn process_incoming_actions<E: Copy + Eq + Hash, W: WorldMutType<E>>(
&mut self,
local_world_manager: &mut LocalWorldManager<E>,
world: &mut W,
events: &mut Vec<EntityEvent<E>>,
) {
let incoming_actions = self.receiver.receive_actions();
for action in incoming_actions {
match action {
EntityAction::SpawnEntity(net_entity, components) => {
let world_entity = world.spawn_entity();
local_world_manager.remote_spawn_entity(&world_entity, &net_entity);
events.push(EntityEvent::<E>::SpawnEntity(world_entity));
for component_kind in components {
let component = self
.received_components
.remove(&(net_entity, component_kind))
.unwrap();
world.insert_boxed_component(&world_entity, component);
events.push(EntityEvent::<E>::InsertComponent(
world_entity,
component_kind,
));
}
}
EntityAction::DespawnEntity(net_entity) => {
let world_entity = local_world_manager.remote_despawn_entity(&net_entity);
for component_kind in world.component_kinds(&world_entity) {
if let Some(component) =
world.remove_component_of_kind(&world_entity, &component_kind)
{
events.push(EntityEvent::<E>::RemoveComponent(world_entity, component));
}
}
world.despawn_entity(&world_entity);
events.push(EntityEvent::<E>::DespawnEntity(world_entity));
}
EntityAction::InsertComponent(net_entity, component_kind) => {
let component = self
.received_components
.remove(&(net_entity, component_kind))
.unwrap();
let world_entity = local_world_manager.get_remote_entity(&net_entity);
world.insert_boxed_component(&world_entity, component);
events.push(EntityEvent::<E>::InsertComponent(
world_entity,
component_kind,
));
}
EntityAction::RemoveComponent(net_entity, component_kind) => {
let world_entity = local_world_manager.get_remote_entity(&net_entity);
let component = world
.remove_component_of_kind(&world_entity, &component_kind)
.expect("Component already removed?");
events.push(EntityEvent::<E>::RemoveComponent(world_entity, component));
}
EntityAction::Noop => {
}
}
}
}
pub fn read_updates<E: Copy + Eq + Hash, W: WorldMutType<E>>(
&mut self,
converter: &dyn EntityHandleConverter<E>,
local_world_manager: &LocalWorldManager<E>,
component_kinds: &ComponentKinds,
world: &mut W,
server_tick: Tick,
reader: &mut BitReader,
events: &mut Vec<EntityEvent<E>>,
) -> Result<(), SerdeErr> {
loop {
let update_continue = bool::de(reader)?;
if !update_continue {
break;
}
let net_entity_id = NetEntity::de(reader)?;
self.read_update(
converter,
local_world_manager,
component_kinds,
world,
server_tick,
reader,
&net_entity_id,
events,
)?;
}
Ok(())
}
fn read_update<E: Copy + Eq + Hash, W: WorldMutType<E>>(
&mut self,
handle_converter: &dyn EntityHandleConverter<E>,
local_world_manager: &LocalWorldManager<E>,
component_kinds: &ComponentKinds,
world: &mut W,
server_tick: Tick,
reader: &mut BitReader,
net_entity_id: &NetEntity,
events: &mut Vec<EntityEvent<E>>,
) -> Result<(), SerdeErr> {
loop {
let component_continue = bool::de(reader)?;
if !component_continue {
break;
}
let component_update = component_kinds.read_create_update(reader)?;
let component_kind = component_update.kind;
let world_entity = local_world_manager.get_remote_entity(net_entity_id);
let converter = EntityConverter::new(handle_converter, local_world_manager);
world.component_apply_update(
&converter,
&world_entity,
&component_kind,
component_update,
)?;
events.push(EntityEvent::UpdateComponent(
server_tick,
world_entity,
component_kind,
));
}
Ok(())
}
}