Skip to content

Commit cec71a8

Browse files
committed
added hexagonal support
1 parent 406e6eb commit cec71a8

19 files changed

Lines changed: 382 additions & 55 deletions

dist/phaser-arcade-physics.js

Lines changed: 127 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11919,7 +11919,7 @@ var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
1191911919
point.x = WorldToTileX(worldX, snapToFloor, camera, layer, orientation);
1192011920
point.y = WorldToTileY(worldY, snapToFloor, camera, layer, orientation);
1192111921
}
11922-
else if (orientation === 'isometric' || orientation === 'staggered')
11922+
else if (orientation === 'isometric' || orientation === 'staggered' || orientation === 'hexagonal')
1192311923
{
1192411924

1192511925
var tileWidth = layer.baseTileWidth;
@@ -11966,15 +11966,27 @@ var WorldToTileXY = function (worldX, worldY, snapToFloor, point, camera, layer)
1196611966
}
1196711967
else if (orientation === 'staggered')
1196811968
{
11969-
// implement world to tile staggered
1197011969
point.y = snapToFloor
1197111970
? Math.floor((worldY / (tileHeight / 2)))
1197211971
: (worldY / (tileHeight / 2));
1197311972
point.x = snapToFloor
11974-
? Math.floor((worldX - (point.y % 2) * 0.5 * tileWidth) / tileWidth)
11975-
: (worldX - (worldY % 2) * 0.5 * tileWidth) / tileWidth;
11973+
? Math.floor((worldX + (point.y % 2) * 0.5 * tileWidth) / tileWidth)
11974+
: (worldX + (point.y % 2) * 0.5 * tileWidth) / tileWidth;
1197611975

1197711976
}
11977+
else if (orientation === 'hexagonal')
11978+
{
11979+
var sidel = layer.hexSideLength;
11980+
var rowHeight = ((tileHeight - sidel) / 2 + sidel);
11981+
11982+
// similar to staggered, because Tiled uses the oddr representation.
11983+
point.y = snapToFloor
11984+
? Math.floor((worldY / rowHeight))
11985+
: (worldY / rowHeight);
11986+
point.x = snapToFloor
11987+
? Math.floor((worldX - (point.y % 2) * 0.5 * tileWidth) / tileWidth)
11988+
: (worldX - (point.y % 2) * 0.5 * tileWidth) / tileWidth;
11989+
}
1197811990
}
1197911991

1198011992
return point;
@@ -12714,13 +12726,27 @@ var Tile = new Class({
1271412726
}
1271512727
else if (this.layer.orientation === 'staggered')
1271612728
{
12729+
var tmap = this.layer.tilemapLayer.tilemap;
1271712730
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
1271812731
this.pixelY = this.y * (this.baseHeight / 2);
1271912732

1272012733
}
12734+
else if (this.layer.orientation === 'hexagonal')
12735+
{
1272112736

12722-
// this.pixelY = this.y * this.baseHeight - (this.height - this.baseHeight);
12737+
// var tmap = this.layer.tilemapLayer.tilemap;
12738+
console.log(this.layer.hexSideLength);
12739+
var sidel = this.layer.hexSideLength;
12740+
12741+
var rowHeight = ((this.baseHeight - sidel) / 2 + sidel);
12742+
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
12743+
this.pixelY = this.y * rowHeight;
12744+
12745+
console.log('hexapix', this.pixelX, this.pixelY);
12746+
}
1272312747

12748+
// this.pixelY = this.y * this.baseHeight - (this.height - this.baseHeight);
12749+
console.log(this.layer);
1272412750
return this;
1272512751
},
1272612752

