- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
import Matrix2d from "./../../math/matrix2.js";
import Sprite from "./../../renderable/sprite.js";
import Bounds from "./../../physics/bounds.js";
import { TMX_FLIP_V, TMX_FLIP_H, TMX_FLIP_AD, TMX_CLEAR_BIT_MASK } from "./constants.js";
/**
* @classdesc
* a basic tile object
* @augments Bounds
*/
export default class Tile extends Bounds {
/**
* @param {number} x - x index of the Tile in the map
* @param {number} y - y index of the Tile in the map
* @param {number} gid - tile gid
* @param {TMXTileset} tileset - the corresponding tileset object
*/
constructor(x, y, gid, tileset) {
let width, height;
// call the parent constructor
super();
// determine the tile size
if (tileset.isCollection) {
let image = tileset.getTileImage(gid & TMX_CLEAR_BIT_MASK);
width = image.width;
height = image.height;
} else {
width = tileset.tilewidth;
height = tileset.tileheight;
}
this.setMinMax(0, 0, width, height);
/**
* tileset
* @type {TMXTileset}
*/
this.tileset = tileset;
/**
* the tile transformation matrix (if defined)
* @ignore
*/
this.currentTransform = null;
// Tile col / row pos
this.col = x;
this.row = y;
/**
* tileId
* @type {number}
*/
this.tileId = gid;
/**
* True if the tile is flipped horizontally
* @type {boolean}
*/
this.flippedX = (this.tileId & TMX_FLIP_H) !== 0;
/**
* True if the tile is flipped vertically
* @type {boolean}
*/
this.flippedY = (this.tileId & TMX_FLIP_V) !== 0;
/**
* True if the tile is flipped anti-diagonally
* @type {boolean}
*/
this.flippedAD = (this.tileId & TMX_FLIP_AD) !== 0;
/**
* Global flag that indicates if the tile is flipped
* @type {boolean}
*/
this.flipped = this.flippedX || this.flippedY || this.flippedAD;
// create and apply transformation matrix if required
if (this.flipped === true) {
if (this.currentTransform === null) {
this.currentTransform = new Matrix2d();
}
this.setTileTransform(this.currentTransform.identity());
}
// clear out the flags and set the tileId
this.tileId &= TMX_CLEAR_BIT_MASK;
}
/**
* set the transformation matrix for this tile
* @ignore
*/
setTileTransform(transform) {
transform.translate(this.width / 2, this.height / 2);
if (this.flippedAD) {
transform.rotate(-90 * Math.PI / 180);
transform.scale(-1, 1);
}
if (this.flippedX) {
transform.scale(
this.flippedAD ? 1 : -1, this.flippedAD ? -1 : 1
);
}
if (this.flippedY) {
transform.scale(
this.flippedAD ? -1 : 1, this.flippedAD ? 1 : -1
);
}
transform.translate(-this.width / 2, -this.height / 2);
}
/**
* return a renderable object for this Tile object
* @param {object} [settings] - see {@link Sprite}
* @returns {Renderable} a me.Sprite object
*/
getRenderable(settings) {
let renderable;
let tileset = this.tileset;
if (tileset.animations.has(this.tileId)) {
let frames = [];
let frameId = [];
(tileset.animations.get(this.tileId).frames).forEach((frame) => {
frameId.push(frame.tileid);
frames.push({
name : "" + frame.tileid,
delay : frame.duration
});
});
renderable = tileset.texture.createAnimationFromName(frameId, settings);
renderable.addAnimation(this.tileId - tileset.firstgid, frames);
renderable.setCurrentAnimation(this.tileId - tileset.firstgid);
} else {
if (tileset.isCollection === true) {
let image = tileset.getTileImage(this.tileId);
renderable = new Sprite(0, 0,
Object.assign({
image: image
})//, settings)
);
renderable.anchorPoint.set(0, 0);
renderable.scale((settings.width / this.width), (settings.height / this.height));
if (typeof settings.rotation !== "undefined") {
renderable.anchorPoint.set(0.5, 0.5);
renderable.currentTransform.rotate(settings.rotation);
renderable.currentTransform.translate(settings.width / 2, settings.height / 2);
// TODO : move the rotation related code from TMXTiledMap to here (under)
settings.rotation = undefined;
}
} else {
renderable = tileset.texture.createSpriteFromName(this.tileId - tileset.firstgid, settings);
renderable.anchorPoint.set(0, 0);
}
}
// any H/V flipping to apply?
this.setTileTransform(renderable.currentTransform);
return renderable;
}
}