map-diff: move get tile logic to images module
This commit is contained in:
parent
3bb7f887b6
commit
77a7b1fa2f
2 changed files with 37 additions and 28 deletions
|
@ -3,7 +3,11 @@ import Jimp from 'jimp'
|
|||
import { parseWmlFile } from './wml/parser'
|
||||
import rootByTagName from './wml/rootByTagName'
|
||||
|
||||
type ImagesDict = { tile: { [baseCode: string]: Jimp }, focus: Jimp, flag: Jimp }
|
||||
type ImagesGetter = {
|
||||
getTile: ({ baseCode, miscCode }: { baseCode: string, miscCode?: string }) => Jimp,
|
||||
focus: Jimp,
|
||||
flag: Jimp,
|
||||
}
|
||||
|
||||
const getDictTerrainType2ImagesPath = async () => {
|
||||
const terratinCfgPath = path.resolve(__dirname, '../../../data/core/terrain.cfg')
|
||||
|
@ -36,25 +40,42 @@ const readTerrainImages = async () => {
|
|||
return [terrainType, image]
|
||||
})
|
||||
|
||||
const result = Object.fromEntries(await Promise.all(promises))
|
||||
const result: { [baseCode: string]: Jimp } = Object.fromEntries(await Promise.all(promises))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
const getImages = async (): Promise<ImagesDict> => {
|
||||
const produceImagesGetter = async (): Promise<ImagesGetter> => {
|
||||
const focusPath = path.resolve(__dirname, '../../../images/editor/brush.png')
|
||||
const flagPath = path.resolve(__dirname, '../../../data/core/images/flags/flag-1.png')
|
||||
|
||||
const mapTileCodeToImage = readTerrainImages()
|
||||
const focus = Jimp.read(focusPath)
|
||||
const flag = Jimp.read(flagPath)
|
||||
const [images, focus, flag] = await Promise.all([
|
||||
readTerrainImages(),
|
||||
Jimp.read(focusPath),
|
||||
Jimp.read(flagPath),
|
||||
])
|
||||
|
||||
return {
|
||||
tile: await mapTileCodeToImage,
|
||||
focus: await focus,
|
||||
flag: await flag,
|
||||
getTile: ({ baseCode, miscCode }) => {
|
||||
if (miscCode && images[`${baseCode}^${miscCode}`]) {
|
||||
const compoundImage = images[`${baseCode}^${miscCode}`]
|
||||
return compoundImage
|
||||
}
|
||||
|
||||
const baseImage = images[baseCode]
|
||||
|
||||
// todo: we should use the defaultBase correctly
|
||||
if (miscCode) {
|
||||
const miscImage = images[`^${miscCode}`]
|
||||
return baseImage.clone().composite(miscImage, 0, 0)
|
||||
}
|
||||
|
||||
return baseImage
|
||||
},
|
||||
focus,
|
||||
flag,
|
||||
}
|
||||
}
|
||||
|
||||
export { ImagesDict }
|
||||
export default getImages
|
||||
export { ImagesGetter }
|
||||
export default produceImagesGetter
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import Jimp from 'jimp'
|
||||
import * as tilemap from './tilemap'
|
||||
import type { Tilemap } from './tilemap'
|
||||
import type { ImagesDict } from './images'
|
||||
import type { ImagesGetter } from './images'
|
||||
|
||||
type Side = 'left' | 'right'
|
||||
|
||||
|
@ -25,22 +25,10 @@ const getTileImageCoordenates = (tileX: number, tileY: number) => {
|
|||
return [imageX, imageY]
|
||||
}
|
||||
|
||||
const producePainters = (output: Jimp, images: ImagesDict, leftPadding: number) => {
|
||||
const producePainters = (output: Jimp, images: ImagesGetter, leftPadding: number) => {
|
||||
const paintTile = (x: number, y: number, baseCode: string, miscCode?: string) => {
|
||||
if (miscCode && images.tile[`${baseCode}^${miscCode}`]) {
|
||||
const compoundImage = images.tile[`${baseCode}^${miscCode}`]
|
||||
output.composite(compoundImage, x + leftPadding, y)
|
||||
return
|
||||
}
|
||||
|
||||
const baseImage = images.tile[baseCode]
|
||||
output.composite(baseImage, x + leftPadding, y)
|
||||
|
||||
// todo: we should use the defaultBase correctly
|
||||
if (miscCode) {
|
||||
const miscImage = images.tile[`^${miscCode}`]
|
||||
output.composite(miscImage, x + leftPadding, y)
|
||||
}
|
||||
const tileImage = images.getTile({ baseCode, miscCode })
|
||||
output.composite(tileImage, x + leftPadding, y)
|
||||
}
|
||||
|
||||
// todo: the flag color should follow the player number
|
||||
|
@ -59,7 +47,7 @@ const paint = async (
|
|||
oldTilemap: Tilemap,
|
||||
newTilemap: Tilemap,
|
||||
outputFilename: string,
|
||||
images: ImagesDict
|
||||
images: ImagesGetter
|
||||
) => {
|
||||
const { height, width } = imageSize(oldTilemap)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue