use crate::content; use crate::game::{Game, Item, Npc, Obj, Talk}; use crate::player::{Nearby, Pad}; use bevy::color::Alpha; use bevy::picking::hover::Hovered; use bevy::prelude::*; use bevy::ui_widgets::{Activate, Button}; const PARCH: Color = Color::srgb(0.95, 0.9, 0.78); const MUTED: Color = Color::srgba(0.95, 0.9, 0.78, 0.6); const GOLD: Color = Color::srgb(0.92, 0.75, 0.35); const PANEL: Color = Color::srgba(0.09, 0.07, 0.07, 0.92); const EDGE: Color = Color::srgb(0.78, 0.68, 0.5); const BTN: Color = Color::srgba(0.09, 0.07, 0.07, 0.92); const BTN_HOVER: Color = Color::srgba(0.3, 0.24, 0.18, 0.95); const DOT_OFF: Color = Color::srgb(0.25, 0.22, 0.18); const RED: Color = Color::srgb(0.8, 0.2, 0.2); const AMBER: Color = Color::srgb(0.95, 0.68, 0.2); const BLUE: Color = Color::srgb(0.35, 0.55, 0.9); const WARM: Color = Color::srgb(0.9, 0.35, 0.15); const LANTERN: Color = Color::srgb(1.0, 0.85, 0.2); const BAR_BG: Color = Color::srgb(0.03, 0.025, 0.025); const BIG: f32 = 32.0; const SMALL: f32 = 16.0; const ICON: f32 = 8.0; const BORDER: f32 = 2.0; const PAD: f32 = 8.0; const Z_FADE: i32 = 10; const Z_HUD: i32 = 20; const Z_TOAST: i32 = 25; const Z_DIALOG: i32 = 30; const TOAST_LIFE: f32 = 2.5; const MAX_TOASTS: usize = 4; pub struct UiPlugin; impl Plugin for UiPlugin { fn build(&self, app: &mut App) { app.init_resource::() .init_resource::() .init_resource::() .init_resource::() .add_observer(on_choice) .add_systems(Startup, setup) .add_systems(Update, (hud, lantern, prompt, dialog, choice_hover, toasts, inventory, fade)); } } #[derive(Resource, Default)] pub struct Fade(pub f32); #[derive(Resource)] struct UiFont(Handle); #[derive(Resource, Default)] struct Picked(Option); #[derive(Resource, Default)] struct DialogUi { shown: Option<(Talk, &'static str)>, dirty: bool, root: Option, } #[derive(Resource, Default)] struct InvUi { shown: Vec<(Item, usize)>, root: Option, } #[derive(Component, Clone, Copy)] enum HudText { Clock, Zone, Money, } #[derive(Component, Clone, Copy)] enum Need { Hunger, Energy, Warmth, } #[derive(Component)] struct NeedFill(Need); #[derive(Component)] struct Lantern; #[derive(Component)] struct FadeOverlay; #[derive(Component)] struct PromptRoot; #[derive(Component)] struct PromptText; #[derive(Component)] struct ToastRoot; #[derive(Component)] struct Toast { age: f32, } #[derive(Component)] struct ChoiceBtn(usize); fn text(s: impl Into, size: f32, color: Color, font: &Handle) -> impl Bundle { (Text::new(s), TextFont::from_font_size(size).with_font(font.clone()), TextColor(color)) } fn frame(node: Node) -> impl Bundle { ( Node { border: UiRect::all(Val::Px(BORDER)), padding: UiRect::all(Val::Px(PAD)), ..node }, BackgroundColor(PANEL), BorderColor::all(EDGE), ) } fn icon(color: Color) -> impl Bundle { ( Node { width: Val::Px(ICON), height: Val::Px(ICON), ..default() }, BackgroundColor(color), ) } fn setup(mut commands: Commands, assets: Res) { let font = assets.load::("fonts/pixel.ttf"); commands.insert_resource(UiFont(font.clone())); commands.spawn(( FadeOverlay, Node { position_type: PositionType::Absolute, left: Val::Px(0.0), right: Val::Px(0.0), top: Val::Px(0.0), bottom: Val::Px(0.0), ..default() }, BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.0)), GlobalZIndex(Z_FADE), )); commands.spawn(( ToastRoot, Node { position_type: PositionType::Absolute, left: Val::Px(0.0), right: Val::Px(0.0), top: Val::Px(64.0), flex_direction: FlexDirection::Column, align_items: AlignItems::Center, row_gap: Val::Px(4.0), ..default() }, GlobalZIndex(Z_TOAST), )); commands .spawn(( frame(Node { position_type: PositionType::Absolute, left: Val::Px(16.0), top: Val::Px(16.0), flex_direction: FlexDirection::Column, row_gap: Val::Px(4.0), ..default() }), GlobalZIndex(Z_HUD), )) .with_children(|p| { p.spawn(Node { align_items: AlignItems::Center, column_gap: Val::Px(6.0), ..default() }) .with_children(|r| { r.spawn((HudText::Clock, text("", SMALL, PARCH, &font))); r.spawn(( Lantern, Node { width: Val::Px(ICON), height: Val::Px(ICON), ..default() }, BackgroundColor(LANTERN), Visibility::Hidden, )); }); p.spawn((HudText::Zone, text("", SMALL, MUTED, &font))); }); commands .spawn(( frame(Node { position_type: PositionType::Absolute, right: Val::Px(16.0), top: Val::Px(16.0), align_items: AlignItems::Center, column_gap: Val::Px(8.0), ..default() }), GlobalZIndex(Z_HUD), )) .with_children(|p| { p.spawn(icon(GOLD)); p.spawn((HudText::Money, text("", BIG, PARCH, &font))); }); commands .spawn(( frame(Node { position_type: PositionType::Absolute, left: Val::Px(16.0), bottom: Val::Px(16.0), flex_direction: FlexDirection::Column, row_gap: Val::Px(6.0), ..default() }), GlobalZIndex(Z_HUD), )) .with_children(|p| { for (kind, color) in [(Need::Hunger, AMBER), (Need::Energy, BLUE), (Need::Warmth, WARM)] { p.spawn(Node { align_items: AlignItems::Center, column_gap: Val::Px(8.0), ..default() }) .with_children(|r| { r.spawn(icon(color)); r.spawn(( Node { width: Val::Px(96.0), height: Val::Px(6.0), ..default() }, BackgroundColor(BAR_BG), )) .with_children(|b| { b.spawn(( Node { position_type: PositionType::Absolute, left: Val::Px(0.0), top: Val::Px(0.0), bottom: Val::Px(0.0), width: Val::Percent(100.0), ..default() }, BackgroundColor(color), NeedFill(kind), )); }); }); } }); commands .spawn(( PromptRoot, Node { position_type: PositionType::Absolute, left: Val::Px(0.0), right: Val::Px(0.0), bottom: Val::Px(28.0), justify_content: JustifyContent::Center, ..default() }, Visibility::Hidden, GlobalZIndex(Z_HUD), )) .with_children(|p| { p.spawn(frame(Node::default())).with_children(|b| { b.spawn((PromptText, text("", SMALL, PARCH, &font))); }); }); } fn on_choice(ev: On, q: Query<&ChoiceBtn>, mut picked: ResMut) { if let Ok(c) = q.get(ev.entity) { picked.0 = Some(c.0); } } fn hud(g: Res, mut texts: Query<(&mut Text, &HudText)>, mut fills: Query<(&mut Node, &NeedFill)>) { for (mut t, k) in &mut texts { let s = match k { HudText::Clock => g.clock(), HudText::Zone => g.zone.name().to_string(), HudText::Money => g.money.to_string(), }; if t.0 != s { t.0 = s; } } for (mut n, f) in &mut fills { let v = match f.0 { Need::Hunger => g.hunger, Need::Energy => g.energy, Need::Warmth => g.warmth, }; let w = Val::Percent(v.clamp(0.0, 100.0)); if n.width != w { n.width = w; } } } fn lantern(g: Res, mut q: Query<&mut Visibility, With>) { let vis = if g.flag("lantern") { Visibility::Inherited } else { Visibility::Hidden }; for mut v in &mut q { if *v != vis { *v = vis; } } } fn prompt_label(t: Talk) -> String { match t { Talk::Npc(n) => n.name().to_string(), Talk::Obj(o) => match o { Obj::HomeBed => "Home", Obj::BalePile => "Cotton bales", Obj::BaleDrop => "Mill door", Obj::MillInn => "The Weavers' Arms", Obj::CoalInn => "The Black Horse", Obj::Signpost => "Signpost", } .to_string(), Talk::Ev(_) => "?".to_string(), Talk::Camp => "Camp".to_string(), } } fn prompt( g: Res, near: Res, mut roots: Query<&mut Visibility, With>, mut texts: Query<&mut Text, With>, ) { let label = near.0.filter(|_| g.dialog.is_none() && !g.camping).map(prompt_label); let vis = if label.is_some() { Visibility::Inherited } else { Visibility::Hidden }; for mut v in &mut roots { if *v != vis { *v = vis; } } if let Some(l) = label { for mut t in &mut texts { let s = format!("[E] {l}"); if t.0 != s { t.0 = s; } } } } fn bonded(n: Npc) -> bool { matches!(n, Npc::Hale | Npc::Pike | Npc::Bess | Npc::Edwin | Npc::OldTom | Npc::Annie) } fn dialog( mut commands: Commands, mut g: ResMut, pad: Res, mut picked: ResMut, mut ui: ResMut, font: Res, ) { let input = pad.choice.or(picked.0.take()); if let (Some(i), Some((talk, node))) = (input, g.dialog) { let mut d = content::talk(talk, node, &g); if d.choices.is_empty() { if i == 0 { g.dialog = None; } } else if i < d.choices.len() { let c = d.choices.swap_remove(i); g.dialog = c.next.map(|n| (talk, n)); if let Some(f) = c.effect { f(&mut g); } ui.dirty = true; } } let Some((talk, node)) = g.dialog else { if let Some(root) = ui.root.take() { commands.entity(root).despawn(); } ui.shown = None; ui.dirty = false; return; }; if ui.root.is_some() && ui.shown == Some((talk, node)) && !ui.dirty { return; } if let Some(root) = ui.root.take() { commands.entity(root).despawn(); } let d = content::talk(talk, node, &g); let hearts = match talk { Talk::Npc(n) if bonded(n) => Some(g.heart(n)), _ => None, }; let labels: Vec = if d.choices.is_empty() { vec!["Continue".to_string()] } else { d.choices.iter().map(|c| c.label.clone()).collect() }; let font = &font.0; let root = commands .spawn(( Node { position_type: PositionType::Absolute, left: Val::Px(0.0), right: Val::Px(0.0), bottom: Val::Px(24.0), justify_content: JustifyContent::Center, ..default() }, GlobalZIndex(Z_DIALOG), )) .with_children(|r| { r.spawn(frame(Node { width: Val::Px(1000.0), flex_direction: FlexDirection::Column, padding: UiRect::all(Val::Px(16.0)), row_gap: Val::Px(8.0), ..default() })) .with_children(|p| { p.spawn(Node { align_items: AlignItems::Center, column_gap: Val::Px(8.0), ..default() }) .with_children(|s| { s.spawn(text(d.speaker.clone(), BIG, GOLD, font)); if let Some(h) = hearts { for k in 0..5 { s.spawn(icon(if k < h { RED } else { DOT_OFF })); } } }); p.spawn(text(d.text.clone(), BIG, PARCH, font)); for (i, label) in labels.iter().enumerate() { p.spawn(( Button, Hovered::default(), ChoiceBtn(i), Node { padding: UiRect::axes(Val::Px(PAD), Val::Px(4.0)), ..default() }, BackgroundColor(BTN), )) .with_children(|b| { b.spawn(text(format!("{} {}", i + 1, label), BIG, PARCH, font)); }); } }); }) .id(); ui.root = Some(root); ui.shown = Some((talk, node)); ui.dirty = false; } fn choice_hover(mut q: Query<(&Hovered, &mut BackgroundColor), With>) { for (h, mut bg) in &mut q { let c = if h.get() { BTN_HOVER } else { BTN }; if bg.0 != c { bg.0 = c; } } } fn toasts( mut commands: Commands, time: Res