Skip to content

Commit 4f743bf

Browse files
committed
Use improved spawn API for the tic_tac_toe example
Playing with bevyengine/bevy#17521
1 parent 40b2ced commit 4f743bf

1 file changed

Lines changed: 64 additions & 64 deletions

File tree

bevy_replicon_example_backend/examples/tic_tac_toe.rs

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ use std::{
66
io,
77
};
88

9-
use bevy::{platform::collections::HashMap, prelude::*};
9+
use bevy::{
10+
ecs::{relationship::RelatedSpawner, spawn::SpawnWith},
11+
platform::collections::HashMap,
12+
prelude::*,
13+
};
1014
use bevy_replicon::prelude::*;
1115
use bevy_replicon_example_backend::{ExampleClient, ExampleServer, RepliconExampleBackendPlugins};
1216
use clap::{Parser, ValueEnum};
@@ -159,64 +163,62 @@ fn setup_ui(mut commands: Commands, symbol_font: Res<SymbolFont>) {
159163
const TEXT_COLOR: Color = Color::srgb(0.5, 0.5, 1.0);
160164
const FONT_SIZE: f32 = 32.0;
161165

162-
commands
163-
.spawn(Node {
166+
commands.spawn((
167+
Node {
164168
width: Val::Percent(100.0),
165169
height: Val::Percent(100.0),
166170
align_items: AlignItems::Center,
167171
justify_content: JustifyContent::Center,
168172
..Default::default()
169-
})
170-
.with_children(|parent| {
171-
parent
172-
.spawn(Node {
173-
flex_direction: FlexDirection::Column,
174-
width: Val::Px(BOARD_SIZE - LINE_THICKNESS),
175-
height: Val::Px(BOARD_SIZE - LINE_THICKNESS),
176-
..Default::default()
177-
})
178-
.with_children(|parent| {
179-
parent
180-
.spawn(Node {
181-
display: Display::Grid,
182-
grid_template_columns: vec![GridTrack::auto(); GRID_SIZE],
183-
..Default::default()
184-
})
185-
.with_children(|parent| {
186-
for index in 0..GRID_SIZE * GRID_SIZE {
187-
parent.spawn(Cell { index }).observe(pick_cell);
188-
}
189-
});
190-
191-
parent
192-
.spawn(Node {
193-
margin: UiRect::top(Val::Px(20.0)),
194-
justify_content: JustifyContent::Center,
173+
},
174+
children![(
175+
Node {
176+
flex_direction: FlexDirection::Column,
177+
width: Val::Px(BOARD_SIZE - LINE_THICKNESS),
178+
height: Val::Px(BOARD_SIZE - LINE_THICKNESS),
179+
..Default::default()
180+
},
181+
children![
182+
(
183+
Node {
184+
display: Display::Grid,
185+
grid_template_columns: vec![GridTrack::auto(); GRID_SIZE],
186+
..Default::default()
187+
},
188+
Children::spawn(SpawnWith(|parent: &mut RelatedSpawner<_>| {
189+
for index in 0..GRID_SIZE * GRID_SIZE {
190+
parent.spawn(Cell { index }).observe(pick_cell);
191+
}
192+
}))
193+
),
194+
(
195+
Node {
196+
margin: UiRect::top(Val::Px(20.0)),
197+
justify_content: JustifyContent::Center,
198+
..Default::default()
199+
},
200+
children![(
201+
Text::default(),
202+
TextFont {
203+
font_size: FONT_SIZE,
195204
..Default::default()
196-
})
197-
.with_children(|parent| {
198-
parent
199-
.spawn((
200-
Text::default(),
201-
TextFont {
202-
font_size: FONT_SIZE,
203-
..Default::default()
204-
},
205-
TextColor(TEXT_COLOR),
206-
BottomText,
207-
))
208-
.with_child((
209-
TextSpan::default(),
210-
TextFont {
211-
font: symbol_font.0.clone(),
212-
font_size: FONT_SIZE,
213-
..Default::default()
214-
},
215-
TextColor(TEXT_COLOR),
216-
));
217-
});
218-
});
219-
});
205+
},
206+
TextColor(TEXT_COLOR),
207+
BottomText,
208+
children![(
209+
TextSpan::default(),
210+
TextFont {
211+
font: symbol_font.0.clone(),
212+
font_size: FONT_SIZE,
213+
..Default::default()
214+
},
215+
TextColor(TEXT_COLOR),
216+
)]
217+
)]
218+
)
219+
]
220+
)],
221+
));
220222
}
221223

222224
/// Converts point clicks into cell picking events.
@@ -297,17 +299,15 @@ fn init_symbols(
297299
commands
298300
.entity(trigger.target())
299301
.remove::<Interaction>()
300-
.with_children(|parent| {
301-
parent.spawn((
302-
Text::new(symbol.glyph()),
303-
TextFont {
304-
font: symbol_font.0.clone(),
305-
font_size: 65.0,
306-
..Default::default()
307-
},
308-
TextColor(symbol.color()),
309-
));
310-
});
302+
.with_child((
303+
Text::new(symbol.glyph()),
304+
TextFont {
305+
font: symbol_font.0.clone(),
306+
font_size: 65.0,
307+
..Default::default()
308+
},
309+
TextColor(symbol.color()),
310+
));
311311
}
312312

313313
/// Sends cell and local player entities and starts the game.

0 commit comments

Comments
 (0)