Skip to content

Commit df5fb1b

Browse files
authored
Use improved spawn API for the tic_tac_toe example (#463)
Playing with bevyengine/bevy#17521
1 parent d907b55 commit df5fb1b

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

bevy_replicon_example_backend/examples/tic_tac_toe.rs

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
44
use std::fmt::{self, Formatter};
55

6-
use bevy::{platform::collections::HashMap, prelude::*};
6+
use bevy::{
7+
ecs::{relationship::RelatedSpawner, spawn::SpawnWith},
8+
platform::collections::HashMap,
9+
prelude::*,
10+
};
711
use bevy_replicon::prelude::*;
812
use bevy_replicon_example_backend::{ExampleClient, ExampleServer, RepliconExampleBackendPlugins};
913
use clap::{Parser, ValueEnum};
@@ -156,64 +160,62 @@ fn setup_ui(mut commands: Commands, symbol_font: Res<SymbolFont>) {
156160
const TEXT_COLOR: Color = Color::srgb(0.5, 0.5, 1.0);
157161
const FONT_SIZE: f32 = 32.0;
158162

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

219221
/// Converts point clicks into cell picking events.
@@ -294,17 +296,15 @@ fn init_symbols(
294296
commands
295297
.entity(trigger.target())
296298
.remove::<Interaction>()
297-
.with_children(|parent| {
298-
parent.spawn((
299-
Text::new(symbol.glyph()),
300-
TextFont {
301-
font: symbol_font.0.clone(),
302-
font_size: 65.0,
303-
..Default::default()
304-
},
305-
TextColor(symbol.color()),
306-
));
307-
});
299+
.with_child((
300+
Text::new(symbol.glyph()),
301+
TextFont {
302+
font: symbol_font.0.clone(),
303+
font_size: 65.0,
304+
..Default::default()
305+
},
306+
TextColor(symbol.color()),
307+
));
308308
}
309309

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

0 commit comments

Comments
 (0)