89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
TEMPLATE_DIR = ROOT / "templates/2d-character-v1"
|
|
OUTPUT_ROOT = ROOT / "assets/images/2d-game-characters"
|
|
ID_PATTERN = re.compile(r"^\d{2,3}-[a-z0-9][a-z0-9-]*$")
|
|
|
|
DIRECTORIES = (
|
|
"source/up",
|
|
"source/down",
|
|
"source/left",
|
|
"source/right",
|
|
"parts/up/v1",
|
|
"parts/down/v1",
|
|
"parts/left/v1",
|
|
"parts/right/v1",
|
|
"assembled/up",
|
|
"assembled/down",
|
|
"assembled/left",
|
|
"assembled/right",
|
|
"motion/up/v1",
|
|
"motion/down/v1",
|
|
"motion/left/v1",
|
|
"motion/right/v1",
|
|
"previews/playback",
|
|
"diagnostics",
|
|
)
|
|
|
|
FILES = {
|
|
"character-config.template.json": "character-config-v1.json",
|
|
"direction-map.template.json": "direction-map-v1.json",
|
|
"template-package.template.json": "template-package-v1.json",
|
|
"character-readme.template.md": "README.md",
|
|
}
|
|
|
|
|
|
def replace_tokens(text: str, character_id: str, name_zh: str, name_en: str) -> str:
|
|
return (
|
|
text.replace("__CHARACTER_ID__", character_id)
|
|
.replace("__CHARACTER_NAME_ZH__", name_zh)
|
|
.replace("__CHARACTER_NAME_EN__", name_en)
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="Create a safe OSM four-direction 2D character scaffold.")
|
|
parser.add_argument("character_id", help="Lowercase id such as 03-example")
|
|
parser.add_argument("--name-zh", required=True, help="Chinese display name")
|
|
parser.add_argument("--name-en", default="", help="Optional English display name")
|
|
parser.add_argument("--dry-run", action="store_true", help="Print planned paths without writing")
|
|
args = parser.parse_args()
|
|
|
|
if not ID_PATTERN.fullmatch(args.character_id):
|
|
raise SystemExit("character_id must match NN-lowercase-slug, for example 03-example")
|
|
target = OUTPUT_ROOT / args.character_id
|
|
if target.exists():
|
|
raise SystemExit(f"Refusing to overwrite existing character directory: {target}")
|
|
|
|
planned = [target / directory for directory in DIRECTORIES]
|
|
planned.extend(target / output for output in FILES.values())
|
|
if args.dry_run:
|
|
print(f"Dry run: {target}")
|
|
for path in planned:
|
|
print(path.relative_to(ROOT).as_posix())
|
|
return
|
|
|
|
for directory in DIRECTORIES:
|
|
(target / directory).mkdir(parents=True, exist_ok=False)
|
|
for template_name, output_name in FILES.items():
|
|
source = TEMPLATE_DIR / template_name
|
|
content = replace_tokens(source.read_text(encoding="utf-8"), args.character_id, args.name_zh, args.name_en)
|
|
destination = target / output_name
|
|
destination.write_text(content, encoding="utf-8")
|
|
if destination.suffix == ".json":
|
|
json.loads(content)
|
|
|
|
print(f"Created character scaffold: {target}")
|
|
print(f"Start with: {target / 'character-config-v1.json'}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|