pinch-points 0.1.0

A fast, kid-friendly crab-routing game: route streams of crabs into your sandcastle before the tide comes in
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! The match-setup screen: the rows, the keys that move them, and the seat
//! names typed into them.
//!
//! Split from the model the way settings is, so the dials and their wire
//! form stay readable without the UI wrapped around them.

use super::*;
use crate::app::i18n::fill;
use crate::app::menu_ui;
use crate::app::settings::GameSettings;

#[derive(Resource, Default)]
pub struct MatchMenu {
    pub selected: usize,
    /// The seat whose name is being typed, if any. While this is set the
    /// keyboard belongs to the name: navigation and launching wait.
    pub naming: Option<u8>,
}

/// A row of the card, by its index. What the row *is* rides on its cells:
/// the row node itself only ever folds away or takes the cursor's band.
#[derive(Component)]
pub struct MatchRow(pub usize);

/// One half of a row's text: the dial's name, or what it is set to.
#[derive(Component)]
pub struct MatchCell(pub usize, pub Row, pub menu_ui::Half);

#[derive(Component)]
pub struct MatchUi;

/// The controller-join footer: hint plus the joined list.
#[derive(Component)]
pub struct MatchPadInfo(pub bool);

/// The line under the card saying why the map dial is offering none of the
/// player's own beaches. Empty the rest of the time, and spawned either
/// way, so the footer below it does not shift when it appears.
#[derive(Component)]
pub struct MatchBeachNote;

pub fn enter_match_setup(
    mut commands: Commands,
    settings: Res<GameSettings>,
    mut menu: ResMut<MatchMenu>,
    mut config: ResMut<MatchConfig>,
    beaches: Res<CustomBeaches>,
) {
    menu.selected = 0;
    menu.naming = None;
    // The shelf may have changed since the last visit (a beach deleted, or
    // resaved with fewer castles): a config still pointing at it is moved
    // on, so the dial reads what will be played.
    crate::app::match_setup::settle_map(&mut config, &beaches);
    let tr = settings.tr();
    commands
        .spawn((MatchUi, menu_ui::between_bars()))
        .with_children(|wrap| {
            // A fixed height, because the rows that come and go fold away
            // rather than blanking: four AI-level rows in the middle of the
            // card would be four holes if they merely emptied, and a card
            // that shrinks around them jumps while you turn the dial that
            // adds them.
            let (mut node, fill, edge, shadow) = menu_ui::screen_card();
            node.height = Val::Px(
                2.0 * menu_ui::CARD_PAD_Y
                    + menu_ui::HEADING_H
                    + ROWS as f32 * (menu_ui::ROW_H + menu_ui::ROW_GAP),
            );
            node.justify_content = JustifyContent::FlexStart;
            wrap.spawn((node, fill, edge, shadow))
                .with_children(|card| {
                    card.spawn(menu_ui::heading(tr.match_heading, true));
                    for row in 0..ROWS {
                        card.spawn((MatchRow(row), menu_ui::card_row()))
                            .with_children(|line| {
                                for (half, width) in [
                                    (menu_ui::Half::Label, LABEL_W),
                                    (menu_ui::Half::Value, VALUE_W),
                                ] {
                                    line.spawn((
                                        MatchCell(row, Row::ALL[row], half),
                                        menu_ui::cell(width, ROW_FONT),
                                    ));
                                }
                            });
                    }
                });
            // The beach note sits first, right under the card and so under
            // the map row it is about, and in the parchment the rows use
            // rather than the controller footer's grey: it is about the
            // choice being made, not a standing hint.
            wrap.spawn((
                MatchBeachNote,
                // A row of its own height whether or not it has anything to
                // say: an empty text node collapses, and the controller
                // footer under it would step up the screen every time the
                // seat count crossed what the beaches can hold.
                Node {
                    height: Val::Px(20.0),
                    ..default()
                },
                Text::new(""),
                TextFont {
                    font_size: FontSize::Px(15.0),
                    ..default()
                },
                TextLayout::no_wrap(),
                TextColor(palette::PARCHMENT.with_alpha(0.65)),
            ));
            for is_list in [false, true] {
                wrap.spawn((
                    MatchPadInfo(is_list),
                    Text::new(""),
                    TextFont {
                        font_size: FontSize::Px(15.0),
                        ..default()
                    },
                    TextLayout::no_wrap(),
                    TextColor(palette::PARCHMENT.with_alpha(0.40)),
                ));
            }
        });
}

