#!/bin/bash
#
# Stage the sprites a `json kind=character` block names.
#
# akgl_character_load_json resolves every `sprite` field against
# AKGL_REGISTRY_SPRITE and fails on a name that is not there, so a chapter that
# shows a character file on its own is not runnable on its own. Rather than make
# the chapter print two sprite files it is not about, the sprites live here and
# tools/docs_checkjson.c loads everything in ./sprites through
# akgl_sprite_load_json before it touches the character.
#
# **The two sprite names are a contract with docs/11-characters.md**, which names
# `hero standing left` and `hero walking left` in its `sprite_mappings`. Changing
# either name here breaks that chapter, so change both together.
#
# The sheet is staged inside ./sprites because a sprite file's `spritesheet`
# path is resolved relative to the sprite file's own directory.
#
# $1 is the repository root. The caller runs this with the sandbox as the
# working directory.

set -u

ROOT="${1:-}"
if [ -z "${ROOT}" ]; then
    echo "usage: character.sh <repository-root>" >&2
    exit 2
fi

mkdir -p sprites || exit 1
cp "${ROOT}/tests/assets/spritesheet.png" sprites/spritesheet.png || exit 1

# 576x384 of 48x48 frames: a 12x8 grid, so frames 0-95 exist. Two frames each,
# which is enough for the animation fields to mean something without making the
# fixture a thing anybody has to study.
cat > sprites/hero_standing_left.json <<'JSON'
{
    "spritesheet": {
	"filename": "spritesheet.png",
	"frame_width": 48,
	"frame_height": 48
    },
    "name": "hero standing left",
    "width": 48,
    "height": 48,
    "speed": 200,
    "loop": true,
    "loopReverse": false,
    "frames": [0]
}
JSON

cat > sprites/hero_walking_left.json <<'JSON'
{
    "spritesheet": {
	"filename": "spritesheet.png",
	"frame_width": 48,
	"frame_height": 48
    },
    "name": "hero walking left",
    "width": 48,
    "height": 48,
    "speed": 100,
    "loop": true,
    "loopReverse": true,
    "frames": [0, 1, 2]
}
JSON
