courier
Clone
https://git.tahinli.com/Tahinli/courier.git
0 watching·0 forks·Rust·AGPL-3.0·5.3 MiB·6d9eeb9·Tahinli·21 days ago·3 commits
| 1 | use crate::content; |
| 2 | use crate::game::{Game, Item, Npc, Obj, Talk}; |
| 3 | use crate::player::{Nearby, Pad}; |
| 4 | use bevy::color::Alpha; |
| 5 | use bevy::picking::hover::Hovered; |
| 6 | use bevy::prelude::*; |
| 7 | use bevy::ui_widgets::{Activate, Button}; |
| 8 | |
| 9 | const PARCH: Color = Color::srgb(0.95, 0.9, 0.78); |
| 10 | const MUTED: Color = Color::srgba(0.95, 0.9, 0.78, 0.6); |
| 11 | const GOLD: Color = Color::srgb(0.92, 0.75, 0.35); |
| 12 | const PANEL: Color = Color::srgba(0.09, 0.07, 0.07, 0.92); |
| 13 | const EDGE: Color = Color::srgb(0.78, 0.68, 0.5); |
| 14 | const BTN: Color = Color::srgba(0.09, 0.07, 0.07, 0.92); |
| 15 | const BTN_HOVER: Color = Color::srgba(0.3, 0.24, 0.18, 0.95); |
| 16 | const DOT_OFF: Color = Color::srgb(0.25, 0.22, 0.18); |
| 17 | const RED: Color = Color::srgb(0.8, 0.2, 0.2); |
| 18 | const AMBER: Color = Color::srgb(0.95, 0.68, 0.2); |
| 19 | const BLUE: Color = Color::srgb(0.35, 0.55, 0.9); |
| 20 | const WARM: Color = Color::srgb(0.9, 0.35, 0.15); |
| 21 | const LANTERN: Color = Color::srgb(1.0, 0.85, 0.2); |
| 22 | const BAR_BG: Color = Color::srgb(0.03, 0.025, 0.025); |
| 23 | |
| 24 | const BIG: f32 = 32.0; |
| 25 | const SMALL: f32 = 16.0; |
| 26 | const ICON: f32 = 8.0; |
| 27 | const BORDER: f32 = 2.0; |
| 28 | const PAD: f32 = 8.0; |
| 29 | |
| 30 | const Z_FADE: i32 = 10; |
| 31 | const Z_HUD: i32 = 20; |
| 32 | const Z_TOAST: i32 = 25; |
| 33 | const Z_DIALOG: i32 = 30; |
| 34 | |
| 35 | const TOAST_LIFE: f32 = 2.5; |
| 36 | const MAX_TOASTS: usize = 4; |
| 37 | |
| 38 | pub struct UiPlugin; |
| 39 | |
| 40 | impl Plugin for UiPlugin { |
| 41 | fn build(&self, app: &mut App) { |
| 42 | app.init_resource::<Fade>() |
| 43 | .init_resource::<Picked>() |
| 44 | .init_resource::<DialogUi>() |
| 45 | .init_resource::<InvUi>() |
| 46 | .add_observer(on_choice) |
| 47 | .add_systems(Startup, setup) |
| 48 | .add_systems(Update, (hud, lantern, prompt, dialog, choice_hover, toasts, inventory, fade)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[derive(Resource, Default)] |
| 53 | pub struct Fade(pub f32); |
| 54 | |
| 55 | #[derive(Resource)] |
| 56 | struct UiFont(Handle<Font>); |
| 57 | |
| 58 | #[derive(Resource, Default)] |
| 59 | struct Picked(Option<usize>); |
| 60 | |
| 61 | #[derive(Resource, Default)] |
| 62 | struct DialogUi { |
| 63 | shown: Option<(Talk, &'static str)>, |
| 64 | dirty: bool, |
| 65 | root: Option<Entity>, |
| 66 | } |
| 67 | |
| 68 | #[derive(Resource, Default)] |
| 69 | struct InvUi { |
| 70 | shown: Vec<(Item, usize)>, |
| 71 | root: Option<Entity>, |
| 72 | } |
| 73 | |
| 74 | #[derive(Component, Clone, Copy)] |
| 75 | enum HudText { |
| 76 | Clock, |
| 77 | Zone, |
| 78 | Money, |
| 79 | } |
| 80 | |
| 81 | #[derive(Component, Clone, Copy)] |
| 82 | enum Need { |
| 83 | Hunger, |
| 84 | Energy, |
| 85 | Warmth, |
| 86 | } |
| 87 | |
| 88 | #[derive(Component)] |
| 89 | struct NeedFill(Need); |
| 90 | |
| 91 | #[derive(Component)] |
| 92 | struct Lantern; |
| 93 | |
| 94 | #[derive(Component)] |
| 95 | struct FadeOverlay; |
| 96 | |
| 97 | #[derive(Component)] |
| 98 | struct PromptRoot; |
| 99 | |
| 100 | #[derive(Component)] |
| 101 | struct PromptText; |
| 102 | |
| 103 | #[derive(Component)] |
| 104 | struct ToastRoot; |
| 105 | |
| 106 | #[derive(Component)] |
| 107 | struct Toast { |
| 108 | age: f32, |
| 109 | } |
| 110 | |
| 111 | #[derive(Component)] |
| 112 | struct ChoiceBtn(usize); |
| 113 | |
| 114 | fn text(s: impl Into<String>, size: f32, color: Color, font: &Handle<Font>) -> impl Bundle { |
| 115 | (Text::new(s), TextFont::from_font_size(size).with_font(font.clone()), TextColor(color)) |
| 116 | } |
| 117 | |
| 118 | fn frame(node: Node) -> impl Bundle { |
| 119 | ( |
| 120 | Node { |
| 121 | border: UiRect::all(Val::Px(BORDER)), |
| 122 | padding: UiRect::all(Val::Px(PAD)), |
| 123 | ..node |
| 124 | }, |
| 125 | BackgroundColor(PANEL), |
| 126 | BorderColor::all(EDGE), |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | fn icon(color: Color) -> impl Bundle { |
| 131 | ( |
| 132 | Node { |
| 133 | width: Val::Px(ICON), |
| 134 | height: Val::Px(ICON), |
| 135 | ..default() |
| 136 | }, |
| 137 | BackgroundColor(color), |
| 138 | ) |
| 139 | } |
| 140 | |
| 141 | fn setup(mut commands: Commands, assets: Res<AssetServer>) { |
| 142 | let font = assets.load::<Font>("fonts/pixel.ttf"); |
| 143 | commands.insert_resource(UiFont(font.clone())); |
| 144 | |
| 145 | commands.spawn(( |
| 146 | FadeOverlay, |
| 147 | Node { |
| 148 | position_type: PositionType::Absolute, |
| 149 | left: Val::Px(0.0), |
| 150 | right: Val::Px(0.0), |
| 151 | top: Val::Px(0.0), |
| 152 | bottom: Val::Px(0.0), |
| 153 | ..default() |
| 154 | }, |
| 155 | BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.0)), |
| 156 | GlobalZIndex(Z_FADE), |
| 157 | )); |
| 158 | |
| 159 | commands.spawn(( |
| 160 | ToastRoot, |
| 161 | Node { |
| 162 | position_type: PositionType::Absolute, |
| 163 | left: Val::Px(0.0), |
| 164 | right: Val::Px(0.0), |
| 165 | top: Val::Px(64.0), |
| 166 | flex_direction: FlexDirection::Column, |
| 167 | align_items: AlignItems::Center, |
| 168 | row_gap: Val::Px(4.0), |
| 169 | ..default() |
| 170 | }, |
| 171 | GlobalZIndex(Z_TOAST), |
| 172 | )); |
| 173 | |
| 174 | commands |
| 175 | .spawn(( |
| 176 | frame(Node { |
| 177 | position_type: PositionType::Absolute, |
| 178 | left: Val::Px(16.0), |
| 179 | top: Val::Px(16.0), |
| 180 | flex_direction: FlexDirection::Column, |
| 181 | row_gap: Val::Px(4.0), |
| 182 | ..default() |
| 183 | }), |
| 184 | GlobalZIndex(Z_HUD), |
| 185 | )) |
| 186 | .with_children(|p| { |
| 187 | p.spawn(Node { |
| 188 | align_items: AlignItems::Center, |
| 189 | column_gap: Val::Px(6.0), |
| 190 | ..default() |
| 191 | }) |
| 192 | .with_children(|r| { |
| 193 | r.spawn((HudText::Clock, text("", SMALL, PARCH, &font))); |
| 194 | r.spawn(( |
| 195 | Lantern, |
| 196 | Node { |
| 197 | width: Val::Px(ICON), |
| 198 | height: Val::Px(ICON), |
| 199 | ..default() |
| 200 | }, |
| 201 | BackgroundColor(LANTERN), |
| 202 | Visibility::Hidden, |
| 203 | )); |
| 204 | }); |
| 205 | p.spawn((HudText::Zone, text("", SMALL, MUTED, &font))); |
| 206 | }); |
| 207 | |
| 208 | commands |
| 209 | .spawn(( |
| 210 | frame(Node { |
| 211 | position_type: PositionType::Absolute, |
| 212 | right: Val::Px(16.0), |
| 213 | top: Val::Px(16.0), |
| 214 | align_items: AlignItems::Center, |
| 215 | column_gap: Val::Px(8.0), |
| 216 | ..default() |
| 217 | }), |
| 218 | GlobalZIndex(Z_HUD), |
| 219 | )) |
| 220 | .with_children(|p| { |
| 221 | p.spawn(icon(GOLD)); |
| 222 | p.spawn((HudText::Money, text("", BIG, PARCH, &font))); |
| 223 | }); |
| 224 | |
| 225 | commands |
| 226 | .spawn(( |
| 227 | frame(Node { |
| 228 | position_type: PositionType::Absolute, |
| 229 | left: Val::Px(16.0), |
| 230 | bottom: Val::Px(16.0), |
| 231 | flex_direction: FlexDirection::Column, |
| 232 | row_gap: Val::Px(6.0), |
| 233 | ..default() |
| 234 | }), |
| 235 | GlobalZIndex(Z_HUD), |
| 236 | )) |
| 237 | .with_children(|p| { |
| 238 | for (kind, color) in [(Need::Hunger, AMBER), (Need::Energy, BLUE), (Need::Warmth, WARM)] { |
| 239 | p.spawn(Node { |
| 240 | align_items: AlignItems::Center, |
| 241 | column_gap: Val::Px(8.0), |
| 242 | ..default() |
| 243 | }) |
| 244 | .with_children(|r| { |
| 245 | r.spawn(icon(color)); |
| 246 | r.spawn(( |
| 247 | Node { |
| 248 | width: Val::Px(96.0), |
| 249 | height: Val::Px(6.0), |
| 250 | ..default() |
| 251 | }, |
| 252 | BackgroundColor(BAR_BG), |
| 253 | )) |
| 254 | .with_children(|b| { |
| 255 | b.spawn(( |
| 256 | Node { |
| 257 | position_type: PositionType::Absolute, |
| 258 | left: Val::Px(0.0), |
| 259 | top: Val::Px(0.0), |
| 260 | bottom: Val::Px(0.0), |
| 261 | width: Val::Percent(100.0), |
| 262 | ..default() |
| 263 | }, |
| 264 | BackgroundColor(color), |
| 265 | NeedFill(kind), |
| 266 | )); |
| 267 | }); |
| 268 | }); |
| 269 | } |
| 270 | }); |
| 271 | |
| 272 | commands |
| 273 | .spawn(( |
| 274 | PromptRoot, |
| 275 | Node { |
| 276 | position_type: PositionType::Absolute, |
| 277 | left: Val::Px(0.0), |
| 278 | right: Val::Px(0.0), |
| 279 | bottom: Val::Px(28.0), |
| 280 | justify_content: JustifyContent::Center, |
| 281 | ..default() |
| 282 | }, |
| 283 | Visibility::Hidden, |
| 284 | GlobalZIndex(Z_HUD), |
| 285 | )) |
| 286 | .with_children(|p| { |
| 287 | p.spawn(frame(Node::default())).with_children(|b| { |
| 288 | b.spawn((PromptText, text("", SMALL, PARCH, &font))); |
| 289 | }); |
| 290 | }); |
| 291 | } |
| 292 | |
| 293 | fn on_choice(ev: On<Activate>, q: Query<&ChoiceBtn>, mut picked: ResMut<Picked>) { |
| 294 | if let Ok(c) = q.get(ev.entity) { |
| 295 | picked.0 = Some(c.0); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | fn hud(g: Res<Game>, mut texts: Query<(&mut Text, &HudText)>, mut fills: Query<(&mut Node, &NeedFill)>) { |
| 300 | for (mut t, k) in &mut texts { |
| 301 | let s = match k { |
| 302 | HudText::Clock => g.clock(), |
| 303 | HudText::Zone => g.zone.name().to_string(), |
| 304 | HudText::Money => g.money.to_string(), |
| 305 | }; |
| 306 | if t.0 != s { |
| 307 | t.0 = s; |
| 308 | } |
| 309 | } |
| 310 | for (mut n, f) in &mut fills { |
| 311 | let v = match f.0 { |
| 312 | Need::Hunger => g.hunger, |
| 313 | Need::Energy => g.energy, |
| 314 | Need::Warmth => g.warmth, |
| 315 | }; |
| 316 | let w = Val::Percent(v.clamp(0.0, 100.0)); |
| 317 | if n.width != w { |
| 318 | n.width = w; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | fn lantern(g: Res<Game>, mut q: Query<&mut Visibility, With<Lantern>>) { |
| 324 | let vis = if g.flag("lantern") { Visibility::Inherited } else { Visibility::Hidden }; |
| 325 | for mut v in &mut q { |
| 326 | if *v != vis { |
| 327 | *v = vis; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | fn prompt_label(t: Talk) -> String { |
| 333 | match t { |
| 334 | Talk::Npc(n) => n.name().to_string(), |
| 335 | Talk::Obj(o) => match o { |
| 336 | Obj::HomeBed => "Home", |
| 337 | Obj::BalePile => "Cotton bales", |
| 338 | Obj::BaleDrop => "Mill door", |
| 339 | Obj::MillInn => "The Weavers' Arms", |
| 340 | Obj::CoalInn => "The Black Horse", |
| 341 | Obj::Signpost => "Signpost", |
| 342 | } |
| 343 | .to_string(), |
| 344 | Talk::Ev(_) => "?".to_string(), |
| 345 | Talk::Camp => "Camp".to_string(), |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | fn prompt( |
| 350 | g: Res<Game>, |
| 351 | near: Res<Nearby>, |
| 352 | mut roots: Query<&mut Visibility, With<PromptRoot>>, |
| 353 | mut texts: Query<&mut Text, With<PromptText>>, |
| 354 | ) { |
| 355 | let label = near.0.filter(|_| g.dialog.is_none() && !g.camping).map(prompt_label); |
| 356 | let vis = if label.is_some() { Visibility::Inherited } else { Visibility::Hidden }; |
| 357 | for mut v in &mut roots { |
| 358 | if *v != vis { |
| 359 | *v = vis; |
| 360 | } |
| 361 | } |
| 362 | if let Some(l) = label { |
| 363 | for mut t in &mut texts { |
| 364 | let s = format!("[E] {l}"); |
| 365 | if t.0 != s { |
| 366 | t.0 = s; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | fn bonded(n: Npc) -> bool { |
| 373 | matches!(n, Npc::Hale | Npc::Pike | Npc::Bess | Npc::Edwin | Npc::OldTom | Npc::Annie) |
| 374 | } |
| 375 | |
| 376 | fn dialog( |
| 377 | mut commands: Commands, |
| 378 | mut g: ResMut<Game>, |
| 379 | pad: Res<Pad>, |
| 380 | mut picked: ResMut<Picked>, |
| 381 | mut ui: ResMut<DialogUi>, |
| 382 | font: Res<UiFont>, |
| 383 | ) { |
| 384 | let input = pad.choice.or(picked.0.take()); |
| 385 | if let (Some(i), Some((talk, node))) = (input, g.dialog) { |
| 386 | let mut d = content::talk(talk, node, &g); |
| 387 | if d.choices.is_empty() { |
| 388 | if i == 0 { |
| 389 | g.dialog = None; |
| 390 | } |
| 391 | } else if i < d.choices.len() { |
| 392 | let c = d.choices.swap_remove(i); |
| 393 | g.dialog = c.next.map(|n| (talk, n)); |
| 394 | if let Some(f) = c.effect { |
| 395 | f(&mut g); |
| 396 | } |
| 397 | ui.dirty = true; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | let Some((talk, node)) = g.dialog else { |
| 402 | if let Some(root) = ui.root.take() { |
| 403 | commands.entity(root).despawn(); |
| 404 | } |
| 405 | ui.shown = None; |
| 406 | ui.dirty = false; |
| 407 | return; |
| 408 | }; |
| 409 | if ui.root.is_some() && ui.shown == Some((talk, node)) && !ui.dirty { |
| 410 | return; |
| 411 | } |
| 412 | if let Some(root) = ui.root.take() { |
| 413 | commands.entity(root).despawn(); |
| 414 | } |
| 415 | |
| 416 | let d = content::talk(talk, node, &g); |
| 417 | let hearts = match talk { |
| 418 | Talk::Npc(n) if bonded(n) => Some(g.heart(n)), |
| 419 | _ => None, |
| 420 | }; |
| 421 | let labels: Vec<String> = if d.choices.is_empty() { |
| 422 | vec!["Continue".to_string()] |
| 423 | } else { |
| 424 | d.choices.iter().map(|c| c.label.clone()).collect() |
| 425 | }; |
| 426 | let font = &font.0; |
| 427 | |
| 428 | let root = commands |
| 429 | .spawn(( |
| 430 | Node { |
| 431 | position_type: PositionType::Absolute, |
| 432 | left: Val::Px(0.0), |
| 433 | right: Val::Px(0.0), |
| 434 | bottom: Val::Px(24.0), |
| 435 | justify_content: JustifyContent::Center, |
| 436 | ..default() |
| 437 | }, |
| 438 | GlobalZIndex(Z_DIALOG), |
| 439 | )) |
| 440 | .with_children(|r| { |
| 441 | r.spawn(frame(Node { |
| 442 | width: Val::Px(1000.0), |
| 443 | flex_direction: FlexDirection::Column, |
| 444 | padding: UiRect::all(Val::Px(16.0)), |
| 445 | row_gap: Val::Px(8.0), |
| 446 | ..default() |
| 447 | })) |
| 448 | .with_children(|p| { |
| 449 | p.spawn(Node { |
| 450 | align_items: AlignItems::Center, |
| 451 | column_gap: Val::Px(8.0), |
| 452 | ..default() |
| 453 | }) |
| 454 | .with_children(|s| { |
| 455 | s.spawn(text(d.speaker.clone(), BIG, GOLD, font)); |
| 456 | if let Some(h) = hearts { |
| 457 | for k in 0..5 { |
| 458 | s.spawn(icon(if k < h { RED } else { DOT_OFF })); |
| 459 | } |
| 460 | } |
| 461 | }); |
| 462 | p.spawn(text(d.text.clone(), BIG, PARCH, font)); |
| 463 | for (i, label) in labels.iter().enumerate() { |
| 464 | p.spawn(( |
| 465 | Button, |
| 466 | Hovered::default(), |
| 467 | ChoiceBtn(i), |
| 468 | Node { |
| 469 | padding: UiRect::axes(Val::Px(PAD), Val::Px(4.0)), |
| 470 | ..default() |
| 471 | }, |
| 472 | BackgroundColor(BTN), |
| 473 | )) |
| 474 | .with_children(|b| { |
| 475 | b.spawn(text(format!("{} {}", i + 1, label), BIG, PARCH, font)); |
| 476 | }); |
| 477 | } |
| 478 | }); |
| 479 | }) |
| 480 | .id(); |
| 481 | |
| 482 | ui.root = Some(root); |
| 483 | ui.shown = Some((talk, node)); |
| 484 | ui.dirty = false; |
| 485 | } |
| 486 | |
| 487 | fn choice_hover(mut q: Query<(&Hovered, &mut BackgroundColor), With<ChoiceBtn>>) { |
| 488 | for (h, mut bg) in &mut q { |
| 489 | let c = if h.get() { BTN_HOVER } else { BTN }; |
| 490 | if bg.0 != c { |
| 491 | bg.0 = c; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | fn toasts( |
| 497 | mut commands: Commands, |
| 498 | time: Res<Time>, |
| 499 | mut g: ResMut<Game>, |
| 500 | font: Res<UiFont>, |
| 501 | roots: Query<Entity, With<ToastRoot>>, |
| 502 | mut q: Query<(Entity, &mut Toast, &mut TextColor, &mut BackgroundColor, &mut BorderColor)>, |
| 503 | ) { |
| 504 | let Ok(root) = roots.single() else { return }; |
| 505 | let dt = time.delta_secs(); |
| 506 | let texts = if g.toasts.is_empty() { Vec::new() } else { std::mem::take(&mut g.toasts) }; |
| 507 | |
| 508 | let mut live = Vec::new(); |
| 509 | for (e, mut t, mut tc, mut bg, mut bc) in &mut q { |
| 510 | t.age += dt; |
| 511 | if t.age >= TOAST_LIFE { |
| 512 | commands.entity(e).despawn(); |
| 513 | continue; |
| 514 | } |
| 515 | let a = 1.0 - t.age / TOAST_LIFE; |
| 516 | tc.0 = PARCH.with_alpha(a); |
| 517 | bg.0 = PANEL.with_alpha(0.92 * a); |
| 518 | *bc = BorderColor::all(EDGE.with_alpha(a)); |
| 519 | live.push((t.age, e)); |
| 520 | } |
| 521 | |
| 522 | live.sort_by(|a, b| b.0.total_cmp(&a.0)); |
| 523 | let fresh = texts.len().min(MAX_TOASTS); |
| 524 | for (_, e) in live.iter().take((live.len() + fresh).saturating_sub(MAX_TOASTS)) { |
| 525 | commands.entity(*e).despawn(); |
| 526 | } |
| 527 | |
| 528 | let skip = texts.len() - fresh; |
| 529 | for s in texts.into_iter().skip(skip) { |
| 530 | commands.spawn(( |
| 531 | Toast { age: 0.0 }, |
| 532 | ChildOf(root), |
| 533 | frame(Node::default()), |
| 534 | Text::new(s), |
| 535 | TextFont::from_font_size(SMALL).with_font(font.0.clone()), |
| 536 | TextColor(PARCH), |
| 537 | )); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | fn inventory( |
| 542 | mut commands: Commands, |
| 543 | g: Res<Game>, |
| 544 | pad: Res<Pad>, |
| 545 | font: Res<UiFont>, |
| 546 | mut ui: ResMut<InvUi>, |
| 547 | mut open: Local<bool>, |
| 548 | ) { |
| 549 | if pad.inventory { |
| 550 | *open = !*open; |
| 551 | } |
| 552 | let mut items: Vec<(Item, usize)> = Vec::new(); |
| 553 | for &i in &g.items { |
| 554 | match items.iter_mut().find(|(x, _)| *x == i) { |
| 555 | Some(e) => e.1 += 1, |
| 556 | None => items.push((i, 1)), |
| 557 | } |
| 558 | } |
| 559 | let stale = if *open { ui.root.is_none() || ui.shown != items } else { ui.root.is_some() }; |
| 560 | if !stale { |
| 561 | return; |
| 562 | } |
| 563 | if let Some(root) = ui.root.take() { |
| 564 | commands.entity(root).despawn(); |
| 565 | } |
| 566 | if *open { |
| 567 | let font = &font.0; |
| 568 | let root = commands |
| 569 | .spawn(( |
| 570 | frame(Node { |
| 571 | position_type: PositionType::Absolute, |
| 572 | right: Val::Px(16.0), |
| 573 | top: Val::Px(72.0), |
| 574 | width: Val::Px(320.0), |
| 575 | flex_direction: FlexDirection::Row, |
| 576 | flex_wrap: FlexWrap::Wrap, |
| 577 | column_gap: Val::Px(12.0), |
| 578 | row_gap: Val::Px(4.0), |
| 579 | ..default() |
| 580 | }), |
| 581 | GlobalZIndex(Z_HUD), |
| 582 | )) |
| 583 | .with_children(|p| { |
| 584 | for (item, n) in &items { |
| 585 | let s = if *n > 1 { format!("{} x{n}", item.name()) } else { item.name().to_string() }; |
| 586 | p.spawn(Node { |
| 587 | width: Val::Percent(45.0), |
| 588 | ..default() |
| 589 | }) |
| 590 | .with_children(|c| { |
| 591 | c.spawn(text(s, SMALL, PARCH, font)); |
| 592 | }); |
| 593 | } |
| 594 | }) |
| 595 | .id(); |
| 596 | ui.root = Some(root); |
| 597 | ui.shown = items; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | fn fade(time: Res<Time>, target: Res<Fade>, mut shown: Local<f32>, mut q: Query<&mut BackgroundColor, With<FadeOverlay>>) { |
| 602 | let k = 1.0 - (-time.delta_secs() * 6.0).exp(); |
| 603 | *shown += (target.0.clamp(0.0, 1.0) - *shown) * k; |
| 604 | for mut bg in &mut q { |
| 605 | bg.0 = Color::srgba(0.0, 0.0, 0.0, *shown); |
| 606 | } |
| 607 | } |