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 start screen: the title sign, the mode list, and the crab field
//! guide, over the beach postcard that [`scenery`] paints and animates.

mod scenery;

pub use scenery::{menu_ambience, refit_shore};

use crate::app::effects::VisualRng;
use crate::app::i18n::fill;
use crate::app::menu_ui;
use crate::app::menu_ui::card_shadow;
use crate::app::palette;
use crate::app::settings::GameSettings;
use crate::app::{Campaign, CampaignKind, Screen};
use crate::sim::challenge_levels;
use bevy::prelude::*;

/// The wordmark, spaced out by hand.
///
/// Set in the game's own face rather than a drawn pixel-font, letter-spaced
/// with figure spaces (U+2007, as wide as a digit) so it reads as a sign
/// rather than a sentence. The gap between the two words is three of them,
/// so "PINCH" and "POINTS" still read as two words at that spacing.
const TITLE: &str = "P\u{2007}I\u{2007}N\u{2007}C\u{2007}H\u{2007}\u{2007}\u{2007}\u{2007}P\u{2007}O\u{2007}I\u{2007}N\u{2007}T\u{2007}S";

/// The title's ink and the shadow it drops on the sky.
const TITLE_INK: Color = Color::srgb(0.99, 0.85, 0.36);
const TITLE_SHADOW: Color = Color::srgba(0.05, 0.10, 0.20, 0.55);

/// The title sign is the one card that hangs over the bright sky, where a
/// white cloud drifting behind 12% of transparency shows through as a grey
/// smudge across the letters. It gets a denser fill for that reason alone.
const SIGN_FILL: Color = Color::srgba(0.05, 0.09, 0.14, 0.96);

/// One landing-menu entry, in display order (digit 1 launches the
/// first). The launch ladder and the i18n name/blurb tables follow this
/// order; matching on the enum keeps them from drifting.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum MenuEntry {
    TidePool,
    TurfWar,
    Driftwood,
    Replay,
    BeachLobby,
    Settings,
    BeachDay,
    Achievements,
    DailyChallenge,
    /// Only offered when a round is waiting to be picked up.
    Resume,
}

impl MenuEntry {
    pub const ALL: [MenuEntry; 10] = [
        MenuEntry::TidePool,
        MenuEntry::TurfWar,
        MenuEntry::Driftwood,
        MenuEntry::Replay,
        MenuEntry::BeachLobby,
        MenuEntry::Settings,
        MenuEntry::BeachDay,
        MenuEntry::Achievements,
        MenuEntry::DailyChallenge,
        MenuEntry::Resume,
    ];
}

/// Number of modes on the list (digit 1 launches entry 0, and so on).
pub const MENU_ENTRY_COUNT: usize = MenuEntry::ALL.len();

/// Which rows are on offer. Every mode always is; resuming is there only
/// when there is a round to resume.
fn live_rows() -> [bool; MENU_ENTRY_COUNT] {
    let waiting = crate::app::suspend::waiting();
    std::array::from_fn(|row| MenuEntry::ALL[row] != MenuEntry::Resume || waiting)
}

/// Which mode row the cursor is on. Persists across visits to the menu.
#[derive(Resource, Default)]
pub struct MenuList {
    pub selected: usize,
}

#[derive(Component)]
pub struct MenuRow(pub usize);

/// The three columns of a menu row. They are separate text nodes rather
/// than one padded string so each can carry its own size and ink: the
/// hotkey wants to recede, the mode name wants to lead, and the blurb is
/// an aside. As one string in one colour they competed, which is what made
/// the list read as a wall.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum MenuCol {
    Key,
    Name,
    Blurb,
}

/// One cell of one row.
#[derive(Component)]
pub struct MenuCell(pub usize, pub MenuCol);

/// Everything spawned for the menu decoration.
#[derive(Component)]
pub struct MenuArt;

