GizStackGizStack
View .mdOpen in ChatGPTOpen in Claude

gizgame/play

The surface program of a game: what a host calls (a page's surface, a window), the art a game declares behind the loading screen.

gizlang
import * as Play from "gizgame/play";

Types

Program

gizlang
type Program = { initialize: (input: bytes) => bytes; frame: (input: bytes) => bytes; snapshot: (input: bytes) => bytes; restore: (input: bytes) => bytes; cues?: (input: bytes) => bytes; file?: (input: bytes) => bytes; net?: (input: bytes) => bytes; phase?: (input: bytes) => bytes; }

A surface program: its exports, as a page's surface (app/components/src/surface.js) and a window (gizapp/play) call them, each bytes to bytes. CUES is optional: the sounds a frame asks for. NET is optional, for a game played on a server: NET(MESSAGE) takes a message of the server, and NET(b"") gives the next message to send (b"" none). The host calls it before each frame. PHASE is optional: the game's phase as a word (loading, title, play, paused, ended), which a tool reads to know what it measured (giz games measure).

  • initialize: (arg0: bytes) => bytes
  • frame: (arg0: bytes) => bytes
  • snapshot: (arg0: bytes) => bytes
  • restore: (arg0: bytes) => bytes
  • cues: _gizlang_sdk_core.Option<(arg0: bytes) => bytes>
  • file: _gizlang_sdk_core.Option<(arg0: bytes) => bytes>
  • net: _gizlang_sdk_core.Option<(arg0: bytes) => bytes>
  • phase: _gizlang_sdk_core.Option<(arg0: bytes) => bytes>

Art

gizlang
type Art<A> = (game: Game, resources: Resources, screen: Screen) => A throws string

The art a game draws with: declared once the host gave its files (Game.file), behind the loading screen. Work that reads and decodes them goes in steps (Game.later), so the screen keeps moving.

Draw

gizlang
type Draw<A> = (picture: Picture, art: mut A) => void throws string

A game's drawing of one frame: its ART into PICTURE. ART is the view's own state from frame to frame (a camera, marks that fade); the game's state stays in the World.

Play

gizlang
class Play<A> { game: Game; resources: Resources = new Resources(); private build: Art<A>; private paint: Draw<A>; private art: A | undefined = undefined; private built: i64 = -1; private ready: bool = false; private carrier: ((input: bytes) => bytes) | undefined = undefined; private splash: Sprites | undefined = undefined; private written: bool = false; private ring: Vec<f64> = []; private settled: f64 = 0.0; held: bool = false; atOnce: bool = false; constructor(game: Game, art: Art<A>, draw: Draw<A>) { this.game = game; game.drawn = true; this.build = art; this.paint = draw; serve(this.program()); } carried(carry: (input: bytes) => bytes): Play<A> { this.carrier = carry; serve(this.program()); return this; } private picture(screen: Screen): bytes throws string { this.resources.settle(); if (this.art !== undefined && this.built !== this.game.map) { this.art = undefined; this.ready = false; this.splash = undefined; this.resources.clear(); this.game.settling = undefined; this.settled = 0.0; } if (!this.ready) { const given = this.game.given(); if (given <1 || this.splash === undefined) { return this.loading(screen, given*0.5); } if (this.art === undefined) { this.built = this.game.map; this.resources.limitTo(this.game.budget()); this.game.reading = true; try { this.art = this.build(this.game, this.resources, screen); } catch (why) { this.game.reading = false; if (why === WAITING) { return this.loading(screen, 0.5); } throw why; } this.game.reading = false; } const until = Task.now()+WORK_MS; while (this.game.prepare()) { if (this.game.waiting() || !this.atOnce && Task.now()> = until) { return this.loading(screen, 0.5+this.game.prepared()*0.45+this.settled*0.05); } } if (!this.atOnce && this.game.settling !== undefined && this.settled <1.0) { return this.loading(screen, 0.95+this.settled*0.05); } this.ready = true; } else { const until = Task.now()+WORK_MS; while (this.game.prepare()) { if (this.game.waiting() || !this.atOnce && Task.now()> = until) { break; } } } this.game.targets = []; let p = new Picture(this.game, screen, this.resources); let art = this.art!; this.paint(p, art); this.art = art; if (this.game.prepared()> = 1.0) { for (const file of this.game.held()) { if (!this.resources.holds(file)) { p.graph.forget(file); this.game.letGo(file); } } } return p.graph.packet(this.resources, screen.measure); } private loading(screen: Screen, done: f64): bytes throws string { const faced = lettered(this.game); if (this.splash === undefined || faced !== this.written) { let made = new Sprites(faces(this.game), 1024, 6000, (code): Lent | undefined => this.game.glyph(code)); this.ring = made.ring(48); this.splash = made; this.written = faced; } let sprites = this.splash!; const t = Task.now()/1000.0; let p = new Picture(this.game, screen, this.resources); let h = new Hud(p, sprites); let c = h.canvas; const middle = h.width*0.5; const pale = [0.93, 0.95, 1, 0.94]; const dim = [0.72, 0.78, 0.9, 0.6]; const accent = [0.55, 0.72, 1, 0.9]; const breath = 0.8+0.2*Math.sin(t*1.6); c.light(middle, h.height*0.36, h.width*0.9, [0.16, 0.24, 0.46, 0.22*breath]); c.light(middle, h.height*1.05, h.width*0.8, [0.32, 0.18, 0.12, 0.12]); const size = Math.min(56.0, h.width*0.11); if (faced && this.game.name !== "") { h.center(this.game.name, h.height*0.42-size*0.5, size, pale, true); } const y = h.height*0.42+size+36; const wide = Math.min(320.0, h.width*0.6); const left = middle-wide*0.5; const fill = Math.max(0.02, Math.min(1, done)); h.bar(middle, y, wide, 3, fill, accent, [1, 1, 1, 0.14]); const run = (t*0.6)%1; c.light(left+wide*run, y, 90, [0.55, 0.72, 1, 0.35*Math.sin(run*Math.PI)]); const ry = y+44; const turn = t*4; c.sprite(this.ring, middle, ry, 30, 30, 0, [1, 1, 1, 0.16]); for (const k of range(0, 3)) { const a = turn-f64(k)*0.45; c.dot(middle+Math.cos(a)*14, ry+Math.sin(a)*14, 6-f64(k)*1.5, [accent[0], accent[1], accent[2], 0.9-f64(k)*0.3], 1); } if (faced) { h.center(`${i64(fill*100) }%`, ry+28, 15, dim); } sprites.draw(p, c, [0.016, 0.02, 0.032, 1]); if (this.game.settling !== undefined) { this.settled = this.game.settling!(p.graph); } return p.graph.packet(this.resources, screen.measure); } initialize(input: bytes): bytes throws string { return this.picture(this.game.initialize(input)); } file(input: bytes): bytes throws string { return this.game.receive(input); } frame(input: bytes): bytes throws string { try { return this.held || this.art !== undefined && !this.ready ? this.still(input): this.picture(this.game.frame(input)); } catch (why) { if (why === WAITING) { throw `${WAITING }:  ${ this.game.needs() },  read outside a step of later`; } throw why; } } still(input: bytes): bytes throws string { return this.picture(this.game.still(input)); } snapshot(input: bytes): bytes throws string { return this.game.snapshot(); } restore(input: bytes): bytes throws string { return this.game.restore(input); } cues(input: bytes): bytes throws string { return this.game.cues(); } net(input: bytes): bytes { return this.carrier === undefined ? b"": this.carrier!(input); } phase(): bytes { if (this.held || this.art === undefined || !this.ready) { return utf8("loading"); } const at = this.game.phase(); return utf8(at === TITLE ? "title": at === PLAYING ? "play": at === PAUSED ? "paused": "ended"); } program(): Program { let p: Program = { initialize: (input): bytes => this.initialize(input)!, frame: (input): bytes => this.frame(input)!, snapshot: (input): bytes => this.snapshot(input)!, restore: (input): bytes => this.restore(input)!, cues: (input): bytes => this.cues(input)!, file: (input): bytes => this.file(input)!, phase: (input): bytes => this.phase() }; if (this.carrier !== undefined) { p.net = (input): bytes => this.net(input); } return p; } }

A Game as a surface program: its art, built behind the loading screen, and its drawing at each frame. The host's calls reach the last Play made (hostFrame and the others), so a game's module only declares it; a window takes them in one record (program()).

Opaque: create and use it through this module's functions.

Functions

engineAssets

gizlang
function engineAssets(): string

The assets lock of the engine's own files (its fonts), which a host gives every game beside the game's own.

faces

gizlang
function faces(game: Game): Fonts

The fonts GAME draws with, made once.

Effects: fail, read, write, console, io, alloc.

constructor

gizlang
constructor(game: Game, art: Art<A>, draw: Draw<A>)

GAME, drawn with the ART that its first frame builds and the DRAW of each frame.

hostInitialize

gizlang
@Export("initialize") export function hostInitialize(input: bytes): bytes

The host's initialize call.

Effects: fail, read, write, console, io, alloc.

hostFrame

gizlang
@Export("frame") export function hostFrame(input: bytes): bytes

The host's frame call.

Effects: fail, read, write, console, io, alloc.

hostSnapshot

gizlang
@Export("snapshot") export function hostSnapshot(input: bytes): bytes

The host's snapshot call.

Effects: fail, read, write, console, io, alloc.

hostRestore

gizlang
@Export("restore") export function hostRestore(input: bytes): bytes

The host's restore call.

Effects: fail, read, write, console, io, alloc.

hostCues

gizlang
@Export("cues") export function hostCues(input: bytes): bytes

The host's cues call: the sounds to play.

Effects: fail, read, write, console, io, alloc.

hostFile

gizlang
@Export("file") export function hostFile(input: bytes): bytes

The host's file call: a file of the lock.

Effects: fail, read, write, console, io, alloc.

hostNet

gizlang
@Export("net") export function hostNet(input: bytes): bytes

The host's net call: the messages of the online world.

Effects: fail, read, write, console, io, alloc.

hostPhase

gizlang
@Export("phase") export function hostPhase(input: bytes): bytes

The host's phase call: the game's phase as a word.

Effects: fail, read, write, console, io, alloc.

Methods

Play.carried

gizlang
carried(carry: (input: bytes) => bytes): Play<A>

The game talks with a server: the host carries its messages to and from CARRY (Program.net), as an online world's match (Online carry) or a talk with the game's minds (gizgame/talk Talker carry).

Play.initialize

gizlang
initialize(input: bytes): bytes throws string

The first frame, at the host's size and with the device's features: the loading screen, with the game's title.

Play.file

gizlang
file(input: bytes): bytes throws string

An asset file before initialize: its name, a newline and its bytes.

Play.frame

gizlang
frame(input: bytes): bytes throws string

One host frame: its input, the fixed ticks it covers, and the picture.

Play.still

gizlang
still(input: bytes): bytes throws string

One picture of the World as it is, with no tick: a held frame, or a page's change of view.

Play.snapshot

gizlang
snapshot(input: bytes): bytes throws string

The whole simulation: the World, its pending input and its tick.

Play.restore

gizlang
restore(input: bytes): bytes throws string

Replaces the simulation with a snapshot of this game; another schema or rate is refused first.

Play.cues

gizlang
cues(input: bytes): bytes throws string

The sounds of the ticks since the last call.

Play.net

gizlang
net(input: bytes): bytes

One message of the server (INPUT), or the next one to send (b"": b"" none), for a game played on a server.

Play.phase

gizlang
phase(): bytes

The phase as a word: the loading screen's "loading" until the art is ready, then the game's.

Play.program

gizlang
program(): Program

The exports as one record, for a window (gizapp/play host).

Constants

WORK_MS

gizlang
const WORK_MS: f64 = 10

The milliseconds of a frame that the art's work takes behind the loading screen (Game.later): the screen keeps moving.