# Build game UI

Cozy UI ships ready-made game screens: inventory, item details, character sheet, collection book, collectible cards, a world map, a minimap, a shop with a wallet and purchase dialog, perks, a skill tree and buffs. They hold no game rules. You pass plain data, they draw it and tell you what the player did, so they fit any game state: React state, a store, a server or an ECS.

## What is in the kit

| Component | What it does | Wire it with |
|---|---|---|
| [Inventory](https://cozy-ui.solo.engineer/docs/components/inventory/) | Bag, chest or hotbar: drag and drop with mouse, touch and keyboard, stacking and merging, locked slots, tabs, search and sort. | `items``onItemsChange(next``change)` |
| [Item Info](https://cozy-ui.solo.engineer/docs/components/item-info/) | Item details and tooltips: rarity, stats, compare against what is equipped (+/- in colour), action buttons. | `item``compareTo``actions` |
| [Character Info](https://cozy-ui.solo.engineer/docs/components/character-info/) | Character sheet: portrait, level and XP, health and energy bars, attributes with bonuses, status chips, equipment slots. | `character``onEquipmentSelect` |
| [Gallery](https://cozy-ui.solo.engineer/docs/components/gallery/) | Collection book for items, creatures or friends: silhouettes until found, progress, filters, a big view with next and previous. | `entries``onOpenChange` |
| [Item Card](https://cozy-ui.solo.engineer/docs/components/item-card/) | Collectible cards with rarity frames, elements, abilities, foil, 3D tilt and flip. CardHand fans a hand; CardGrid is a binder. | `card``onSelect``owned` |
| [Currency Bar](https://cozy-ui.solo.engineer/docs/components/currency-bar/) | HUD wallet: animated amounts, floating +/- deltas, caps like 35 / 60, a get-more button and a "not enough" shake. | `wallet``onAdd``highlight` |
| [Shop](https://cozy-ui.solo.engineer/docs/components/shop/) | In-game store: category tabs, featured deals with countdowns, discounts, stock, owned items and real-money currency packs. | `items``wallet``onBuy` |
| [Purchase Dialog](https://cozy-ui.solo.engineer/docs/components/purchase-dialog/) | The confirm step: quantity, total, balance before and after, get more, then busy, success or error. Your game charges. | `item``wallet``onConfirm` |
| [Perk Picker](https://cozy-ui.solo.engineer/docs/components/perk-picker/) | Pick one of a few perks on level-up, with rarity, reroll, skip and confirm. | `perks``onConfirm``onReroll` |
| [Skill Tree](https://cozy-ui.solo.engineer/docs/components/skill-tree/) | Talent tree: prerequisites, ranks, points, learn and refund, pan and zoom, a details panel. | `nodes``ranks``onRanksChange``points` |
| [Buff Bar](https://cozy-ui.solo.engineer/docs/components/buff-bar/) | Active buffs and debuffs with countdown rings, stacks and tooltips. | `buffs``onExpire` |
| [Game Map and MiniMap](https://cozy-ui.solo.engineer/docs/components/game-map/) | World map like a maps app (pins, clusters, place panel, search, filter chips, waypoints, clouds over unexplored land) and a HUD minimap with edge arrows. | `markers``regions``player``onMarkerSelect` |

Two full screens show them together: the [Character Screen](https://cozy-ui.solo.engineer/blocks/character-screen/) (sheet, bag, item details, equip and sell, collection) and the [Village HUD](https://cozy-ui.solo.engineer/blocks/village-hud/) (resources, harvest button, level-up cards, minimap that opens the map) and the [Village Shop](https://cozy-ui.solo.engineer/blocks/village-shop/) (wallet, shelves, purchase dialog, gem packs).

## Install

With npm, everything is in one package:

Terminal:

```bash
npm install @0xtrou/cozy-ui
```

main.tsx:

```tsx
import "@0xtrou/cozy-ui/styles.css"
import { Inventory, ItemCard, GameMap, MiniMap } from "@0xtrou/cozy-ui"
```

Or copy the source into your project with the shadcn CLI and change anything:

Terminal:

```bash
npx shadcn@latest add @cozy/cozy-theme @cozy/inventory @cozy/item-info @cozy/character-info @cozy/gallery @cozy/item-card @cozy/game-map @cozy/currency-bar @cozy/shop @cozy/purchase-dialog @cozy/perk-picker @cozy/skill-tree @cozy/buff-bar
```

## Data in, events out

Every game component takes plain objects and calls you back. Keep the real state where your game keeps it and hand the components a view of it. Each item, card, entry and marker has a `data` field typed by you, so your own object comes back in every event.

Backpack.tsx:

```tsx
import { Inventory, ItemInfo, type GameItem, type InventorySlots } from "@0xtrou/cozy-ui"

// Your game state: a store, a server snapshot or ECS components.
const bag: InventorySlots = useGame((s) => s.bag)
const [selected, setSelected] = useState<number | null>(null)

<Inventory
  items={bag}
  slots={24}
  lockedFrom={18}                         // bag upgrades unlock the rest
  onItemsChange={(next, change) => {
    // change: { type: "move" | "merge" | "sort", from, to }
    game.send({ type: "bag/update", next, change })
  }}
  selected={selected}
  onSelect={(_, index) => setSelected(index)}
  onActivate={(item) => game.send({ type: "use", id: item.id })}   // double-click or Enter
/>
{selected != null && bag[selected] && <ItemInfo item={bag[selected]!} compareTo={equippedHat} />}
```

## Cards from your items

A card is one object. Art can be a React node (SVG, sprite, canvas) or an image URL; elements and rarities are your own sets.

Deck.tsx:

```tsx
import { ItemCard, CardHand, type CardData } from "@0xtrou/cozy-ui"

const toCard = (c: MyCard): CardData<MyCard> => ({
  id: c.id, name: c.name, art: c.imageUrl, rarity: c.rarity, element: c.element,
  stat: { label: "HP", value: c.hp }, type: c.typeLine,
  abilities: c.moves.map((m) => ({ name: m.name, cost: m.cost, value: m.damage, text: m.text })),
  footer: { number: `${c.no}/120`, set: "Village Set" }, data: c,
})

<CardHand cards={hand.map(toCard)} selectedId={picked} onSelect={(card) => play(card.data!)} />
<ItemCard card={toCard(reward)} size="lg" flipOnClick />
```

## Map and minimap from one world

Use your game's own coordinates. Both components take the same object, so the minimap and the full map never disagree. Mark a region `discovered: false` to keep it under clouds until the player gets there.

WorldMap.tsx:

```tsx
import { GameMap, MiniMap, useRegionChange } from "@0xtrou/cozy-ui"

const world = {
  mapWidth: 1200, mapHeight: 800, background: "/maps/village.png",
  kinds: { shop: { label: "Shops", icon: <CoinIcon />, color: "#7FC4E0" } },
  markers: places.map((p) => ({ id: p.id, x: p.x, y: p.y, label: p.name, kind: p.kind, tracked: p.quest })),
  regions, player: { x: me.x, y: me.y, heading: me.facing }, waypoint,
}

useRegionChange(world.regions, world.player, (r) => r && toast(`Entering ${r.name}`))

<MiniMap {...world} onOpen={() => setMapOpen(true)} />
<GameMap {...world} search scale={{ perUnit: 0.5, unit: "m" }}
  onWaypointChange={setWaypoint}
  markerActions={(m) => <Button onClick={() => travel(m.id)}>Travel</Button>} />
```

## Shop and payments

Cozy UI draws the store; it never charges anyone. In-game prices use your currencies (`{ currency: "coin", amount: 120 }`); real-money prices are strings your store SDK already formatted (`{ real: "$4.99" }`). Call the App Store or Google Play in-app purchase API (required for digital goods in mobile apps) or Stripe on the web from `onConfirm`, and return its Promise: the dialog shows busy, then success or the error with a retry.

Store.tsx:

```tsx
import { CurrencyBar, Shop, PurchaseDialog, spend } from "@0xtrou/cozy-ui"

<Shop items={stock} wallet={wallet} categories={tabs} onBuy={setPicked} onSelect={setPicked}
  headerEnd={<CurrencyBar wallet={wallet} onAdd={() => setTab("gems")} highlight={short} />} />

<PurchaseDialog
  open={!!picked} onOpenChange={(o) => !o && setPicked(null)}
  item={picked} wallet={wallet} maxQuantity={10}
  realMoneyNote="Charged by the App Store"
  onGetMore={(currency) => { setPicked(null); setTab("gems"); setShort(currency) }}
  onConfirm={(item, qty) => item.data.iap
    ? store.purchase(item.data.sku)                    // your IAP / Stripe call, returns a Promise
    : game.buy(item.id, qty)}                          // or spend(wallet, price) for in-game coins
/>
```

## Perks, skills and buffs

The game keeps the rules: which perks are offered, how many points the player has, when a buff ends. The components show them and report choices. Skill points are yours to count; `spentPoints(ranks, nodes)` and `canLearn(...)` help on the server too.

LevelUp.tsx:

```tsx
import { PerkPicker, SkillTree, BuffBar, spentPoints } from "@0xtrou/cozy-ui"

<PerkPicker perks={offer} rerolls={rerolls} onReroll={reroll} onConfirm={(perk) => game.takePerk(perk.id)} />

<SkillTree nodes={tree} ranks={ranks} onRanksChange={(next, change) => game.learn(change.id, change.to)}
  points={level * 2 - spentPoints(ranks, tree)} level={level} allowRefund />

<BuffBar buffs={effects.map((e) => ({ id: e.id, name: e.name, icon: e.icon, endsAt: e.endsAt }))}
  onExpire={(b) => game.removeEffect(b.id)} />
```

## Rarities, elements and look

- Rarity colours come from the palette (`--cz-rarity-common` … `--cz-rarity-legendary`). Pass `rarities` to use your own names and colours.
- Card elements come from `DEFAULT_ELEMENTS`; pass `elements` with your own set.
- Every `render*` prop (renderCard, renderDetail, renderArt, renderMarker, renderDetails…) swaps one part and keeps the rest.
- All five [palettes](https://cozy-ui.solo.engineer/docs/palettes/) and the [glass material](https://cozy-ui.solo.engineer/docs/materials/) apply to the game components too.

## Input and accessibility

- Mouse, touch and keyboard everywhere: drag items or press Space to pick up and arrow keys to move; arrow keys and +/− on the map; Enter on cards.
- Changes are announced to screen readers, and every control has a name.
- Tilt, foil, map glide and other motion turn off for people who ask for reduced motion.

## Game engines

For web games (React, Phaser or PixiJS with a React overlay, Three.js) use the components directly over your canvas. For Unity, the same tokens and class names map to UI Toolkit: see the [Unity guide](https://cozy-ui.solo.engineer/docs/unity/).

Docs: https://cozy-ui.solo.engineer/docs/game-ui/
