-
Notifications
You must be signed in to change notification settings - Fork 42
/
entity_maping.rs
149 lines (126 loc) · 4.2 KB
/
entity_maping.rs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use bevy::prelude::*;
use bevy_ggrs::*;
use ggrs::*;
use instant::Duration;
pub struct GGRSConfig;
impl Config for GGRSConfig {
type Input = u8;
type State = u8;
type Address = usize;
}
#[derive(Reflect, Component, Default)]
struct ChildEntity;
#[derive(Reflect, Component, Default)]
struct ParentEntity;
#[derive(Reflect, Resource, Default, Debug)]
struct FrameCounter(u16);
fn input_system(_: In<PlayerHandle>, mut delete_events: EventReader<DeleteChildEntityEvent>) -> u8 {
u8::from(delete_events.iter().count() > 0)
}
fn setup_system(mut commands: Commands) {
commands
.spawn((Rollback::new(0), ParentEntity))
.with_children(|parent| {
parent.spawn((Rollback::new(1), ChildEntity));
});
}
fn delete_child_system(
mut commands: Commands,
inputs: Res<PlayerInputs<GGRSConfig>>,
parent: Query<&Children, With<ParentEntity>>,
child: Query<Entity, With<ChildEntity>>,
) {
println!("Inputs: {:?}", **inputs);
println!("Parent's children: {:?}", parent.single());
if let Ok(child) = child.get_single() {
println!("Child exists: {child:?}");
}
if inputs[0].0 == 1 {
println!("Despawning child");
let child_entity = parent.single()[0];
commands.entity(child_entity).despawn();
}
}
fn frame_counter(mut counter: ResMut<FrameCounter>) {
println!("==== Frame {} ====", counter.0);
counter.0 = counter.0.wrapping_add(1);
}
struct DeleteChildEntityEvent;
/// This test makes sure that we correctly map entities stored in resource and components during
/// snapshot and restore.
#[test]
fn entity_mapping() {
let mut app = App::new();
app.add_plugins(MinimalPlugins)
.add_plugin(TransformPlugin)
.add_event::<DeleteChildEntityEvent>()
.init_resource::<FrameCounter>()
.add_startup_system(setup_system)
// Insert the GGRS session
.insert_resource(Session::SyncTestSession(
SessionBuilder::<GGRSConfig>::new()
.with_num_players(1)
.with_check_distance(2)
.add_player(PlayerType::Local, 0)
.unwrap()
.start_synctest_session()
.unwrap(),
));
GGRSPlugin::<GGRSConfig>::new()
.with_update_frequency(60)
.with_input_system(input_system)
.register_rollback_component::<ChildEntity>()
.register_rollback_component::<ParentEntity>()
.register_rollback_resource::<FrameCounter>()
.with_rollback_schedule(
Schedule::default().with_stage(
"default",
SystemStage::single_threaded()
.with_system(delete_child_system)
.with_system(frame_counter.before(delete_child_system)),
),
)
.build(&mut app);
// Sleep helper that will make sure at least one frame should be executed by the GGRS fixed
// update loop.
let sleep = || std::thread::sleep(Duration::from_secs_f32(1.0 / 60.0));
// Re-usable queries
let get_queries = |app: &mut App| {
(
app.world.query::<(&ChildEntity, &Parent)>(),
app.world.query::<(&ParentEntity, &Children)>(),
)
};
// Update once, the world should now be setup
app.update();
let (mut child_query, mut parent_query) = get_queries(&mut app);
assert!(
child_query.get_single(&app.world).is_ok(),
"Child doesn't exist"
);
assert!(
parent_query.get_single(&app.world).is_ok(),
"Parent doesn't exist"
);
sleep();
app.update();
// Send the event to delete the child entity
app.world
.resource_mut::<Events<DeleteChildEntityEvent>>()
.send(DeleteChildEntityEvent);
// Run for a number of times to make sure we get some rollbacks to happen
for _ in 0..5 {
sleep();
app.update();
}
// Make sure the child is delete and the parent still exists
let (mut child_query, mut parent_query) = get_queries(&mut app);
assert!(
child_query.get_single(&app.world).is_err(),
"Child exists after deletion"
);
assert!(
parent_query.get_single(&app.world).is_ok(),
"Parent doesn't exist"
);
}