//! Network protocol: replicated components and client/server events.
//!
//! Registration order must be identical on server and client, so everything is
//! registered here and used by both.
use bevy::prelude::*;
use bevy_replicon::prelude::*;
use serde::{Deserialize, Serialize};
use crate::{components::*, grid::Dir};
/// First event a client sends; selects (or creates) the persistent character.
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct Login {
pub name: String,
}
/// Direction the player is holding, `None` to stop.
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct SetMoveDir(pub Option
);
/// Pick up the item on the given tile (own or adjacent).
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct PickUp {
pub target: GridPos,
}
/// Interact with the building on the given adjacent tile.
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct Interact {
pub target: GridPos,
}
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct UseItem {
pub slot: usize,
}
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct DropItem {
pub slot: usize,
}
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct Craft {
pub recipe: String,
}
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct Construct {
pub building: String,
pub target: GridPos,
}
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct Deconstruct {
pub target: GridPos,
}
/// Text shown in the receiving player's message log.
#[derive(Event, Clone, Debug, Serialize, Deserialize)]
pub struct GameLog {
pub text: String,
}
pub struct ProtocolPlugin;
impl Plugin for ProtocolPlugin {
fn build(&self, app: &mut App) {
app.replicate::()
.replicate::()
.replicate::()
.replicate::()
.replicate::()
.replicate::()
.replicate::()
.replicate::()
.replicate::()
.replicate::()
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_client_event::(Channel::Ordered)
.add_server_event::(Channel::Ordered);
}
}