~starkingdoms/starkingdoms

ref: b6949834d53aadf53fa99b5f914a3dac09702395 starkingdoms/crates/unified/src/server/mod.rs -rw-r--r-- 1.2 KiB
b6949834 — core replication experiment 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::time::Duration;
use bevy::prelude::*;
use starkingdoms_proc::replicable;
use crate::replication::{ReplicateExt, Replicated, ReplicationServerPlugin};

pub fn server_plugin(app: &mut App) {
    app
        .replicate::<TestComponent>()
        .add_systems(Startup, spawn_test_thingy)
        .add_systems(FixedUpdate, update_test_thingy)
        .add_systems(Update, remove_test_thingy);
}

#[derive(Component)]
struct TestComponent2;

#[derive(Component)]
#[replicable]
pub struct TestComponent {
    hi: i32
}

fn spawn_test_thingy(mut commands: Commands) {
    commands.spawn((
        Replicated,
        TestComponent2,
        TestComponent { hi: 123 },
    ));
}
fn update_test_thingy(mut query: Query<&mut TestComponent>) {
    for mut thingy in query.iter_mut() {
        thingy.hi += 1;
    }
}
fn remove_test_thingy(mut commands: Commands, query: Query<Entity, With<TestComponent2>>, time: Res<Time>) {
    if time.elapsed() < Duration::from_secs(10) { return; }
    for entity in query.iter() {
        commands.entity(entity).remove::<TestComponent>();
    }
    if time.elapsed() < Duration::from_secs(20) { return; }
    for entity in query.iter() {
        commands.entity(entity).despawn();
    }
}