/// Label gutter and value gutter, the value including the `< >` the dial
/// wears. Two cells, the way every other card in the shell does it, rather
/// than one cell holding a label padded out with spaces.
///
/// Padding was `{:<15}`, and Rust counts that width in `char`s. Two scripts
/// break that. The Latin ones overrun it - "Niveau van de AI S1" is
/// nineteen characters - and pushed the dial right on those rows alone.
/// Japanese undershoots it and still comes out wider: the shipped CJK face
/// draws a full em where DejaVu Sans Mono draws 0.602, so a two-character
/// label padded to fifteen is 17 columns wide and a six-character one is
/// 21, and no number of spaces closes a gap that is 1.66 of one. A cell of
/// a fixed pixel width has no opinion about either.
///
/// Wide enough for the longest label and the longest built-in value in any
/// language, measured against those two faces. What the player types - a
/// seat name, a handmade beach's name - is not bounded by anything here and
/// clips, which is the same contract the settings card keeps.
pub(super) const LABEL_W: f32 = 220.0;
pub(super) const VALUE_W: f32 = 428.0;
pub(super) const ROW_FONT: f32 = 19.0;

/// Keep the controller footer current: the join hint, and who joined -
/// and the note about beaches this table has grown too big for.
pub fn update_match_pad_info(
    seats: Res<crate::app::gamepad::PadSeats>,
    config: Res<MatchConfig>,
    settings: Res<GameSettings>,
    beaches: Res<CustomBeaches>,
    mut rows: Query<(&MatchPadInfo, &mut Text)>,
    mut note: Query<&mut Text, (With<MatchBeachNote>, Without<MatchPadInfo>)>,
) {
    let tr = settings.tr();
    if let Ok(mut text) = note.single_mut() {
        let line = crate::app::match_setup::beaches_note(&config, tr, &beaches).unwrap_or_default();
        menu_ui::set_text(&mut text, &line);
    }
    let humans = config.seats - config.bots;
    for (info, mut text) in &mut rows {
        let line = if info.0 {
            if seats.0.is_empty() {
                String::new()
            } else {
                // Pad claim i drives the i-th human seat from the top.
                let list: Vec<String> = (0..seats.0.len())
                    .map(|i| {
                        let seat = humans.saturating_sub(1 + i as u8);
                        crate::app::seat_label(tr, seat)
                    })
                    .collect();
                fill(tr.match_pad_joined, &[("list", &list.join(", "))])
            }
        } else {
            tr.match_pad_hint.to_string()
        };
        menu_ui::set_text(&mut text, &line);
    }
}

/// The seat the `slot`-th AI row configures: the AI fills the top seats, so
/// slot 0 is the highest one. `None` once the slot is past the AI count.
pub(super) fn ai_seat(config: &MatchConfig, slot: u8) -> Option<u8> {
    (slot < config.bots).then(|| config.seats - 1 - slot)
}

/// Step the difficulty of the AI seat behind `slot`. A dead slot (the row
/// is hidden) changes nothing.
pub(super) fn cycle_ai_level(config: &mut MatchConfig, slot: u8, right: bool) {
    if let Some(seat) = ai_seat(config, slot) {
        config.bot_levels[seat as usize] = config.bot_levels[seat as usize].cycled(right);
    }
}

/// Which rows are showing right now: the AI-level rows appear one per AI
/// seat, so a two-human match has none of them.
pub(super) fn live_rows(config: &MatchConfig) -> [bool; ROWS] {
    std::array::from_fn(|row| match Row::ALL[row] {
        Row::BotLevel(slot) => ai_seat(config, slot).is_some(),
        Row::Name(seat) => seat < config.seats,
        Row::Players | Row::Bots | Row::Map | Row::Gulls | Row::Round | Row::Mode => true,
    })
}

