use bevy::ecs::entity::MapEntities; use bevy::math::Curve; use bevy::prelude::*; use leafwing_input_manager::prelude::*; use lightyear::prelude::*; use lightyear::{input::config::InputConfig, prelude::input::leafwing}; use serde::{Deserialize, Serialize}; /// Identifies which client owns a given player #[derive(Component, Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct PlayerId(pub PeerId); /// Stores position of the player n stuff #[derive(Component, Serialize, Deserialize, Clone, Debug, PartialEq, Default)] pub struct PlayerPosition(pub Vec2); /// Stores the colour of the box so each player has a different lookin one #[derive(Component, Serialize, Deserialize, Clone, Debug, PartialEq, Default)] pub struct PlayerColor(pub(crate) Color); #[derive(Component)] #[require(PlayerPosition, PlayerColor)] pub(crate) struct Player; // Example of a component that contains an entity. // This component, when replicated, needs to have the inner entity mapped from the Server world // to the client World. // You will need to derive the `MapEntities` trait for the component, and register // app.add_map_entities() in your protocol #[derive(Component, Deserialize, Serialize, Clone, Debug, PartialEq)] pub struct PlayerParent(Entity); // Ensures that the replication thing works on the client :thumbsup: impl MapEntities for PlayerParent { fn map_entities(&mut self, entity_mapper: &mut E) { self.0 = entity_mapper.get_mapped(self.0); } } /* not used pub struct Channel1; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct Message1(pub usuze); */ /// Inputs #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, Hash, Reflect, Actionlike)] pub enum PlayerActions { Up, Down, Left, Right, } /// Does the stuff to add the protocol pub struct ProtocolPlugin; impl Plugin for ProtocolPlugin { fn build(&self, app: &mut App) { app.add_plugins(leafwing::InputPlugin:: { config: InputConfig { rebroadcast_inputs: true, ..default() }, }); app.component::().replicate(); app.component::().replicate(); app.component::().replicate(); } }