@@ -19466,6 +19492,16 @@ var LayerData = new Class({
1946619492
* @since 3.0.0
1946719493
*/
1946819494
this.tilemapLayer = GetFastValue(config, 'tilemapLayer', null);
19495+
19496+
/**
19497+
* Optional : Only for hexagonal tilemaps.
19498+
* The length of the horizontal sides of the hexagon.
19499+
* @name Phaser.Tilemaps.MapData#tiles
19500+
* @type {integer}
19501+
* @since 3.0.0
19502+
*/
19503+
this.hexSideLength = GetFastValue(config, 'hexSideLength', 0);
19504+
1946919505
}
1947019506

1947119507
});
@@ -19691,6 +19727,15 @@ var MapData = new Class({
1969119727
* @since 3.0.0
1969219728
*/
1969319729
this.tiles = GetFastValue(config, 'tiles', []);
19730+
19731+
/**
19732+
* Optional : Only for hexagonal tilemaps.
19733+
* The length of the horizontal sides of the hexagon.
19734+
* @name Phaser.Tilemaps.MapData#tiles
19735+
* @type {integer}
19736+
* @since 3.0.0
19737+
*/
19738+
this.hexSideLength = GetFastValue(config, 'hexSideLength', 0);
1969419739
}
1969519740

1969619741
});
@@ -47297,7 +47342,7 @@ var TileToWorldXY = function (tileX, tileY, point, camera, layer)
4729747342
point.x = TileToWorldX(tileX, camera, layer, orientation);
4729847343
point.y = TileToWorldY(tileY, camera, layer, orientation);
4729947344
}
47300-
else if (orientation === 'isometric' || orientation === 'staggered')
47345+
else if (orientation === 'isometric' || orientation === 'staggered' || orientation === 'hexagonal')
4730147346
{
4730247347

4730347348
var layerWorldX = 0;
@@ -47320,10 +47365,18 @@ var TileToWorldXY = function (tileX, tileY, point, camera, layer)
4732047365
}
4732147366
else if (orientation === 'staggered')
4732247367
{
47323-
// todo
4732447368
point.x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2);
4732547369
point.y = layerWorldY + tileY * (tileHeight / 2);
4732647370
}
47371+
else if (orientation === 'hexagonal')
47372+
{
47373+
var sidel = layer.hexSideLength;
47374+
var rowHeight = ((tileHeight - sidel) / 2 + sidel);
47375+
47376+
// similar to staggered, because Tiled uses the oddr representation.
47377+
point.x = layerWorldX + tileX * tileWidth + tileY % 2 * (tileWidth / 2);
47378+
point.y = layerWorldY + tileY * rowHeight;
47379+
}
4732747380

4732847381

4732947382
}
@@ -102659,13 +102712,14 @@ var TileToWorldX = function (tileX, camera, layer)
102659102712
{
102660102713
return layerWorldX + tileX * tileWidth;
102661102714
}
102662-
else if (orientation === 'isometric')
102715+
else if (orientation === 'isometric' || orientation === 'staggered' || orientation === 'hexagonal' )
102663102716
{
102664102717
// Not Best Solution ?
102665-
console.warn('With isometric map types you have to use the TileToWorldXY function.');
102718+
console.warn('With the current map type you have to use the TileToWorldXY function.');
102666102719
return null;
102667102720
}
102668102721

102722+
102669102723

102670102724
};
102671102725

@@ -102720,6 +102774,18 @@ var TileToWorldY = function (tileY, camera, layer)
102720102774
console.warn('With isometric map types you have to use the TileToWorldXY function.');
102721102775
return null;
102722102776
}
102777+
else if (orientation === 'staggered')
102778+
{
102779+
return layerWorldY + tileY * (tileHeight / 2);
102780+
}
102781+
else if (orientation === 'hexagonal')
102782+
{
102783+
var sidel = layer.tilemapLayer.tilemap.hexSideLength;
102784+
var rowHeight = ((tileHeight - sidel) / 2 + sidel);
102785+
102786+
// same as staggered
102787+
return layerWorldY + tileY * rowHeight;
102788+
}
102723102789
};
102724102790

102725102791
module.exports = TileToWorldY;
@@ -102884,10 +102950,25 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer)
102884102950
}
102885102951
else if (orientation === 'isometric')
102886102952
{
102887-
console.warn('With isometric map types you have to use the WorldToTileXY function.');
102953+
console.warn('With standard isometric map types you have to use the WorldToTileXY function.');
102888102954
return null;
102889102955

102890102956
}
102957+
else if (orientation === 'staggered')
102958+
{
102959+
return snapToFloor
102960+
? Math.floor(worldY / (tileHeight / 2))
102961+
: worldY / (tileHeight / 2);
102962+
}
102963+
else if (orientation === 'hexagonal')
102964+
{
102965+
102966+
var sidel = layer.hexSideLength;
102967+
var rowHeight = ((tileHeight - sidel) / 2 + sidel);
102968+
return snapToFloor
102969+
? Math.floor(worldY / rowHeight)
102970+
: worldY / rowHeight;
102971+
}
102891102972
};
102892102973

102893102974
module.exports = WorldToTileY;
@@ -103157,11 +103238,15 @@ var ParseJSONTiled = function (name, json, insertNull)
103157103238
{
103158103239
if (json.orientation === 'isometric' || json.orientation === 'staggered')
103159103240
{
103160-
console.warn('isometric map types are WIP in this version of Phaser');
103241+
console.warn('Isometric map types are WIP in this version of Phaser');
103242+
}
103243+
else if (json.orientation === 'hexagonal')
103244+
{
103245+
console.warn('Hexagonal map types are WIP in this version of Phaser');
103161103246
}
103162103247
else if (json.orientation !== 'orthogonal')
103163103248
{
103164-
console.warn('Only orthogonal and standard isometric map types are supported in this version of Phaser');
103249+
console.warn('Only orthogonal, hexagonal and isometric map types are supported in this version of Phaser');
103165103250
return null;
103166103251
}
103167103252

@@ -103180,6 +103265,11 @@ var ParseJSONTiled = function (name, json, insertNull)
103180103265
infinite: json.infinite
103181103266
});
103182103267

