Add a critter species
A critter’s species sets only its sprite and accent colour. It has no effect on behaviour. This page adds a Gila monster, written in YAML as:
species: gila-monsterHow a species is put together
Section titled “How a species is put together”| Half | Language | Decides |
|---|---|---|
core/src/critter/model.rs |
Rust | Which names a critter.yml may contain |
ui/src/components/critters/species.ts |
TypeScript | The name, colour and pixel art |
You’ll touch these files:
Directorycore/src/critter/
- model.rs add the variant
Directoryui/src/components/critters/
- species.ts draw it
- species.test.ts add it to the expected list
-
Add the variant in Rust
Section titled “Add the variant in Rust”In
core/src/critter/model.rs, a species appears in three places: the publicSpeciesenum, a privateKnownenum inside itsDeserializeimpl, and thematchthat maps one to the other. AddGilaMonsterto all three.core/src/critter/model.rs pub enum Species {#[default]Roadrunner,// …Tumbleweed,GilaMonster,}impl<'de> Deserialize<'de> for Species {fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {#[derive(Deserialize)]#[serde(rename_all = "kebab-case")]enum Known {Roadrunner,// …Tumbleweed,GilaMonster,}let value = serde_json::Value::deserialize(deserializer)?;Ok(match serde_json::from_value::<Known>(value) {Ok(Known::Roadrunner) | Err(_) => Self::Roadrunner,// …Ok(Known::Tumbleweed) => Self::Tumbleweed,Ok(Known::GilaMonster) => Self::GilaMonster,})}}rename_all = "kebab-case"turnsGilaMonsterintogila-monsterin YAML.Why two enums? If a newer version of cactus writes a species this build doesn’t know,
Err(_)falls back toroadrunner, so the critter still loads instead of failing. -
Regenerate the TypeScript types
Section titled “Regenerate the TypeScript types”Terminal window cargo test -p cactusThe
Speciestype inui/src/api/bindings.tsnow includes"gila-monster".Check:
cd ui && npm run typecheckfails inspecies.ts.SPECIESis aRecord<Species, SpeciesInfo>, so it needs an entry for every species and doesn’t have one for the new one yet. -
Draw the sprite
Section titled “Draw the sprite”Add an entry to
SPECIESinui/src/components/critters/species.ts:ui/src/components/critters/species.ts "gila-monster": {name: "Gila Monster",description: "Slow, but never lets go of a session.",accent: "#e07a3f",palette: { o: "#e07a3f", k: "#2b2024", p: "#f2b6a0" },body: [...blank(9),"..........kkoo..","...okokokokoeko.","..okokokokokokpp",".kokokokokokok..","..k.k.....k.k...","..k.k.....k.k...","................",],stride: {from: 13,rows: [".k...k...k...k..",".k...k...k...k..","................",],},lid: "k",},A sprite is a 16×16 grid with one character per pixel. Each character is looked up in
paletteto get its colour, and.is transparent.Key nameShown in the species picker descriptionOne line in the picker accentThe critter’s colour elsewhere in the UI paletteCharacter → colour body16 rows of exactly 16 characters, drawn from the bottom. blank(n)addsnempty rows on top.strideThe second frame of the walking animation: from row fromto the bottom,rowsreplace the body.from + rows.lengthmust be 16.lidThe character a closed eye turns into when the critter blinks Reserved characters you can’t redefine in
palette:Char eThe eye. Every body needs at least one. wThe eye when the critter’s flow passes xThe eye when it fails Only
bodyandstrideare drawn by hand. The app derives blinking (e→lid) and the pass/fail eyes (e→w/x).Check:
npm run typecheckpasses again. -
Update the test
Section titled “Update the test”In
ui/src/components/critters/species.test.ts, the testcovers every species the backend knowshas a list of every species. Add"gila-monster"to it.Terminal window cd ui && npm test -- speciesThe other tests in the file then check your drawing:
- every frame is 16×16
- every character is in the palette
- there’s an
eto blink - the stride fits
- no frame needs more than 90
<rect>s, which keeps sprites cheap to render
-
Look at it
Section titled “Look at it”Run the app in debug and open
/#sprites, a dev-only gallery of every sprite in every state. Check it on a dark theme and a light theme. Light themes add an outline, but pale colours can still disappear.Then pick it for a critter (Ctrl+B), save, and check
critter.ymlsaysspecies: gila-monster.