-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map Entity IDs When Writing Snapshot to World #31
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
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, Component, 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() | ||
.insert(Rollback::new(0)) | ||
.insert(ParentEntity) | ||
.with_children(|parent| { | ||
parent.spawn().insert(Rollback::new(1)).insert(ChildEntity); | ||
}); | ||
} | ||
|
||
fn delete_child_system( | ||
mut commands: Commands, | ||
inputs: Res<Vec<(u8, InputStatus)>>, | ||
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( | ||
SessionBuilder::<GGRSConfig>::new() | ||
.with_num_players(1) | ||
.with_check_distance(2) | ||
.add_player(PlayerType::Local, 0) | ||
.unwrap() | ||
.start_synctest_session() | ||
.unwrap(), | ||
) | ||
.insert_resource(SessionType::SyncTestSession); | ||
|
||
GGRSPlugin::<GGRSConfig>::new() | ||
.with_update_frequency(60) | ||
.with_input_system(input_system) | ||
.register_rollback_type::<ChildEntity>() | ||
.register_rollback_type::<ParentEntity>() | ||
.register_rollback_type::<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" | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want to look at this section here. I'm not sure if there's a better place to do this. The comment explains.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense.