Last active
May 25, 2020 08:23
-
-
Save Joncom/387a0b1a10dc304ec51a0320fce58682 to your computer and use it in GitHub Desktop.
Helper functions for ImpactJS to double or halve the tilesize of a BackgroundMap (without breaking their appearance)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function halveTilesize( layer: Layer, tileset: Image ) { | |
if( layer.tilesize % 2 !== 0 || (layer.name !== 'collision' && !tileset.loaded) ) { | |
return false; | |
} | |
const oldTileWidth = tileset.width / layer.tilesize; | |
const newTileWidth = (tileset.width / layer.tilesize) * 2; | |
const newData = []; | |
for( let y = 0; y < layer.height; y++ ) { | |
newData[y * 2] = []; | |
newData[y * 2 + 1] = []; | |
for( let x = 0; x < layer.width; x++ ) { | |
const oldTile = layer.data[y][x] - 1; | |
if( oldTile === -1 ) { // Empty tile | |
newData[y * 2][x * 2] = 0; | |
newData[y * 2][x * 2 + 1] = 0; | |
newData[y * 2 + 1][x * 2] = 0; | |
newData[y * 2 + 1][x * 2 + 1] = 0; | |
} else { | |
let a: number, b: number, c: number, d: number; | |
if (layer.name === 'collision') { | |
a = b = c = d = oldTile + 1; | |
} else { | |
const oldTilesetY = Math.floor(oldTile / oldTileWidth); | |
const oldTilesetX = oldTile - oldTilesetY * oldTileWidth; | |
const newTilesetX = oldTilesetX * 2; | |
const newTilesetY = oldTilesetY * 2; | |
a = (newTilesetY + 0) * newTileWidth + (newTilesetX + 0) + 1; | |
b = (newTilesetY + 0) * newTileWidth + (newTilesetX + 1) + 1; | |
c = (newTilesetY + 1) * newTileWidth + (newTilesetX + 0) + 1; | |
d = (newTilesetY + 1) * newTileWidth + (newTilesetX + 1) + 1; | |
} | |
newData[y * 2][x * 2] = a; | |
newData[y * 2][x * 2 + 1] = b; | |
newData[y * 2 + 1][x * 2] = c; | |
newData[y * 2 + 1][x * 2 + 1] = d; | |
} | |
} | |
} | |
layer.tilesize /= 2; | |
layer.width *= 2; | |
layer.height *= 2; | |
layer.data = newData; | |
return true; | |
} | |
export function doubleTilesize( layer: Layer, tileset: Image ) { | |
if( layer.width % 2 !== 0 || layer.height % 2 !== 0 || (layer.name !== 'collision' && !tileset.loaded) ) { | |
return false; | |
} | |
const oldTileWidth = tileset.width / layer.tilesize; | |
const newTileWidth = (tileset.width / layer.tilesize) / 2; | |
if( newTileWidth % 1 !== 0 ) { | |
return false; | |
} | |
const newData = []; | |
for( let y = 0; y < layer.height; y += 2 ) { | |
newData[y / 2] = []; | |
for( let x = 0; x < layer.width; x += 2 ) { | |
const oldTile = layer.data[y][x] - 1; | |
if( oldTile === -1 ) { // Empty tile | |
newData[y / 2][x / 2] = 0; | |
} else { | |
if(layer.name === 'collision') { | |
newData[y / 2][x / 2] = oldTile + 1; | |
} else { | |
const oldTilesetY = Math.floor(oldTile / oldTileWidth); | |
const oldTilesetX = oldTile - oldTilesetY * oldTileWidth; | |
const newTilesetX = oldTilesetX / 2; | |
const newTilesetY = oldTilesetY / 2; | |
newData[y / 2][x / 2] = newTilesetY * newTileWidth + newTilesetX + 1; | |
} | |
} | |
} | |
} | |
layer.tilesize *= 2; | |
layer.width /= 2; | |
layer.height /= 2; | |
layer.data = newData; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment