bevy_octopus/
plugin.rs

1use crate::{
2    channels::{ChannelId, ChannelPacket, send_channel_message_system},
3    client,
4    network_node::{NetworkNode, network_node_event},
5    transformer::{DecoderChannels, EncoderChannels},
6    transports::{tcp::TcpPlugin, udp::UdpPlugin},
7};
8use bevy::{
9    app::{App, Plugin, PostUpdate, PreUpdate},
10    prelude::{IntoScheduleConfigs, SystemSet},
11};
12
13pub struct OctopusPlugin;
14
15impl Plugin for OctopusPlugin {
16    fn build(&self, app: &mut App) {
17        let app = register_reflect_types(app);
18        app.init_resource::<EncoderChannels>()
19            .init_resource::<DecoderChannels>()
20            .add_message::<ChannelPacket>()
21            .configure_sets(
22                PreUpdate,
23                (NetworkSet::Receive, NetworkSet::Decoding).chain(),
24            )
25            .configure_sets(PostUpdate, (NetworkSet::Encoding, NetworkSet::Send).chain())
26            .add_systems(PreUpdate, network_node_event.in_set(NetworkSet::Decoding))
27            .add_systems(
28                PostUpdate,
29                send_channel_message_system.in_set(NetworkSet::Send),
30            )
31            .add_plugins(client::plugin);
32
33        app.add_plugins(UdpPlugin).add_plugins(TcpPlugin);
34    }
35}
36
37#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
38pub enum NetworkSet {
39    Receive,
40    Decoding,
41    Encoding,
42    Send,
43}
44
45fn register_reflect_types(app: &mut App) -> &mut App {
46    app.register_type::<ChannelId>()
47        .register_type::<NetworkNode>()
48        .register_type::<&'static str>()
49}