added backwards compatible check and maps

This commit is contained in:
jonny 2021-06-25 17:35:42 +02:00
parent 54d392be82
commit f536d538ea
5 changed files with 142 additions and 11 deletions

View File

@ -17,6 +17,8 @@ export class GameMap {
public exitUrls: Array<string> = [] public exitUrls: Array<string> = []
public hasStartTile = false;
public constructor(private map: ITiledMap) { public constructor(private map: ITiledMap) {
this.layersIterator = new LayersIterator(map); this.layersIterator = new LayersIterator(map);
@ -27,6 +29,8 @@ export class GameMap {
tile.properties.forEach(prop => { tile.properties.forEach(prop => {
if (prop.name == "exitUrl" && typeof prop.value == "string") { if (prop.name == "exitUrl" && typeof prop.value == "string") {
this.exitUrls.push(prop.value); this.exitUrls.push(prop.value);
} else if (prop.name == "start") {
this.hasStartTile = true
} }
}) })
} }

View File

@ -1043,7 +1043,7 @@ ${escapedMessage}
} else { } else {
// Now, let's find the start layer // Now, let's find the start layer
if (this.startLayerName) { if (this.startLayerName) {
this.initPositionFromLayerName(this.startLayerName, null); this.initPositionFromLayerName(this.startLayerName, this.startLayerName);
} }
if (this.startX === undefined) { if (this.startX === undefined) {
// If we have no start layer specified or if the hash passed does not exist, let's go with the default start position. // If we have no start layer specified or if the hash passed does not exist, let's go with the default start position.
@ -1059,10 +1059,10 @@ ${escapedMessage}
} }
} }
private initPositionFromLayerName(layerName: string, startLayerName: string | null) { private initPositionFromLayerName(selectedOrdDefaultLayer: string, selectedLayer: string | null) {
for (const layer of this.gameMap.layersIterator) { for (const layer of this.gameMap.layersIterator) {
if ((layerName === layer.name || layer.name.endsWith('/' + layerName)) && layer.type === 'tilelayer' && (layerName === defaultStartLayerName || this.isStartLayer(layer))) { if ((selectedOrdDefaultLayer === layer.name || layer.name.endsWith('/' + selectedOrdDefaultLayer)) && layer.type === 'tilelayer' && (selectedOrdDefaultLayer === defaultStartLayerName || this.isStartLayer(layer))) {
const startPosition = this.startUser(layer, startLayerName); const startPosition = this.startUser(layer, selectedLayer);
this.startX = startPosition.x + this.mapFile.tilewidth / 2; this.startX = startPosition.x + this.mapFile.tilewidth / 2;
this.startY = startPosition.y + this.mapFile.tileheight / 2; this.startY = startPosition.y + this.mapFile.tileheight / 2;
} }
@ -1116,8 +1116,8 @@ ${escapedMessage}
return gameManager.loadMap(room, this.scene).catch(() => { }); return gameManager.loadMap(room, this.scene).catch(() => { });
} }
private startUser(layer: ITiledMapTileLayer, startName: string | null): PositionInterface { private startUser(selectedOrDefaultLayer: ITiledMapTileLayer, startName: string | null): PositionInterface {
const tiles = layer.data; const tiles = selectedOrDefaultLayer.data;
if (typeof (tiles) === 'string') { if (typeof (tiles) === 'string') {
throw new Error('The content of a JSON map must be filled as a JSON array, not as a string'); throw new Error('The content of a JSON map must be filled as a JSON array, not as a string');
} }
@ -1126,21 +1126,20 @@ ${escapedMessage}
if (objectKey === 0) { if (objectKey === 0) {
return; return;
} }
const y = Math.floor(key / layer.width); const y = Math.floor(key / selectedOrDefaultLayer.width);
const x = key % layer.width; const x = key % selectedOrDefaultLayer.width;
if (startName) { if (startName && this.gameMap.hasStartTile) {
const properties = this.gameMap.getPropertiesForIndex(objectKey); const properties = this.gameMap.getPropertiesForIndex(objectKey);
if (!properties.length || !properties.some(property => property.name == "start" && property.value == startName)) { if (!properties.length || !properties.some(property => property.name == "start" && property.value == startName)) {
return return
} }
} }
possibleStartPositions.push({ x: x * this.mapFile.tilewidth, y: y * this.mapFile.tilewidth }); possibleStartPositions.push({ x: x * this.mapFile.tilewidth, y: y * this.mapFile.tilewidth });
}); });
// Get a value at random amongst allowed values // Get a value at random amongst allowed values
if (possibleStartPositions.length === 0) { if (possibleStartPositions.length === 0) {
console.warn('The start layer "' + layer.name + '" for this map is empty.'); console.warn('The start layer "' + selectedOrDefaultLayer.name + '" for this map is empty.');
return { return {
x: 0, x: 0,
y: 0 y: 0

View File

@ -0,0 +1,33 @@
{ "columns":2,
"image":"function_tiles.png",
"imageheight":64,
"imagewidth":64,
"margin":0,
"name":"function_tiles",
"spacing":0,
"tilecount":4,
"tiledversion":"1.6.0",
"tileheight":32,
"tiles":[
{
"id":0,
"properties":[
{
"name":"start",
"type":"string",
"value":"S1"
}]
},
{
"id":1,
"properties":[
{
"name":"start",
"type":"string",
"value":"S2"
}]
}],
"tilewidth":32,
"type":"tileset",
"version":"1.6"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -0,0 +1,95 @@
{
"compressionlevel": -1,
"height": 5,
"infinite": false,
"layers": [
{
"data": [
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4
],
"height": 5,
"id": 4,
"name": "background",
"opacity": 1,
"type": "tilelayer",
"visible": true,
"width": 5,
"x": 0,
"y": 0
},
{
"data": [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 2, 3, 3, 0, 2,
2
],
"height": 5,
"id": 1,
"name": "start",
"opacity": 1,
"type": "tilelayer",
"visible": true,
"width": 5,
"x": 0,
"y": 0
},
{
"draworder": "topdown",
"id": 3,
"name": "floorLayer",
"objects": [],
"opacity": 1,
"type": "objectgroup",
"visible": true,
"x": 0,
"y": 0
}
],
"nextlayerid": 5,
"nextobjectid": 1,
"orientation": "orthogonal",
"renderorder": "right-down",
"tiledversion": "1.6.0",
"tileheight": 32,
"tilesets": [
{
"columns": 2,
"firstgid": 1,
"image": "function_tiles.png",
"imageheight": 64,
"imagewidth": 64,
"margin": 0,
"name": "function_tiles",
"spacing": 0,
"tilecount": 4,
"tileheight": 32,
"tiles": [
{
"id": 0,
"properties": [
{
"name": "start",
"type": "string",
"value": "S1"
}
]
},
{
"id": 1,
"properties": [
{
"name": "start",
"type": "string",
"value": "S2"
}
]
}
],
"tilewidth": 32
}
],
"tilewidth": 32,
"type": "map",
"version": "1.6",
"width": 5
}