commit 91f4cf3c3d8576c495f456e4aefa737c9b97d9c5
parent 5f425167a60589e1625c442e42c983351f89d378
Author: MTRNord <mtrnord1@gmail.com>
Date: Tue, 2 Apr 2024 18:38:02 +0200
Apply suggestion from https://github.com/cBournhonesque/lightyear/issues/220#issuecomment-2032071880
Diffstat:
2 files changed, 25 insertions(+), 46 deletions(-)
diff --git a/src/networking/client.rs b/src/networking/client.rs
@@ -8,7 +8,7 @@ use bevy_ecs_ldtk::LdtkWorldBundle;
use lightyear::prelude::client::*;
use lightyear::prelude::*;
-use crate::player::PlayerBundle;
+use crate::player::{AnimationIndices, AnimationTimer, PlayerBundle};
use super::protocol::{
protocol, ClientMut, Components, Inputs, MatrixRPGGameProto, PlayerId, PlayerPosition,
@@ -152,7 +152,7 @@ pub(crate) fn buffer_input(mut client: ClientMut, keypress: Res<ButtonInput<KeyC
fn player_movement(
mut position_query: Query<
(&mut Transform, &mut PlayerPosition),
- (With<Predicted>, Without<Camera>),
+ (With<Predicted>, With<PlayerId>, Without<Camera>),
>,
mut cameras: Query<&mut Transform, With<Camera>>,
mut input_reader: EventReader<InputEvent<Inputs>>,
@@ -182,21 +182,40 @@ fn spawn_player(
mut commands: Commands,
players: Query<&PlayerId, With<PlayerPosition>>,
metadata: Res<GlobalMetadata>,
+ asset_server: Res<AssetServer>,
+ mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
// return early if we still don't have access to the client id
let Some(client_id) = metadata.client_id else {
return;
};
- // Only spawn other players
for player_id in players.iter() {
if player_id.0 == client_id {
return;
}
}
info!("got spawn input");
+
+ let texture = asset_server.load("tilesets/user.png");
+ let layout = TextureAtlasLayout::from_grid(Vec2::new(16.0, 16.0), 8, 8, None, None);
+ let texture_atlas_layout = texture_atlas_layouts.add(layout);
+ // Use only the subset of sprites in the sheet that make up the run animation
+ let animation_indices = AnimationIndices { first: 0, last: 3 };
+ let atlas = TextureAtlas {
+ layout: texture_atlas_layout.clone(),
+ index: animation_indices.first,
+ };
commands.spawn((
PlayerBundle::new(client_id, Vec2::ZERO),
+ AnimationTimer(Timer::from_seconds(0.3, TimerMode::Repeating)),
+ animation_indices,
+ SpriteSheetBundle {
+ transform: Transform::from_xyz(0., 0., 17.).with_scale(Vec3::splat(2.0)),
+ texture: texture.clone(),
+ atlas,
+ ..default()
+ },
// IMPORTANT: this lets the server know that the entity is pre-predicted
// when the server replicates this entity; we will get a Confirmed entity which will use this entity
// as the Predicted version
diff --git a/src/networking/mod.rs b/src/networking/mod.rs
@@ -4,14 +4,10 @@ use bevy::prelude::*;
use bevy::render::RenderPlugin;
use bevy::utils::Duration;
-use lightyear::{
- client::components::Confirmed, prelude::*, shared::events::components::ComponentInsertEvent,
-};
+use lightyear::{client::components::Confirmed, prelude::*};
use serde::{Deserialize, Serialize};
-use crate::player::{AnimationIndices, AnimationTimer};
-
-use self::protocol::{Inputs, PlayerId, PlayerPosition};
+use self::protocol::{Inputs, PlayerPosition};
pub mod client;
pub mod protocol;
@@ -34,7 +30,7 @@ pub struct SharedPlugin;
impl Plugin for SharedPlugin {
fn build(&self, app: &mut App) {
if app.is_plugin_added::<RenderPlugin>() {
- app.add_systems(PostUpdate, (draw_elements, spawn_tiles));
+ app.add_systems(PostUpdate, draw_elements);
// app.add_plugins(LogDiagnosticsPlugin {
// filter: Some(vec![
// IoDiagnosticsPlugin::BYTES_IN,
@@ -65,42 +61,6 @@ pub(crate) fn shared_movement_behaviour(mut position: Mut<PlayerPosition>, input
}
}
-pub fn spawn_tiles(
- mut commands: Commands,
- asset_server: Res<AssetServer>,
- mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
- mut new_players: EventReader<ComponentInsertEvent<PlayerId>>,
-) {
- let texture = asset_server.load("tilesets/user.png");
- let layout = TextureAtlasLayout::from_grid(Vec2::new(16.0, 16.0), 8, 8, None, None);
- let texture_atlas_layout = texture_atlas_layouts.add(layout);
-
- for player_event in new_players.read() {
- let entity = player_event.entity();
- info!("Spawning tile");
- // Use only the subset of sprites in the sheet that make up the run animation
- let animation_indices = AnimationIndices { first: 0, last: 3 };
- let atlas = TextureAtlas {
- layout: texture_atlas_layout.clone(),
- index: animation_indices.first,
- };
- if let Some(mut e) = commands.get_entity(entity) {
- e.with_children(|parent| {
- parent.spawn((
- AnimationTimer(Timer::from_seconds(0.3, TimerMode::Repeating)),
- animation_indices,
- SpriteSheetBundle {
- transform: Transform::from_xyz(0., 0., 17.).with_scale(Vec3::splat(2.0)),
- texture: texture.clone(),
- atlas,
- ..default()
- },
- ));
- });
- }
- }
-}
-
/// System that draws the player's boxes and cursors
pub fn draw_elements(mut gizmos: Gizmos, players: Query<&PlayerPosition, Without<Confirmed>>) {
for position in &players {