/// The title sign and its tagline, floating on the sky.
fn spawn_title(commands: &mut Commands, settings: &GameSettings) {
    // Title card: floats over the sky, sized to its content so the
    // panorama shows around it.
    commands
        .spawn((
            MenuArt,
            Node {
                position_type: PositionType::Absolute,
                top: Val::Px(44.0),
                left: Val::Px(0.0),
                width: Val::Percent(100.0),
                flex_direction: FlexDirection::Column,
                align_items: AlignItems::Center,
                ..default()
            },
        ))
        .with_children(|wrap| {
            // The sign itself: the same deep-sea card with a gold hairline
            // that the mode list, the crab legend and the stage list wear,
            // tying the postcard to the rest of the game and keeping pale
            // letters legible where a cloud drifts behind.
            wrap.spawn((
                Node {
                    flex_direction: FlexDirection::Column,
                    align_items: AlignItems::Center,
                    row_gap: Val::Px(6.0),
                    padding: UiRect::axes(Val::Px(34.0), Val::Px(16.0)),
                    border: UiRect::all(Val::Px(1.0)),
                    border_radius: BorderRadius::all(Val::Px(18.0)),
                    ..default()
                },
                BackgroundColor(SIGN_FILL),
                BorderColor::all(palette::CARD_EDGE),
                card_shadow(),
            ))
            .with_children(|card| {
                card.spawn((
                    Text::new(TITLE),
                    TextFont {
                        font_size: FontSize::Px(54.0),
                        ..default()
                    },
                    TextColor(TITLE_INK),
                    TextShadow {
                        offset: Vec2::new(2.0, 3.0),
                        color: TITLE_SHADOW,
                    },
                ));
                card.spawn((
                    Text::new(settings.tr().tagline),
                    TextFont {
                        font_size: FontSize::Px(18.0),
                        ..default()
                    },
                    TextColor(palette::PARCHMENT),
                ));
            });
        });
}

/// The mode list, centred over the sea.
fn spawn_mode_list(commands: &mut Commands) {
    commands
        .spawn((
            MenuArt,
            Node {
                position_type: PositionType::Absolute,
                // Under the title sign, with the sea behind it: the crab
                // legend that used to sit below has moved to the screens
                // where there are crabs to compare it against.
                top: Val::Px(196.0),
                width: Val::Percent(100.0),
                flex_direction: FlexDirection::Column,
                align_items: AlignItems::Center,
                ..default()
            },
        ))
        .with_children(|wrap| {
            wrap.spawn((
                Node {
                    flex_direction: FlexDirection::Column,
                    // The rows carry their own padding now, so the gap
                    // between them is a hairline rather than a blank line.
                    row_gap: Val::Px(2.0),
                    padding: UiRect::axes(Val::Px(20.0), Val::Px(16.0)),
                    border: UiRect::all(Val::Px(1.0)),
                    border_radius: BorderRadius::all(Val::Px(18.0)),
                    ..default()
                },
                BackgroundColor(palette::CARD_FILL),
                BorderColor::all(palette::CARD_EDGE),
                card_shadow(),
            ))
            .with_children(|panel| {
                for row in 0..MENU_ENTRY_COUNT {
                    panel
                        .spawn((
                            MenuRow(row),
                            Node {
                                align_items: AlignItems::Center,
                                column_gap: Val::Px(14.0),
                                padding: UiRect::axes(Val::Px(12.0), Val::Px(4.0)),
                                border_radius: BorderRadius::all(Val::Px(8.0)),
                                ..default()
                            },
                            BackgroundColor(Color::NONE),
                        ))
                        .with_children(|line| {
                            for (col, width, size) in [
                                (MenuCol::Key, 16.0, 17.0),
                                (MenuCol::Name, 216.0, 21.0),
                                (MenuCol::Blurb, 384.0, 17.0),
                            ] {
                                line.spawn((
                                    Node {
                                        width: Val::Px(width),
                                        flex_shrink: 0.0,
                                        overflow: Overflow::clip_x(),
                                        ..default()
                                    },
                                    children![(
                                        MenuCell(row, col),
                                        Text::new(""),
                                        TextFont {
                                            font_size: FontSize::Px(size),
                                            ..default()
                                        },
                                        TextLayout::no_wrap(),
                                        TextColor(palette::IDLE_ROW),
                                    )],
                                ));
                            }
                        });
                }
            });
        });
}

pub fn enter_menu(mut commands: Commands, settings: Res<GameSettings>) {
    spawn_title(&mut commands, &settings);
    spawn_mode_list(&mut commands);
    spawn_version(&mut commands);
}

