placeholder-project

Temp project
git clone git://archive.git.mtrnord.blog/MTRNord/placeholder-project.git
Log | Files | Refs

player.rs (1598B)


      1 use bevy::prelude::*;
      2 use lightyear::{connection::netcode::ClientId, shared::replication::components::NetworkTarget};
      3 
      4 use crate::networking::protocol::{PlayerId, PlayerPosition, Replicate};
      5 
      6 /// Plugin for spawning the player and controlling them.
      7 pub struct PlayerPlugin;
      8 
      9 impl Plugin for PlayerPlugin {
     10     fn build(&self, app: &mut App) {
     11         app.add_systems(Update, animate_sprite);
     12     }
     13 }
     14 
     15 #[derive(Component)]
     16 pub struct AnimationIndices {
     17     pub first: usize,
     18     pub last: usize,
     19 }
     20 
     21 #[derive(Component, Deref, DerefMut)]
     22 pub struct AnimationTimer(pub Timer);
     23 
     24 fn animate_sprite(
     25     time: Res<Time>,
     26     mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut TextureAtlas)>,
     27 ) {
     28     for (indices, mut timer, mut atlas) in &mut query {
     29         timer.tick(time.delta());
     30         if timer.just_finished() {
     31             atlas.index = if atlas.index == indices.last {
     32                 indices.first
     33             } else {
     34                 atlas.index + 1
     35             };
     36         }
     37     }
     38 }
     39 
     40 #[derive(Bundle)]
     41 pub struct PlayerBundle {
     42     id: PlayerId,
     43     pub position: PlayerPosition,
     44     replicate: Replicate,
     45 }
     46 
     47 impl PlayerBundle {
     48     pub(crate) fn new(id: ClientId, position: Vec2) -> Self {
     49         Self {
     50             id: PlayerId(id),
     51             position: PlayerPosition(position),
     52             replicate: Replicate {
     53                 // prediction_target: NetworkTarget::None,
     54                 prediction_target: NetworkTarget::Only(vec![id]),
     55                 interpolation_target: NetworkTarget::AllExcept(vec![id]),
     56                 ..default()
     57             },
     58         }
     59     }
     60 }