#!/usr/bin/env bash
# Regenerates ALL art + maps from source scripts. Requires Aseprite + Tiled installed.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ASE="${ASEPRITE:-/Applications/Aseprite.app/Contents/MacOS/aseprite}"
TILED="${TILED:-/Applications/Tiled.app/Contents/MacOS/Tiled}"
SPRITES="$ROOT/public/assets/sprites"
MAPS="$ROOT/public/assets/maps"
mkdir -p "$SPRITES" "$MAPS"

echo "== [1/3] Aseprite: pixel art =="
"$ASE" -b --script-param outdir="$SPRITES" --script "$ROOT/tools/gen_all.lua"

echo "== [2/3] Node: Tiled .tmx sources =="
node "$ROOT/tools/gen_maps.mjs"

echo "== [3/3] Tiled: export .tmx -> .json =="
for f in "$ROOT/tools/tmx/"*.tmx; do
  name="$(basename "$f" .tmx)"
  "$TILED" --export-map "$f" "$MAPS/$name.json"
  echo "  exported $name.json"
done
echo "== assets built =="