#[allow(clippy::too_many_arguments)]
pub fn match_setup_input(
    keys: Res<ButtonInput<KeyCode>>,
    beaches: Res<CustomBeaches>,
    mut typed: MessageReader<bevy::input::keyboard::KeyboardInput>,
    mut menu: ResMut<MatchMenu>,
    mut config: ResMut<MatchConfig>,
    mut settings: ResMut<GameSettings>,
    mut tournament: ResMut<crate::app::tournament::Tournament>,
    mut next_screen: ResMut<NextState<Screen>>,
) {
    if let Some(seat) = menu.naming {
        type_a_name(seat, &mut typed, &keys, &mut settings, &mut menu);
        return;
    }
    // Nothing typed while not naming, but the reader must still be drained
    // or the first keystroke of a rename arrives with a backlog.
    typed.clear();
    if keys.just_pressed(KeyCode::Escape) {
        next_screen.set(Screen::Menu);
        return;
    }
    // Tab opens a name for typing, not Enter. Enter used to do it, and on
    // a name row that meant Enter could never do the other thing it says
    // it does: the name rows are last on the list, so a player who had
    // just named everyone pressed Enter to start and got the name box
    // again, and again.
    if keys.just_pressed(KeyCode::Tab)
        && let Row::Name(seat) = Row::ALL[menu.selected]
    {
        menu.naming = Some(seat);
        return;
    }
    if keys.just_pressed(KeyCode::Enter) {
        config.armed = true;
        *tournament = if config.series {
            crate::app::tournament::Tournament::start()
        } else {
            crate::app::tournament::Tournament::default()
        };
        next_screen.set(Screen::Versus);
        return;
    }
    menu.selected = menu_ui::nav_live(&keys, menu.selected, &live_rows(&config));
    let Some(right) = menu_ui::left_right(&keys) else {
        return;
    };
    match Row::ALL[menu.selected] {
        Row::Players => {
            let seats = i32::from(config.seats) + if right { 1 } else { -1 };
            config.seats = seats.clamp(2, MAX_PLAYERS as i32) as u8;
            config.bots = config.bots.min(config.seats - 1);
            // Five and six castles need a beach with room for them, and a
            // handmade beach only seats as many as it has castles: the map
            // follows the table.
            crate::app::match_setup::settle_map(&mut config, &beaches);
        }
        Row::Bots => {
            let bots = i32::from(config.bots) + if right { 1 } else { -1 };
            config.bots = bots.clamp(0, i32::from(config.seats) - 1) as u8;
        }
        Row::BotLevel(slot) => cycle_ai_level(&mut config, slot, right),
        Row::Map => {
            crate::app::match_setup::cycle_map(&mut config, right, &beaches);
            // Stepping down to a small beach drops the seats it cannot hold
            // rather than starting a match six players cannot all sit at.
            if config.map.size().0 < WIDE_ENOUGH {
                config.seats = config.seats.min(CLASSIC_SEATS);
                config.bots = config.bots.min(config.seats - 1);
            }
        }
        Row::Gulls => config.gulls = config.gulls.cycled(right),
        Row::Round => config.round = config.round.cycled(right),
        Row::Mode => config.series = !config.series,
        // A name is typed, not stepped through.
        Row::Name(_) => {}
    }
}

/// The keyboard belongs to one seat's name: characters land in it, Backspace
/// rubs one out, Enter or Esc hands the keyboard back. Bevy's `text` field
/// is used rather than the key codes so the player's own layout, and their
/// shift key, decide what a keystroke means.
fn type_a_name(
    seat: u8,
    typed: &mut MessageReader<bevy::input::keyboard::KeyboardInput>,
    keys: &ButtonInput<KeyCode>,
    settings: &mut GameSettings,
    menu: &mut MatchMenu,
) {
    for event in typed.read() {
        if !event.state.is_pressed() {
            continue;
        }
        // Written as a ladder rather than a match: the interesting keys are
        // three out of a hundred and fifty, and the rest are just text.
        let done = matches!(
            event.key_code,
            KeyCode::Enter | KeyCode::NumpadEnter | KeyCode::Escape | KeyCode::Tab
        );
        if matches!(event.key_code, KeyCode::Backspace | KeyCode::Delete) {
            settings.pop_name_char(seat);
        } else if !done {
            for ch in event.text.iter().flat_map(|text| text.chars()) {
                settings.push_name_char(seat, ch);
            }
        }
    }
    // Every way out of the name box puts it away and nothing else: Enter
    // does not also start the match here, or a player finishing a name
    // would find the round already running.
    let done = keys.just_pressed(KeyCode::Enter)
        || keys.just_pressed(KeyCode::NumpadEnter)
        || keys.just_pressed(KeyCode::Escape)
        || keys.just_pressed(KeyCode::Tab);
    if done {
        settings.tidy_name(seat);
        menu.naming = None;
    }
}

