Skip to content

Commit

Permalink
Allow to filter external tile map collision per layer (#6998)
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H authored Oct 2, 2024
1 parent fe743bb commit f6cb203
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 59 deletions.
45 changes: 45 additions & 0 deletions Extensions/TileMap/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,14 @@ const defineCollisionMask = function (extension, _, gd) {
objectContent.collisionMaskTag = newValue;
return true;
}
if (propertyName === 'layerIndex') {
objectContent.layerIndex = parseFloat(newValue);
return true;
}
if (propertyName === 'useAllLayers') {
objectContent.useAllLayers = newValue === '1';
return true;
}
if (propertyName === 'debugMode') {
objectContent.debugMode = newValue === '1';
return true;
Expand Down Expand Up @@ -1149,6 +1157,28 @@ const defineCollisionMask = function (extension, _, gd) {
)
)
);
objectProperties.set(
'layerIndex',
new gd.PropertyDescriptor((objectContent.layerIndex || 1).toString())
.setType('number')
.setLabel(_('Layer index'))
.setGroup(_('Layers'))
.setAdvanced(true)
);
objectProperties.set(
'useAllLayers',
new gd.PropertyDescriptor(
objectContent.useAllLayers ||
objectContent.useAllLayers === undefined ||
objectContent.useAllLayers === null
? 'true'
: 'false'
)
.setType('boolean')
.setLabel(_('Use all layers'))
.setGroup(_('Layers'))
.setAdvanced(true)
);
objectProperties.set(
'debugMode',
new gd.PropertyDescriptor(objectContent.debugMode ? 'true' : 'false')
Expand All @@ -1157,12 +1187,14 @@ const defineCollisionMask = function (extension, _, gd) {
.setDescription(
_('When activated, it displays the hitboxes in the given color.')
)
.setGroup(_('Appearance'))
);
objectProperties.set(
'outlineColor',
new gd.PropertyDescriptor(objectContent.outlineColor)
.setType('color')
.setLabel(_('Outline color'))
.setGroup(_('Appearance'))
);
objectProperties.set(
'outlineOpacity',
Expand All @@ -1173,6 +1205,7 @@ const defineCollisionMask = function (extension, _, gd) {
)
.setType('number')
.setLabel(_('Outline opacity (0-255)'))
.setGroup(_('Appearance'))
);
objectProperties.set(
'outlineSize',
Expand All @@ -1183,12 +1216,14 @@ const defineCollisionMask = function (extension, _, gd) {
)
.setType('number')
.setLabel(_('Outline size (in pixels)'))
.setGroup(_('Appearance'))
);
objectProperties.set(
'fillColor',
new gd.PropertyDescriptor(objectContent.fillColor)
.setType('color')
.setLabel(_('Fill color'))
.setGroup(_('Appearance'))
);
objectProperties.set(
'fillOpacity',
Expand All @@ -1199,6 +1234,7 @@ const defineCollisionMask = function (extension, _, gd) {
)
.setType('number')
.setLabel(_('Fill opacity (0-255)'))
.setGroup(_('Appearance'))
);

return objectProperties;
Expand All @@ -1207,6 +1243,8 @@ const defineCollisionMask = function (extension, _, gd) {
tilemapJsonFile: '',
tilesetJsonFile: '',
collisionMaskTag: '',
layerIndex: 1,
useAllLayers: true,
debugMode: false,
fillColor: '255;255;255',
outlineColor: '255;255;255',
Expand Down Expand Up @@ -2381,6 +2419,7 @@ module.exports = {
_tilemapJsonFile = '';
_tilesetJsonFile = '';
_collisionMaskTag = '';
_layerIndex = null;
_outlineColor = 0xffffff;
_fillColor = 0xffffff;
_outlineOpacity = 0;
Expand Down Expand Up @@ -2481,6 +2520,7 @@ module.exports = {
const tilemapJsonFile = this._tilemapJsonFile;
const tilesetJsonFile = this._tilesetJsonFile;
const collisionMaskTag = this._collisionMaskTag;
const layerIndex = this._layerIndex;
const outlineColor = this._outlineColor;
const fillColor = this._fillColor;
const outlineOpacity = this._outlineOpacity;
Expand Down Expand Up @@ -2509,6 +2549,7 @@ module.exports = {
this._pixiObject,
tileMap,
collisionMaskTag,
layerIndex,
outlineSize,
outlineColor,
outlineOpacity,
Expand Down Expand Up @@ -2566,6 +2607,8 @@ module.exports = {
const tilemapJsonFile = object.content.tilemapJsonFile;
const tilesetJsonFile = object.content.tilesetJsonFile;
const collisionMaskTag = object.content.collisionMaskTag;
const useAllLayers = object.content.useAllLayers;
const layerIndex = useAllLayers ? null : object.content.layerIndex;
const outlineColor = objectsRenderingService.rgbOrHexToHexNumber(
object.content.outlineColor
);
Expand All @@ -2580,6 +2623,7 @@ module.exports = {
tilemapJsonFile !== this._tilemapJsonFile ||
tilesetJsonFile !== this._tilesetJsonFile ||
collisionMaskTag !== this._collisionMaskTag ||
layerIndex !== this._layerIndex ||
outlineColor !== this._outlineColor ||
fillColor !== this._fillColor ||
outlineOpacity !== this._outlineOpacity ||
Expand All @@ -2589,6 +2633,7 @@ module.exports = {
this._tilemapJsonFile = tilemapJsonFile;
this._tilesetJsonFile = tilesetJsonFile;
this._collisionMaskTag = collisionMaskTag;
this._layerIndex = layerIndex;
this._outlineColor = outlineColor;
this._fillColor = fillColor;
this._outlineOpacity = outlineOpacity;
Expand Down
31 changes: 24 additions & 7 deletions Extensions/TileMap/collision/TransformedTileMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace gdjs {
*/
private _source: TileMapHelper.EditableTileMap;
tag: string;
private _layerIndex: integer | null;
private _layers: Map<integer, TransformedCollisionTileMapLayer>;
// TODO Tiled allows to offset the layers
/**
Expand All @@ -37,9 +38,14 @@ namespace gdjs {
/**
* @param source The model that describes the tile map.
*/
constructor(source: TileMapHelper.EditableTileMap, tag: string) {
constructor(
source: TileMapHelper.EditableTileMap,
tag: string,
layerIndex: number | null = null
) {
this._source = source;
this.tag = tag;
this._layerIndex = layerIndex;
this._layers = new Map<integer, TransformedCollisionTileMapLayer>();
this._buildLayersFromTileMap(source, this._layers);
}
Expand All @@ -55,17 +61,28 @@ namespace gdjs {
tileMap: TileMapHelper.EditableTileMap,
layers: Map<integer, TransformedCollisionTileMapLayer>
) {
for (const sourceLayer of tileMap.getLayers()) {
// TODO A visitor could be used to avoid a cast.
if (!(sourceLayer instanceof TileMapHelper.EditableTileMapLayer)) {
// TODO Collision mask for object layers is not handled.
continue;
if (this._layerIndex) {
const tileLayer = tileMap.getTileLayer(this._layerIndex);
if (!tileLayer) {
return;
}
const tileLayer = sourceLayer as TileMapHelper.EditableTileMapLayer;
layers.set(
tileLayer.id,
new TransformedCollisionTileMapLayer(this, tileLayer)
);
} else {
for (const sourceLayer of tileMap.getLayers()) {
// TODO A visitor could be used to avoid a cast.
if (!(sourceLayer instanceof TileMapHelper.EditableTileMapLayer)) {
// TODO Collision mask for object layers is not handled.
continue;
}
const tileLayer = sourceLayer as TileMapHelper.EditableTileMapLayer;
layers.set(
tileLayer.id,
new TransformedCollisionTileMapLayer(this, tileLayer)
);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/TileMapHelper.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Extensions/TileMap/helper/TileMapHelper.js.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export declare namespace PixiTileMapHelper {
pixiGraphics: PIXI.Graphics,
tileMap: EditableTileMap,
typeFilter: string,
layerIndex: integer | null,
outlineSize: integer,
outlineColor: integer,
outlineOpacity: float,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Extensions/TileMap/tilemapcollisionmaskruntimeobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace gdjs {
* For instance, platforms, jumpthru, ladder, spike, water...
*/
private _collisionMaskTag: string;
private _layerIndex: integer | null;
private _tileMapManager: gdjs.TileMap.TileMapRuntimeManager;

/**
Expand Down Expand Up @@ -80,6 +81,11 @@ namespace gdjs {
this._tilemapJsonFile = objectData.content.tilemapJsonFile;
this._tilesetJsonFile = objectData.content.tilesetJsonFile;
this._collisionMaskTag = objectData.content.collisionMaskTag;
this._layerIndex = objectData.content.useAllLayers
? null
: Number.isFinite(objectData.content.layerIndex)
? objectData.content.layerIndex
: 1;
this._debugMode = objectData.content.debugMode;
this._fillColor = gdjs.rgbOrHexStringToNumber(
objectData.content.fillColor
Expand Down Expand Up @@ -108,7 +114,8 @@ namespace gdjs {
);
this._collisionTileMap = new gdjs.TileMap.TransformedCollisionTileMap(
editableTileMap,
this._collisionMaskTag
this._collisionMaskTag,
this._layerIndex
);

this._renderer = new gdjs.TileMap.TileMapCollisionMaskRenderer(
Expand Down Expand Up @@ -178,6 +185,7 @@ namespace gdjs {
if (oldObjectData.outlineSize !== newObjectData.outlineSize) {
this.setOutlineSize(newObjectData.outlineSize);
}
// TODO Handle changes to collisionMaskTag, useAllLayers and layerIndex.
return true;
}

Expand Down Expand Up @@ -254,7 +262,8 @@ namespace gdjs {

this._collisionTileMap = new gdjs.TileMap.TransformedCollisionTileMap(
tileMap,
this._collisionMaskTag
this._collisionMaskTag,
this._layerIndex
);
// The tile map polygons always keep the same references.
// It works because the tilemap is never modified.
Expand Down
Loading

0 comments on commit f6cb203

Please sign in to comment.