103268+
if (mapData.orientation === 'hexagonal')
103269+
{
103270+
mapData.hexSideLength = json.hexsidelength;
103271+
}
103272+
103183103273
mapData.layers = ParseTileLayers(json, insertNull);
103184103274
mapData.images = ParseImageLayers(json);
103185103275

@@ -103336,6 +103426,10 @@ var ParseTileLayers = function (json, insertNull)
103336103426
orientation: json.orientation
103337103427
});
103338103428

103429+
if (layerData.orientation === 'hexagonal')
103430+
{
103431+
layerData.hexSideLength = json.hexsidelength;
103432+
}
103339103433

103340103434
for (var c = 0; c < curl.height; c++)
103341103435
{
@@ -103410,6 +103504,11 @@ var ParseTileLayers = function (json, insertNull)
103410103504
properties: GetFastValue(curl, 'properties', {}),
103411103505
orientation: json.orientation
103412103506
});
103507+
103508+
if (layerData.orientation === 'hexagonal')
103509+
{
103510+
layerData.hexSideLength = json.hexsidelength;
103511+
}
103413103512
var row = [];
103414103513

103415103514
// Loop through the data field in the JSON.
@@ -104801,6 +104900,15 @@ var Tilemap = new Class({
104801104900
* @since 3.0.0
104802104901
*/
104803104902
this.currentLayerIndex = 0;
104903+
104904+
/**
104905+
* Optional : Only for hexagonal tilemaps.
104906+
* The length of the horizontal sides of the hexagon.
104907+
* @name Phaser.Tilemaps.MapData#tiles
104908+
* @type {integer}
104909+
* @since 3.0.0
104910+
*/
104911+
this.hexSideLength = mapData.hexSideLength;
104804104912
},
104805104913

104806104914
/**
@@ -107220,7 +107328,8 @@ var DynamicTilemapLayer = new Class({
107220107328

107221107329
// Link the LayerData with this static tilemap layer
107222107330
this.layer.tilemapLayer = this;
107223-
107331+
console.log('fug',this.layer);
107332+
107224107333
/**
107225107334
* The Tileset/s associated with this layer.
107226107335
*
@@ -175599,7 +175708,7 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
175599175708
var inIsoBounds = function () { return true; };
175600175709
if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1)
175601175710
{
175602-
if (layer.orientation === 'orthogonal' || layer.orientation === 'staggered')
175711+
if (layer.orientation === 'orthogonal' || layer.orientation === 'staggered' || layer.orientation === 'hexagonal')
175603175712
{
175604175713
// Camera world view bounds, snapped for scaled tile size
175605175714
// Cull Padding values are given in tiles, not pixels
@@ -175635,7 +175744,7 @@ var CullTiles = function (layer, camera, outputArray, renderOrder)
175635175744
var tile;
175636175745

175637175746

175638-
if (layer.orientation === 'orthogonal' || layer.orientation === 'staggered')
175747+
if (layer.orientation === 'orthogonal' || layer.orientation === 'staggered' || layer.orientation === 'hexagonal')
175639175748
{
175640175749

175641175750
if (renderOrder === 0)
@@ -177434,7 +177543,7 @@ var DynamicTilemapLayerWebGLRenderer = function (renderer, src, interpolationPer
177434177543
var frameWidth = 0;
177435177544
var frameHeight = 0;
177436177545

177437-
if (src.layer.orientation === 'isometric' || src.layer.orientation === 'staggered')
177546+
if (src.layer.orientation === 'isometric' || src.layer.orientation === 'staggered' || src.layer.orientation === 'hexagonal')
177438177547
{
177439177548
// we use the tileset width and height because in isometric maps the tileset's height is often different from the tilemap's.
177440177549
frameWidth = tileset.tileWidth;
@@ -177577,9 +177686,9 @@ var DynamicTilemapLayerCanvasRenderer = function (renderer, src, interpolationPe
177577177686
var width = tile.width;
177578177687
var height = tile.width;
177579177688

177580-
if (src.layer.orientation === 'isometric' || src.layer.orientation === 'staggered')
177689+
if (src.layer.orientation === 'isometric' || src.layer.orientation === 'staggered' || src.layer.orientation === 'hexagonal')
177581177690
{
177582-
// we use the tileset width and height because in isometric maps the tileset's height is often different from the tilemap's.
177691+
// we use the tileset width and height because in isometric and hexagonal maps the tileset's height is often different from the tilemap's.
177583177692
width = tileset.tileWidth;
177584177693
width = tileset.tileHeight;
177585177694
}

dist/phaser-arcade-physics.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)