mirror of
https://github.com/wassname/phaser.git
synced 2026-07-10 00:30:50 +08:00
More TypeScript updates.
This commit is contained in:
+23
-17
@@ -21,28 +21,38 @@ Phaser.Tilemap = function (game, key, tilesets) {
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {string} key - The key of this map data in the Phaser.Cache.
|
||||
*/
|
||||
this.key = key;
|
||||
|
||||
var data = Phaser.TilemapParser.parse(this.game, key);
|
||||
|
||||
/**
|
||||
* @property {array} layers - An array of Tilemap layer data.
|
||||
*/
|
||||
this.layers = [];
|
||||
this.layers = data.layers;
|
||||
|
||||
if (typeof key === 'string')
|
||||
{
|
||||
this.key = key;
|
||||
/**
|
||||
* @property {array} tilesets - An array of Tilesets.
|
||||
*/
|
||||
this.tilesets = data.tilesets;
|
||||
|
||||
this.layers = game.cache.getTilemapData(key).layers;
|
||||
}
|
||||
/**
|
||||
* @property {array} objects - An array of Tiled Object Layers.
|
||||
*/
|
||||
this.objects = data.objects;
|
||||
|
||||
/**
|
||||
* @property {array} images - An array of Tiled Image Layers.
|
||||
*/
|
||||
this.images = data.images;
|
||||
|
||||
/**
|
||||
* @property {number} currentLayer - The current layer.
|
||||
*/
|
||||
this.currentLayer = 0;
|
||||
|
||||
/**
|
||||
* @property {array} tilesets - An array of Tilesets.
|
||||
*/
|
||||
this.tilesets = [];
|
||||
|
||||
/**
|
||||
* @property {array} debugMap - Map data used for debug values only.
|
||||
*/
|
||||
@@ -66,11 +76,6 @@ Phaser.Tilemap = function (game, key, tilesets) {
|
||||
*/
|
||||
this._tempB = 0;
|
||||
|
||||
if (this.layers.length > 0)
|
||||
{
|
||||
this.addTilesets(tilesets);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -87,6 +92,8 @@ Phaser.Tilemap.TILED_JSON = 1;
|
||||
|
||||
Phaser.Tilemap.prototype = {
|
||||
|
||||
|
||||
|
||||
addTilesets: function (tilesets) {
|
||||
|
||||
// { "TiledKey": "TilesetKey" }
|
||||
@@ -128,7 +135,6 @@ Phaser.Tilemap.prototype = {
|
||||
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Creates an empty map of the given dimensions.
|
||||
*
|
||||
|
||||
@@ -69,19 +69,27 @@ Phaser.TilemapParser = {
|
||||
* Parse tileset data from the cache and creates a Tileset object.
|
||||
* @method Phaser.TilemapParser.parse
|
||||
* @param {Phaser.Game} game - Game reference to the currently running game.
|
||||
* @param {object} data
|
||||
* @param {string} format
|
||||
* @return {Phaser.Tileset} Generated Tileset object.
|
||||
* @param {string} key - The key of the tilemap in the Cache.
|
||||
* @return {object} The parsed map object.
|
||||
*/
|
||||
parse: function (game, data, format) {
|
||||
parse: function (game, key) {
|
||||
|
||||
if (format === Phaser.Tilemap.CSV)
|
||||
var map = game.cache.getTilemapData(key);
|
||||
|
||||
if (map)
|
||||
{
|
||||
return this.parseCSV(data);
|
||||
if (map.format === Phaser.Tilemap.CSV)
|
||||
{
|
||||
return this.parseCSV(map.data);
|
||||
}
|
||||
else if (map.format === Phaser.Tilemap.TILED_JSON)
|
||||
{
|
||||
return this.parseTiledJSON(map.data);
|
||||
}
|
||||
}
|
||||
else if (format === Phaser.Tilemap.TILED_JSON)
|
||||
else
|
||||
{
|
||||
return this.parseTiledJSON(data);
|
||||
return { layers: [], objects: [], images: [], tilesets: [] };
|
||||
}
|
||||
|
||||
},
|
||||
@@ -128,8 +136,8 @@ Phaser.TilemapParser = {
|
||||
/**
|
||||
* Parses a Tiled JSON file into valid map data.
|
||||
* @method Phaser.TilemapParser.parseJSON
|
||||
* @param {object} json - The Tiled JSON data.
|
||||
* @return {object} Generated map data.
|
||||
* @param {object} json - The JSON map data.
|
||||
* @return {object} Generated and parsed map data.
|
||||
*/
|
||||
parseTiledJSON: function (json) {
|
||||
|
||||
@@ -278,6 +286,41 @@ Phaser.TilemapParser = {
|
||||
// Tilesets
|
||||
var tilesets = [];
|
||||
|
||||
|
||||
/*
|
||||
// for (var i = this.firstgid; i < this.firstgid + this.total; i++)
|
||||
for (var i = 0; i < this.total; i++)
|
||||
{
|
||||
// Can add extra properties here as needed
|
||||
this.tiles[i] = [x, y];
|
||||
|
||||
x += this.tileWidth + this.tileSpacing;
|
||||
|
||||
count++;
|
||||
|
||||
if (count === this.total)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
countX++;
|
||||
|
||||
if (countX === this.rows)
|
||||
{
|
||||
x = this.tileMargin;
|
||||
y += this.tileHeight + this.tileSpacing;
|
||||
|
||||
countX = 0;
|
||||
countY++;
|
||||
|
||||
if (countY === this.columns)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
/*
|
||||
{
|
||||
"firstgid":1,
|
||||
@@ -302,10 +345,34 @@ Phaser.TilemapParser = {
|
||||
"tilewidth":32
|
||||
},
|
||||
|
||||
|
||||
Create 1 Tileset object that contains the above, but NOT the actual tile indexes
|
||||
Put the tile indexes into a global array (tiles?) - just need x,y + img - a drawImage look-up table
|
||||
|
||||
*/
|
||||
|
||||
for (var i = 0; i < json.tilesets.length; i++)
|
||||
{
|
||||
// name, firstgid, width, height, margin, spacing, properties
|
||||
var set = json.tilesets[i];
|
||||
var newSet = new Phaser.Tileset(set.name, set.firstgid, set.tilewidth, set.tileheight, set.margin, set.spacing, set.properties);
|
||||
|
||||
if (set.tileproperties)
|
||||
{
|
||||
newSet.tileProperties = set.tileproperties;
|
||||
}
|
||||
|
||||
// rows, columns, total
|
||||
var rows = set.imageheight / set.tileheight;
|
||||
var columns = set.imagewidth / set.tilewidth;
|
||||
var total = rows * columns;
|
||||
|
||||
newSet.rows = rows;
|
||||
newSet.columns = columns;
|
||||
newSet.total = total;
|
||||
|
||||
tilesets.push(newSet);
|
||||
|
||||
}
|
||||
|
||||
// Lets build our super tileset index
|
||||
|
||||
+48
-32
@@ -10,8 +10,7 @@
|
||||
*
|
||||
* @class Phaser.Tileset
|
||||
* @constructor
|
||||
* @param {Image} image - The Image object from the Cache.
|
||||
* @param {string} key - The key of the tileset in the cache.
|
||||
* @param {string} name - The name of the tileset in the map data.
|
||||
* @param {number} tileWidth - Width of each tile in pixels.
|
||||
* @param {number} tileHeight - Height of each tile in pixels.
|
||||
* @param {number} tileMargin - The amount of margin around the tilesheet.
|
||||
@@ -20,65 +19,82 @@
|
||||
* @param {number} columns - How many tiles are placed vertically in each column.
|
||||
* @param {number} total - The maximum number of tiles to extract from the image.
|
||||
*/
|
||||
Phaser.Tileset = function (image, key, tileWidth, tileHeight, tileMargin, tileSpacing, rows, columns, total) {
|
||||
Phaser.Tileset = function (name, firstgid, width, height, margin, spacing, properties) {
|
||||
|
||||
/**
|
||||
* @property {string} key - The cache ID.
|
||||
* @property {string} name - The name of the Tileset.
|
||||
*/
|
||||
this.key = key;
|
||||
|
||||
/**
|
||||
* @property {object} image - The image used for rendering.
|
||||
*/
|
||||
this.image = image;
|
||||
|
||||
/**
|
||||
* @property {number} rows - The number of rows in the tile sheet.
|
||||
*/
|
||||
this.rows = rows;
|
||||
|
||||
/**
|
||||
* @property {number} columns - The number of columns in the tile sheet.
|
||||
*/
|
||||
this.columns = columns;
|
||||
|
||||
/**
|
||||
* @property {number} total - The total number of tiles in the tilesheet.
|
||||
*/
|
||||
this.total = total;
|
||||
this.name = name;
|
||||
|
||||
/**
|
||||
* @property {number} firstgid - The Tiled firstgid value.
|
||||
* @default
|
||||
*/
|
||||
this.firstgid = 1;
|
||||
this.firstgid = firstgid;
|
||||
|
||||
/**
|
||||
* @property {number} tileWidth - The width of a tile in pixels.
|
||||
*/
|
||||
this.tileWidth = tileWidth;
|
||||
this.tileWidth = width;
|
||||
|
||||
/**
|
||||
* @property {number} tileHeight - The height of a tile in pixels.
|
||||
*/
|
||||
this.tileHeight = tileHeight;
|
||||
this.tileHeight = height;
|
||||
|
||||
/**
|
||||
* @property {number} tileMargin - The margin around the tiles in the sheet.
|
||||
*/
|
||||
this.tileMargin = tileMargin;
|
||||
this.tileMargin = margin;
|
||||
|
||||
/**
|
||||
* @property {number} tileSpacing - The margin around the tiles in the sheet.
|
||||
*/
|
||||
this.tileSpacing = tileSpacing;
|
||||
this.tileSpacing = spacing;
|
||||
|
||||
/**
|
||||
* @property {object} properties - Tileset specific properties (typically defined in the Tiled editor).
|
||||
*/
|
||||
this.properties = properties;
|
||||
|
||||
/**
|
||||
* @property {object} tilePproperties - Tile specific properties (typically defined in the Tiled editor).
|
||||
*/
|
||||
this.tileProperties = {};
|
||||
|
||||
/**
|
||||
* @property {string} key - The cache ID.
|
||||
*/
|
||||
// this.key = key;
|
||||
|
||||
/**
|
||||
* @property {object} image - The image used for rendering.
|
||||
*/
|
||||
this.image = null;
|
||||
|
||||
/**
|
||||
* @property {number} rows - The number of rows in the tile sheet.
|
||||
*/
|
||||
this.rows = 0;
|
||||
|
||||
/**
|
||||
* @property {number} columns - The number of columns in the tile sheet.
|
||||
*/
|
||||
this.columns = 0;
|
||||
|
||||
/**
|
||||
* @property {number} total - The total number of tiles in the tilesheet.
|
||||
*/
|
||||
this.total = 0;
|
||||
|
||||
/**
|
||||
* @property {array} tiles - An array of the tile data.
|
||||
*/
|
||||
this.tiles = [];
|
||||
// this.tiles = [];
|
||||
|
||||
// this.build();
|
||||
|
||||
|
||||
this.build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user