|
3 | 3 |
|
4 | 4 | use std::fmt::{self, Formatter}; |
5 | 5 |
|
6 | | -use bevy::{platform::collections::HashMap, prelude::*}; |
| 6 | +use bevy::{ |
| 7 | + ecs::{relationship::RelatedSpawner, spawn::SpawnWith}, |
| 8 | + platform::collections::HashMap, |
| 9 | + prelude::*, |
| 10 | +}; |
7 | 11 | use bevy_replicon::prelude::*; |
8 | 12 | use bevy_replicon_example_backend::{ExampleClient, ExampleServer, RepliconExampleBackendPlugins}; |
9 | 13 | use clap::{Parser, ValueEnum}; |
@@ -156,64 +160,62 @@ fn setup_ui(mut commands: Commands, symbol_font: Res<SymbolFont>) { |
156 | 160 | const TEXT_COLOR: Color = Color::srgb(0.5, 0.5, 1.0); |
157 | 161 | const FONT_SIZE: f32 = 32.0; |
158 | 162 |
|
159 | | - commands |
160 | | - .spawn(Node { |
| 163 | + commands.spawn(( |
| 164 | + Node { |
161 | 165 | width: Val::Percent(100.0), |
162 | 166 | height: Val::Percent(100.0), |
163 | 167 | align_items: AlignItems::Center, |
164 | 168 | justify_content: JustifyContent::Center, |
165 | 169 | ..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, |
192 | 201 | ..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 | + )); |
217 | 219 | } |
218 | 220 |
|
219 | 221 | /// Converts point clicks into cell picking events. |
@@ -294,17 +296,15 @@ fn init_symbols( |
294 | 296 | commands |
295 | 297 | .entity(trigger.target()) |
296 | 298 | .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 | + )); |
308 | 308 | } |
309 | 309 |
|
310 | 310 | /// Sends cell and local player entities and starts the game. |
|
0 commit comments