feat(README): added plugin guidelines

This commit is contained in:
malek 2022-07-30 02:28:17 -07:00
parent 93d25e93ba
commit 74cc64c568
3 changed files with 31 additions and 0 deletions

View File

@ -16,3 +16,6 @@ Second, all structs that are passed as events should not be naked if they contai
Third, you must put events and resources within their own local files `event` and `resource` and you should also only refer to them using the `event::EVENT` or `resource::RESOURCE` syntax. You should not import the full namespace down to `event` or `resource`, at maximum you can only import to the level before that. This helps to keep events and resources and other structs distinct. Third, you must put events and resources within their own local files `event` and `resource` and you should also only refer to them using the `event::EVENT` or `resource::RESOURCE` syntax. You should not import the full namespace down to `event` or `resource`, at maximum you can only import to the level before that. This helps to keep events and resources and other structs distinct.
### Plugins
The `plugin` is a very important concept in `bevy` for modularity, and reusability. As such many pieces of `nexus` are split into multiple plugins. As plugins are once again structs given extra behavior they follow the rules above, placed in their own local `plugin` folder, however plugins are likely to be used outside of `nexus` so having `plugin` as part of the name, such as `struct PayloadPlugin` is not only permissible, but required, `Plugin` must be postfixed to the end of any `plugin` struct.

View File

@ -0,0 +1,28 @@
use std::net::SocketAddr;
use bevy::app::{App, Plugin};
use bevy::prelude::{Commands, EventReader, ResMut};
use bytes::Bytes;
use crossbeam_channel::{Receiver, Sender};
use qp2p::{Config, Endpoint};
use tokio::runtime::Runtime;
use bevy_add_events_macro::add_events;
use crate::networking::{event, resource};
pub struct NetworkingPlugin;
impl Plugin for NetworkingPlugin {
fn build(&self, app: &mut App) {
add_events!(app, ServerConnection, ServerSocketAddr);
app.add_startup_system(setup_networking_runtime());
}
}
pub fn setup_networking_runtime(mut commands: Commands) {
commands.insert_resource(resource::NetworkingRuntime(Runtime::new().expect("networking runtime needs to be init'd to work!")));
}
pub fn connection_added(mut commands: Commands, mut socket_addr: EventReader<event::ServerSocketAddr>, mut networking_runtime: ResMut<resource::NetworkingRuntime>) {
let mut config = Config::default();
config.idle_timeout = None;
let endpoint = Endpoint::new_client(SocketAddr::from)
}