/// The build's version, small, in the bottom-right corner: the one thing
/// worth reading off a screenshot when a friend's beach will not take a
/// join, and the number the new-version page is comparing against.
///
/// On the prompt line's own dark pill, at the other end of it, so the two
/// read as one strip along the bottom rather than as a label loose on the
/// sand.
fn spawn_version(commands: &mut Commands) {
    commands.spawn((
        MenuArt,
        Text::new(crate::app::update::Version::current().to_string()),
        TextFont {
            font_size: FontSize::Px(15.0),
            ..default()
        },
        TextLayout::no_wrap(),
        TextColor(palette::PARCHMENT.with_alpha(0.75)),
        Node {
            position_type: PositionType::Absolute,
            bottom: Val::Px(8.0),
            right: Val::Px(12.0),
            padding: UiRect::axes(Val::Px(12.0), Val::Px(7.0)),
            border_radius: BorderRadius::all(Val::Px(11.0)),
            ..default()
        },
        BackgroundColor(Color::srgba(0.05, 0.07, 0.11, 0.72)),
    ));
}

/// Style the mode rows around the selection, settings-menu style.
pub fn update_menu_rows(
    list: Res<MenuList>,
    settings: Res<GameSettings>,
    mut cells: Query<(&MenuCell, &mut Text, &mut TextColor)>,
    mut rows: Query<(&MenuRow, &mut BackgroundColor)>,
) {
    let tr = settings.tr();
    let live = live_rows();
    for (cell, mut text, mut color) in &mut cells {
        let picked = cell.0 == list.selected;
        let line = if live[cell.0] {
            match cell.1 {
                // Rows are keyed 1-9 then 0, which keeps every hotkey one
                // character wide and the name column straight.
                MenuCol::Key => if cell.0 + 1 < 10 { cell.0 + 1 } else { 0 }.to_string(),
                MenuCol::Name => tr.menu_names[cell.0].to_string(),
                // The daily is the one row whose blurb is not the same
                // every day, and the date is the whole point of it: you
                // want to know whether you have already played today's.
                MenuCol::Blurb if MenuEntry::ALL[cell.0] == MenuEntry::DailyChallenge => {
                    let (day, month) = crate::app::clock::civil_date(crate::app::Daily::today());
                    fill(
                        tr.menu_daily_blurb,
                        &[("d", &format!("{day:02}")), ("m", &format!("{month:02}"))],
                    )
                }
                MenuCol::Blurb => tr.menu_blurbs[cell.0].to_string(),
            }
        } else {
            String::new()
        };
        // Three inks, and the row you are on brightens all of them: the
        // name leads, the blurb explains, the hotkey is there when you
        // want it and out of the way when you do not.
        let target = match (cell.1, picked) {
            (MenuCol::Key, true) => palette::GOLD,
            (MenuCol::Key, false) => palette::PARCHMENT.with_alpha(0.30),
            (MenuCol::Name, true) => Color::WHITE,
            (MenuCol::Name, false) => palette::PARCHMENT.with_alpha(0.92),
            (MenuCol::Blurb, true) => palette::PARCHMENT.with_alpha(0.85),
            (MenuCol::Blurb, false) => palette::PARCHMENT.with_alpha(0.45),
        };
        menu_ui::set_text(&mut text, &line);
        menu_ui::set_color(&mut color, target);
    }
    for (row, mut fill) in &mut rows {
        // A band behind the cursor rather than a marker beside it. The list
        // is ten rows of two columns; a caret at the far left is a long way
        // from the words it is pointing at.
        let ground = if row.0 == list.selected && live[row.0] {
            palette::GOLD.with_alpha(0.16)
        } else {
            Color::NONE
        };
        if fill.0 != ground {
            fill.0 = ground;
        }
    }
}

