station/crates/game_core/src/protocol.rs

92 lines
2.7 KiB
Rust

//! 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<Dir>);
/// 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::<WorldMap>()
.replicate::<GridPos>()
.replicate::<Facing>()
.replicate::<Character>()
.replicate::<Vitals>()
.replicate::<Inventory>()
.replicate::<Skills>()
.replicate::<GroundItem>()
.replicate::<Building>()
.replicate::<BuildingState>()
.add_client_event::<Login>(Channel::Ordered)
.add_client_event::<SetMoveDir>(Channel::Ordered)
.add_client_event::<PickUp>(Channel::Ordered)
.add_client_event::<Interact>(Channel::Ordered)
.add_client_event::<UseItem>(Channel::Ordered)
.add_client_event::<DropItem>(Channel::Ordered)
.add_client_event::<Craft>(Channel::Ordered)
.add_client_event::<Construct>(Channel::Ordered)
.add_client_event::<Deconstruct>(Channel::Ordered)
.add_server_event::<GameLog>(Channel::Ordered);
}
}