Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tileEngine): support flipped tiles in Tiled #401

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/tileEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { on } from './events.js';
import { clamp, getWorldRect } from './helpers.js';
import { removeFromArray } from './utils.js';

// @ifdef TILEENGINE_TILED

// Tiled uses the bits 32 and 31 to denote that a tile is
// flipped horizontally or vertically (respectively)
// @see https://doc.mapeditor.org/en/stable/reference/global-tile-ids/
let FLIPPED_HORIZONTALLY = 0x80000000;
let FLIPPED_VERTICALLY = 0x40000000;
// @endif

/**
* Get the row from the y coordinate.
* @private
Expand Down Expand Up @@ -628,6 +637,17 @@ class TileEngine {
// skip empty tiles (0)
if (!tile) return;

let flipped = 0;

// @ifdef TILEENGINE_TILED
// read flags
let flippedHorizontal = tile & FLIPPED_HORIZONTALLY;
let flippedVertical = tile & FLIPPED_VERTICALLY;
flipped = flippedHorizontal || flippedVertical;

tile &= ~(FLIPPED_HORIZONTALLY | FLIPPED_VERTICALLY);
// @endif

// find the tileset the tile belongs to
// assume tilesets are ordered by firstgid
let tileset;
Expand All @@ -648,17 +668,37 @@ class TileEngine {
let sx = (offset % cols) * (tilewidth + margin);
let sy = ((offset / cols) | 0) * (tileheight + margin);

// @ifdef TILEENGINE_TILED
if (flipped) {
context.save();
context.translate(
x + (flippedHorizontal ? tilewidth : 0),
y + (flippedVertical ? tileheight : 0)
);
context.scale(
flippedHorizontal ? -1 : 1,
flippedVertical ? -1 : 1
);
}
// @endif

context.drawImage(
image,
sx,
sy,
tilewidth,
tileheight,
x,
y,
flipped ? 0 : x,
flipped ? 0 : y,
tilewidth,
tileheight
);

// @ifdef TILEENGINE_TILED
if (flipped) {
context.restore();
}
// @endif
});

context.restore();
Expand Down
200 changes: 193 additions & 7 deletions test/unit/tileEngine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { noop } from '../../src/utils.js';
let testContext = {
TILEENGINE_CAMERA: true,
TILEENGINE_DYNAMIC: true,
TILEENGINE_QUERY: true
TILEENGINE_QUERY: true,
TILEENGINE_TILED: true
};
// test-context:end

Expand Down Expand Up @@ -229,8 +230,7 @@ describe(
expect(tileEngine.sx).to.equal(0);
expect(tileEngine.sy).to.equal(0);
});
}
else {
} else {
it('should not have sx and sy properties', () => {
expect(tileEngine.sx).to.not.exist;
expect(tileEngine.sy).to.not.exist;
Expand Down Expand Up @@ -753,9 +753,6 @@ describe(

tileEngine.renderLayer('test');

const img = new Image();
img.src = tileEngine.layerCanvases.test.toDataURL();

expect(context.drawImage.called).to.be.true;
expect(
context.drawImage.calledWith(
Expand Down Expand Up @@ -878,7 +875,7 @@ describe(
expect(tileEngine._r.called).to.be.true;
});
} else {
it('doe snot call render if the layer is dirty', () => {
it('does not call render if the layer is dirty', () => {
let tileEngine = TileEngine({
tilewidth: 10,
tileheight: 10,
Expand Down Expand Up @@ -939,6 +936,195 @@ describe(

expect(fn).to.not.throw();
});

if (testContext.TILEENGINE_TILED) {
it('rotates a tile horizontally', () => {
let tileEngine = TileEngine({
tilewidth: 10,
tileheight: 10,
width: 1,
height: 1,
tilesets: [
{
firstgid: 1,
image: new Image(),
columns: 10
}
],
layers: [
{
name: 'test',
data: [3 + 0x80000000]
}
]
});

let r = tileEngine._r.bind(tileEngine);
let ctx;
tileEngine._r = function overrideR(layer, context) {
ctx = context;
sinon.stub(context, 'drawImage').callsFake(noop);
sinon.stub(context, 'translate').callsFake(noop);
sinon.stub(context, 'scale').callsFake(noop);
r(layer, context);
};

tileEngine.renderLayer('test');

expect(ctx.translate.calledWith(10, 0)).to.be.true;
expect(ctx.scale.calledWith(-1, 1)).to.be.true;
expect(
ctx.drawImage.calledWith(
tileEngine.tilesets[0].image,
20,
0,
10,
10,
0,
0,
10,
10
)
).to.be.true;
});

it('rotates a tile vertically', () => {
let tileEngine = TileEngine({
tilewidth: 10,
tileheight: 10,
width: 1,
height: 1,
tilesets: [
{
firstgid: 1,
image: new Image(),
columns: 10
}
],
layers: [
{
name: 'test',
data: [3 + 0x40000000]
}
]
});

let r = tileEngine._r.bind(tileEngine);
let ctx;
tileEngine._r = function overrideR(layer, context) {
ctx = context;
sinon.stub(context, 'drawImage').callsFake(noop);
sinon.stub(context, 'translate').callsFake(noop);
sinon.stub(context, 'scale').callsFake(noop);
r(layer, context);
};

tileEngine.renderLayer('test');

expect(ctx.translate.calledWith(0, 10)).to.be.true;
expect(ctx.scale.calledWith(1, -1)).to.be.true;
expect(
ctx.drawImage.calledWith(
tileEngine.tilesets[0].image,
20,
0,
10,
10,
0,
0,
10,
10
)
).to.be.true;
});

it('rotates a tile horizontally and vertically', () => {
let tileEngine = TileEngine({
tilewidth: 10,
tileheight: 10,
width: 1,
height: 1,
tilesets: [
{
firstgid: 1,
image: new Image(),
columns: 10
}
],
layers: [
{
name: 'test',
data: [3 + 0x80000000 + 0x40000000]
}
]
});

let r = tileEngine._r.bind(tileEngine);
let ctx;
tileEngine._r = function overrideR(layer, context) {
ctx = context;
sinon.stub(context, 'drawImage').callsFake(noop);
sinon.stub(context, 'translate').callsFake(noop);
sinon.stub(context, 'scale').callsFake(noop);
r(layer, context);
};

tileEngine.renderLayer('test');

expect(ctx.translate.calledWith(10, 10)).to.be.true;
expect(ctx.scale.calledWith(-1, -1)).to.be.true;
expect(
ctx.drawImage.calledWith(
tileEngine.tilesets[0].image,
20,
0,
10,
10,
0,
0,
10,
10
)
).to.be.true;
});
} else {
it('does not rotate tile', () => {
let tileEngine = TileEngine({
tilewidth: 10,
tileheight: 10,
width: 1,
height: 1,
tilesets: [
{
firstgid: 1,
image: new Image(),
columns: 10
}
],
layers: [
{
name: 'test',
data: [3 + 0x80000000]
}
]
});

let r = tileEngine._r.bind(tileEngine);
let ctx;
tileEngine._r = function overrideR(layer, context) {
ctx = context;
sinon.stub(context, 'drawImage').callsFake(noop);
sinon.stub(context, 'translate').callsFake(noop);
sinon.stub(context, 'scale').callsFake(noop);
r(layer, context);
};

tileEngine.renderLayer('test');

expect(ctx.translate.called).to.be.false;
expect(ctx.scale.called).to.be.false;
});
}
});

// --------------------------------------------------
Expand Down
Loading