Something went wrong. Try again.
cozy factory game
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677use 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<PlayerParent>() 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<E: EntityMapper>(&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 protocolpub struct ProtocolPlugin;
impl Plugin for ProtocolPlugin { fn build(&self, app: &mut App) { app.add_plugins(leafwing::InputPlugin::<PlayerActions> { config: InputConfig { rebroadcast_inputs: true, ..default() }, });
app.component::<PlayerId>().replicate();
app.component::<PlayerPosition>().replicate();
app.component::<PlayerColor>().replicate(); }}