107 lines
4.7 KiB
Python
107 lines
4.7 KiB
Python
# Docs inputs for the 2D pages (tools/fixtures.py; see tools/docs_check.py): a layered PSD written by ImageMagick (three
|
|
# layers: Sky, Box, a half-transparent Glow; the merged image flattened by ImageMagick), a two-frame Aseprite sprite
|
|
# (built with the chunk writer of tools/fixtures/aseprite.py), an SVG icon and a Tiled map as TMX and TMJ (hand-written
|
|
# from the Tiled format docs) with its tileset image tiles.png (ImageMagick).
|
|
# tests/gen/docs_2d.luau { files, vars = { psdBytes, asepriteBytes, svgText, tmxText, tmjText, tilesPngBytes } }
|
|
import importlib.util, os, struct, tempfile
|
|
|
|
INPUTS = ["tools/docs_check.py", "tools/fixtures/aseprite.py"]
|
|
|
|
|
|
def load(ctx, name):
|
|
spec = importlib.util.spec_from_file_location("docs2d_" + name, os.path.join(ctx.root, "tools", *name.split("/")))
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
return m
|
|
|
|
|
|
SVG = """<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 32 32">
|
|
<defs>
|
|
<linearGradient id="sky" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="0" stop-color="#3070c0"/>
|
|
<stop offset="1" stop-color="#a0d0ff"/>
|
|
</linearGradient>
|
|
</defs>
|
|
<rect width="32" height="32" rx="4" fill="url(#sky)"/>
|
|
<circle cx="22" cy="10" r="5" fill="#ffd040"/>
|
|
<path d="M2 30 L12 16 L20 26 L24 21 L30 30 Z" fill="#306030" stroke="#103010" stroke-width="1"/>
|
|
</svg>
|
|
"""
|
|
|
|
# a 4 x 3 map of 16 x 16 tiles: one tileset (tiles.png, 4 columns), a tile layer, an object layer with a spawn point
|
|
TMX = """<?xml version="1.0" encoding="UTF-8"?>
|
|
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="4" height="3" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="2">
|
|
<properties>
|
|
<property name="music" value="cave.ogg"/>
|
|
<property name="gravity" type="float" value="9.8"/>
|
|
</properties>
|
|
<tileset firstgid="1" name="tiles" tilewidth="16" tileheight="16" tilecount="8" columns="4">
|
|
<image source="tiles.png" width="64" height="32"/>
|
|
</tileset>
|
|
<layer id="1" name="ground" width="4" height="3">
|
|
<data encoding="csv">
|
|
1,2,2,3,
|
|
5,0,0,7,
|
|
6,6,6,6
|
|
</data>
|
|
</layer>
|
|
<objectgroup id="2" name="things">
|
|
<object id="1" name="spawn" type="Spawn" x="24" y="16">
|
|
<point/>
|
|
</object>
|
|
</objectgroup>
|
|
</map>
|
|
"""
|
|
|
|
TMJ = """{ "type": "map", "version": "1.10", "orientation": "orthogonal", "renderorder": "right-down",
|
|
"width": 2, "height": 2, "tilewidth": 8, "tileheight": 8, "infinite": false, "nextlayerid": 2, "nextobjectid": 1,
|
|
"tilesets": [ { "firstgid": 1, "name": "t", "tilewidth": 8, "tileheight": 8, "tilecount": 4, "columns": 2,
|
|
"image": "t.png", "imagewidth": 16, "imageheight": 16 } ],
|
|
"layers": [ { "id": 1, "name": "main", "type": "tilelayer", "width": 2, "height": 2, "x": 0, "y": 0,
|
|
"opacity": 1, "visible": true, "data": [ 1, 2, 3, 2147483652 ] } ] }
|
|
"""
|
|
|
|
|
|
def aseprite(ctx):
|
|
A = load(ctx, "fixtures/aseprite.py")
|
|
W, H = 32, 24
|
|
sky = bytes((40, 90, 160, 255)) * (W * H)
|
|
hero = bytes((220, 50, 50, 255)) * 64
|
|
f1 = [
|
|
A.layer_chunk("Background", flags=1 | 2 | 8),
|
|
A.layer_chunk("Hero"),
|
|
A.tags_chunk([("walk", 0, 1, 0, 0)]),
|
|
A.cel_chunk(0, 0, 0, W, H, sky),
|
|
A.cel_chunk(1, 4, 8, 8, 8, hero),
|
|
]
|
|
f2 = [
|
|
A.cel_chunk(0, 0, 0, W, H, b"", link=0),
|
|
A.cel_chunk(1, 12, 8, 8, 8, hero),
|
|
]
|
|
return A.sprite(W, H, 32, [(100, f1), (250, f2)])
|
|
|
|
|
|
def generate(ctx):
|
|
dc = load(ctx, "docs_check.py")
|
|
files = {"icon.svg": SVG, "level.tmx": TMX, "level.tmj": TMJ, "sprite.aseprite": aseprite(ctx)}
|
|
with tempfile.TemporaryDirectory() as d:
|
|
out = os.path.join(d, "art.psd")
|
|
ctx.nix("imagemagick", ["magick",
|
|
"(", "-size", "48x32", "xc:#3070c0", "-set", "label", "Sky", ")",
|
|
"(", "-size", "16x16", "xc:#e04040", "-set", "page", "+8+8", "-set", "label", "Box", ")",
|
|
"(", "-size", "20x10", "xc:rgba(255,255,0,0.5)", "-set", "page", "+24+18", "-set", "label", "Glow", ")",
|
|
"(", "-clone", "0--1", "-background", "none", "-flatten", ")", "-insert", "0",
|
|
"-depth", "8", "-type", "TrueColorAlpha", out])
|
|
files["art.psd"] = open(out, "rb").read()
|
|
tiles = os.path.join(d, "tiles.png")
|
|
ctx.nix("imagemagick", ["magick", "-size", "64x32", "pattern:checkerboard", "-depth", "8", tiles])
|
|
files["tiles.png"] = open(tiles, "rb").read()
|
|
dc.emit_inputs(ctx, files, {
|
|
"psdBytes": "art.psd",
|
|
"asepriteBytes": "sprite.aseprite",
|
|
"svgText": "icon.svg",
|
|
"tmxText": "level.tmx",
|
|
"tmjText": "level.tmj",
|
|
"tilesPngBytes": "tiles.png",
|
|
})
|