/// The two halves of a row: the dial's name, and what it is set to.
///
/// Pure, and separate from the system below, so every language's rows can
/// be measured against the cells that hold them without a `World`. The
/// value half carries its own `< >`, since the one row that is typed into
/// rather than stepped through wears neither.
pub(super) fn row_text(
    tr: &crate::app::i18n::Tr,
    config: &MatchConfig,
    settings: &GameSettings,
    beaches: &CustomBeaches,
    naming: Option<u8>,
    row: Row,
) -> (String, String) {
    let dial = |value: &str| format!("< {value} >");
    match row {
        Row::Players => (
            tr.match_players.to_string(),
            dial(&config.seats.to_string()),
        ),
        Row::Bots => {
            let humans = config.seats - config.bots;
            let rest = if humans == 1 {
                tr.human_one.to_string()
            } else {
                fill(tr.human_many, &[("n", &humans.to_string())])
            };
            (
                tr.match_ai.to_string(),
                format!("{}   ({rest})", dial(&config.bots.to_string())),
            )
        }
        Row::BotLevel(slot) => {
            let seat = ai_seat(config, slot).unwrap_or(0);
            let who = crate::app::seat_label(tr, seat);
            (
                format!("{} {who}", tr.match_ai_level),
                dial(tr.bot_levels[config.bot_levels[seat as usize].index()]),
            )
        }
        // What the dial is *not* offering rides under the card, not here:
        // the value is one fixed-width cell, and the sentence runs off the
        // end of it in every language.
        Row::Map => (
            tr.match_map.to_string(),
            dial(&crate::app::match_setup::map_label(config, tr, beaches)),
        ),
        Row::Gulls => (
            tr.match_gulls.to_string(),
            dial(tr.gull_names[config.gulls.index()]),
        ),
        Row::Round => (
            tr.match_round.to_string(),
            dial(tr.round_names[config.round.index()]),
        ),
        Row::Mode => (
            tr.match_mode.to_string(),
            dial(tr.mode_names[usize::from(config.series)]),
        ),
        Row::Name(seat) => {
            let who = crate::app::seat_label(tr, seat);
            let given = settings.names[usize::from(seat)].clone();
            let value = if naming == Some(seat) {
                // A caret says the keyboard is being typed into rather than
                // navigating; the row is its own instructions.
                format!("{given}_   {}", tr.match_name_typing)
            } else if given.is_empty() {
                tr.match_name_empty.to_string()
            } else {
                given
            };
            (format!("{} {who}", tr.match_name), value)
        }
    }
}

pub fn update_match_ui(
    config: Res<MatchConfig>,
    menu: Res<MatchMenu>,
    settings: Res<GameSettings>,
    beaches: Res<CustomBeaches>,
    mut cells: Query<(&MatchCell, &mut Text, &mut TextColor)>,
    mut rows: Query<(&MatchRow, &mut BackgroundColor, &mut Node)>,
) {
    let tr = settings.tr();
    // One source of truth for which rows apply right now, shared with the
    // navigation: a row the cursor cannot reach must not be on screen.
    let live = live_rows(&config);
    for (cell, mut text, mut color) in &mut cells {
        if !live[cell.0] {
            continue;
        }
        let (label, value) = row_text(tr, &config, &settings, &beaches, menu.naming, cell.1);
        let half = match cell.2 {
            menu_ui::Half::Label => label,
            menu_ui::Half::Value => value,
        };
        menu_ui::set_text(&mut text, &half);
        menu_ui::set_color(
            &mut color,
            if cell.0 == menu.selected {
                Color::WHITE
            } else {
                palette::PARCHMENT.with_alpha(0.80)
            },
        );
    }
    for (row, mut fill, mut node) in &mut rows {
        // A row that does not apply leaves no trace: it folds out of the
        // column entirely. The card is a fixed height, so the ones that
        // remain do not move when it does.
        let display = if live[row.0] {
            Display::Flex
        } else {
            Display::None
        };
        if node.display != display {
            node.display = display;
        }
        let ground = menu_ui::band(row.0 == menu.selected && live[row.0]);
        if fill.0 != ground {
            fill.0 = ground;
        }
    }
}