Molten

Step 6: A main menu and a pause screen

Screens form a stack. switchScreen(S) replaces the whole stack with S (and loads its level); pushScreen(S) puts S on top (a pause menu, a dialog) without touching the level; popScreen() returns to the screen below exactly as it was. Only the top screen gets UI input, but every screen in the stack is drawn, bottom to top.

Start on a menu

# game.toml
name = "CoinYard"
title = "Coin Yard"
first_screen = "MainMenu"
escape_quits = false

escape_quits = false stops Escape from quitting the game, so it can open the pause menu.

// screens/main_menu.screen
// No level: just the menu UI over the clear color.
export default screen({
    level: null,
    ui: ["ui/menu"],
});
// screens/pause.screen
// Pushed on top of Playing. level: null keeps the yard loaded underneath.
export default screen({
    level: null,
    ui: ["ui/pause"],
});

Button handlers

A button's onClick names a function exported from a Cast module next to the UI file:

// ui/actions.cast
import { audioPlay, clearColor, popScreen, requestQuit, switchScreen } from "molten";

function click(): void { audioPlay("audio/click.wav", "ui", 0.5, false, 0); }

export function play(): void {
    click();
    clearColor(vec4(0.45, 0.62, 0.8, 1));
    switchScreen("Playing");
}

export function resume(): void {
    click();
    popScreen();
}

export function toMenu(): void {
    click();
    clearColor(vec4(0.05, 0.07, 0.1, 1));
    switchScreen("MainMenu");
}

export function quit(): void { requestQuit(); }

clearColor sets the color behind the scene, here a sky blue while playing.

// ui/menu.ui
import { play, quit } from "./actions";

export default ui(
    <panel class="stage">
        <panel class="dialog">
            <text class="title">Coin Yard</text>
            <text>Collect every coin.</text>
            <button id="play" class="button primary" onClick={play}>Play</button>
            <button id="quit" class="button" onClick={quit}>Quit</button>
        </panel>
    </panel>
);
// ui/pause.ui
import { resume, toMenu } from "./actions";

export default ui(
    <panel class="stage">
        <panel class="dialog">
            <text class="title">Paused</text>
            <button id="resume" class="button primary" onClick={resume}>Resume</button>
            <button id="menu" class="button" onClick={toMenu}>Main menu</button>
        </panel>
    </panel>
);

Each .ui file has its own .style file. Save this as ui/menu.style and copy it to ui/pause.style:

.stage {
    position: absolute;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    align-items: center;
    justify-content: center;
}
.dialog {
    width: 320px;
    padding: 24px;
    flex-direction: column;
    gap: 12px;
    background: #141e2b;
    border: 1px solid #41556c;
    border-radius: 12px;
    color: #e5edf6;
    font-size: 18px;
}
.title { font-size: 32px; color: #ffd76a; }
.button { padding: 10px; background: #2a3646; border-radius: 6px; }
.button:hover { background: #3b4c62; transition: background 120ms; }
.primary { background: #c99a2e; color: #141e2b; }
.primary:hover { background: #e0b246; }

Selectors are classes (.button), ids (#play) and states (:hover, :focus, :disabled). Tag selectors such as button { } are not supported: castc check reports expected a class, id or state selector.

Escape to pause

Behaviors run every tick no matter which screen is on top, so gameplay checks the screen itself. Update the director:

// behaviors/director.cast
import {
    behavior, characterMove, Key, keyDown, keyPressed, popScreen, pushScreen, screenIs, spawnObject, spawnPosition
} from "molten";
import { CoinGame } from "../components/coin_game";

function axis(positive: Key, negative: Key): float {
    return (keyDown(positive) ? 1 : 0) - (keyDown(negative) ? 1 : 0);
}

export default behavior([CoinGame], (objects, dt: float): void => {
    for (const obj of objects) {
        // Behaviors run whatever screen is on top, so check it.
        if (screenIs("Pause")) {
            if (keyPressed("Escape")) popScreen();
            continue;
        }
        if (!screenIs("Playing")) continue;
        if (keyPressed("Escape")) {
            pushScreen("Pause");
            continue;
        }
        // Write through obj.coinGame: `const game = obj.coinGame` would be a copy.
        if (!obj.coinGame.started) {
            obj.coinGame.player = spawnObject("Player", spawnPosition(0));
            obj.coinGame.started = true;
        }
        const player = obj.coinGame.player.as<Player>()?.get();
        if (!player) continue;
        let direction = vec3(axis("D", "A"), 0, axis("S", "W"));
        if (length(direction) > 0) direction = normalize(direction);
        characterMove(player.handle, direction * obj.coinGame.walkSpeed, false);
    }
});

keyPressed is true during the first tick after the key went down; keyDown is true while it is held.

Run it

castc check coinyard prints 8 objects, 3 screens, 1 level. The game opens on the menu. Play switches to Playing, which loads the yard and starts a fresh round (the director is re-created from the level file, so started is false again). Escape pushes Pause on top; the yard stays loaded and drawn underneath. Resume or Escape pops back exactly where you were, and Main menu unloads the level.

The coins keep spinning while paused, because the spin behavior doesn't check the screen. That is a design choice you now control: add if (!screenIs("Playing")) return; at the top of the spin behavior if you want them to stop.

Where to go next

  • Open the game in devtools (dev-run path/to/coinyard), drag more coins in from the Objects tab, and save. See Devtools.
  • Read the Concepts pages for the model behind what you built.
  • Read the Cast language chapters.
  • Ship it: molten build --release coinyard dist/CoinYard (Shipping).
  • Study samples/gridline, a full game in the same style.

View this page's source on GitHub