/// The landing menu: W/S (or arrows) move the selection, Enter launches it,
/// and the digit hotkeys still jump straight into any mode.
#[allow(clippy::too_many_arguments)]
pub fn menu_input(
    keys: Res<ButtonInput<KeyCode>>,
    mut exit: MessageWriter<AppExit>,
    mut list: ResMut<MenuList>,
    settings: Res<GameSettings>,
    mut campaign: ResMut<Campaign>,
    mut config: ResMut<crate::app::match_setup::MatchConfig>,
    mut daily: ResMut<crate::app::Daily>,
    mut resuming: ResMut<crate::app::Resuming>,
    mut notice: ResMut<crate::app::RoundNotice>,
    mut clipboard: ResMut<Clipboard>,
    mut next_screen: ResMut<NextState<Screen>>,
) {
    // Every other screen leaves on Esc, and the menu is the one screen with
    // nowhere left to go: it leaves the game. Without this the only way out
    // is the window's own close button, which a fullscreen player has not
    // got.
    if keys.just_pressed(KeyCode::Escape) {
        exit.write(AppExit::Success);
        return;
    }
    // A pasted round is the other way onto a beach mid-play, and it does not
    // belong to any one row: V works wherever the cursor is.
    if keys.just_pressed(KeyCode::KeyV) {
        match crate::app::suspend::round_from(
            crate::app::codes::paste(&mut clipboard),
            settings.tr(),
        ) {
            Ok(round) => {
                notice.0.clear();
                resuming.0 = Some(round);
                next_screen.set(Screen::Versus);
            }
            Err(complaint) => notice.0 = complaint,
        }
        return;
    }
    // The resume row is only there when a round is waiting, and the cursor
    // steps over it when it is not.
    let live = live_rows();
    list.selected = menu_ui::nav_live(&keys, list.selected, &live);

    const HOTKEYS: [KeyCode; MENU_ENTRY_COUNT] = [
        KeyCode::Digit1,
        KeyCode::Digit2,
        KeyCode::Digit3,
        KeyCode::Digit4,
        KeyCode::Digit5,
        KeyCode::Digit6,
        KeyCode::Digit7,
        KeyCode::Digit8,
        KeyCode::Digit9,
        // The tenth row is the resume slot, and 0 follows 9 on a keyboard.
        KeyCode::Digit0,
    ];
    let choice = if keys.just_pressed(KeyCode::Enter) {
        Some(list.selected)
    } else {
        HOTKEYS.iter().position(|&key| keys.just_pressed(key))
    };
    let Some(choice) = choice.filter(|&row| live[row]) else {
        return;
    };
    list.selected = choice;
    match MenuEntry::ALL[choice] {
        MenuEntry::TidePool => {
            let (levels, builtins) = crate::app::campaign::tide_pool_levels();
            campaign.reset(CampaignKind::TidePool, levels, builtins);
            // Both lists go through the stage select, which is where the
            // player picks up where they left off.
            next_screen.set(Screen::StageSelect);
        }
        MenuEntry::TurfWar => next_screen.set(Screen::MatchSetup),
        MenuEntry::Driftwood => next_screen.set(Screen::Editor),
        // The library, which lists every round kept - including the newest.
        MenuEntry::Replay => next_screen.set(Screen::Replays),
        MenuEntry::BeachLobby => next_screen.set(Screen::Lobby),
        MenuEntry::Settings => next_screen.set(Screen::Settings),
        MenuEntry::BeachDay => {
            let levels = challenge_levels();
            let builtins = levels.len();
            campaign.reset(CampaignKind::BeachDay, levels, builtins);
            next_screen.set(Screen::StageSelect);
        }
        MenuEntry::Achievements => next_screen.set(Screen::Achievements),
        MenuEntry::Resume => match crate::app::suspend::pick_up() {
            Ok(round) => {
                // The round brings its own board, seats and AI; the match
                // config is left alone so it is not mistaken for a source.
                notice.0.clear();
                resuming.0 = Some(round);
                next_screen.set(Screen::Versus);
            }
            Err(e) => notice.0 = e,
        },
        MenuEntry::DailyChallenge => {
            // The daily: today's arena, three fierce bots, standard round.
            config.seats = 4;
            config.bots = 3;
            config.bot_levels = [crate::sim::BotLevel::Hard; crate::sim::MAX_PLAYERS];
            config.map = crate::app::match_setup::MapChoice::GenClassic;
            config.gulls = crate::app::match_setup::GullPressure::Normal;
            config.round = crate::app::match_setup::RoundLength::Standard;
            config.armed = true;
            daily.active = true;
            next_screen.set(Screen::Versus);
        }
    }
}