Skip to content

Commit d8f5832

Browse files
committed
Completely empty Tilemaps can now be created. This allows for dynamic map generation at runtime.
Loads of updates across most the Tilemap files. Not finished yet, still CSV loading to do and a multi-tileset issue to resolve, but it's a lot more flexible now.
1 parent 6f51304 commit d8f5832

12 files changed

Lines changed: 394 additions & 83 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Updates:
160160
* ScaleManager has 2 new events: ScaleManager.enterFullScreen and ScaleManager.leaveFullScreen, so you can respond to fullscreen changes directly.
161161
* RandomDataGenerator.integerInRange(min, max) now includes both `min` and `max` within its range (#501)
162162
* Tween no longer copies all the object properties into the `_valuesStart` object on creation.
163+
* Completely empty Tilemaps can now be created. This allows for dynamic map generation at runtime.
163164

164165

165166
Bug Fixes:

examples/tilemaps/paint tiles.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var layer;
1313

1414
var marker;
1515
var currentTile;
16+
var cursors;
1617

1718
function create() {
1819

@@ -30,6 +31,8 @@ function create() {
3031
marker.lineStyle(2, 0x000000, 1);
3132
marker.drawRect(0, 0, 32, 32);
3233

34+
cursors = game.input.keyboard.createCursorKeys();
35+
3336
}
3437

3538
function update() {
@@ -52,11 +55,28 @@ function update() {
5255
}
5356
}
5457

58+
if (cursors.left.isDown)
59+
{
60+
game.camera.x -= 4;
61+
}
62+
else if (cursors.right.isDown)
63+
{
64+
game.camera.x += 4;
65+
}
66+
67+
if (cursors.up.isDown)
68+
{
69+
game.camera.y -= 4;
70+
}
71+
else if (cursors.down.isDown)
72+
{
73+
game.camera.y += 4;
74+
}
75+
5576
}
5677

5778
function render() {
5879

59-
game.debug.text('Left-click to paint. Shift + Left-click to select tile.', 32, 32, 'rgb(0,0,0)');
60-
game.debug.text('Tile: ' + map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)), 32, 48, 'rgb(0,0,0)');
80+
game.debug.text('Left-click to paint. Shift + Left-click to select tile. Arrows to scroll.', 32, 32, '#efefef');
6181

6282
}

examples/wip/tilemap blank.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
4+
5+
function preload() {
6+
7+
game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
8+
9+
}
10+
11+
var map;
12+
var tileset;
13+
var layer;
14+
var cursors;
15+
16+
function create() {
17+
18+
game.stage.backgroundColor = '#787878';
19+
20+
// Creates a blank tilemap
21+
map = game.add.tilemap();
22+
23+
// Creates a layer and sets-up the map dimensions.
24+
// In this case the map is 30x30 tiles in size and the tiles are 32x32 pixels in size.
25+
map.create('level1', 30, 30, 32, 32);
26+
27+
// Add a Tileset image to the map
28+
map.addTilesetImage('ground_1x1');
29+
30+
map.putTile(4, 1, 1)
31+
map.putTile(10, 2, 1)
32+
map.putTile(10, 3, 1)
33+
map.putTile(10, 4, 1)
34+
35+
// Create a layer. This is where the map is rendered to.
36+
layer = map.createLayer('level1');
37+
38+
// layer.resizeWorld();
39+
// map.setCollisionBetween(1, 12);
40+
// layer.debug = true;
41+
42+
cursors = game.input.keyboard.createCursorKeys();
43+
44+
}
45+
46+
function update() {
47+
48+
if (cursors.left.isDown)
49+
{
50+
}
51+
else if (cursors.right.isDown)
52+
{
53+
}
54+
55+
if (cursors.up.isDown)
56+
{
57+
}
58+
else if (cursors.down.isDown)
59+
{
60+
}
61+
62+
}
63+
64+
function render() {
65+
66+
game.debug.cameraInfo(game.camera, 32, 32);
67+
68+
}

examples/wip/tween var.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.spritesheet('mummy', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18);
7+
8+
}
9+
10+
var mummy;
11+
12+
function create() {
13+
14+
game.stage.backgroundColor = 0x3d4d3d;
15+
16+
mummy = game.add.sprite(300, 300, 'mummy', 5);
17+
18+
var t = game.add.tween(mummy).to( { "x": 400, "y": 400 }, 5000, Phaser.Easing.Linear.None, true);
19+
20+
// var t = game.add.tween(mummy).to( { "scale.x": 4, "scale.y": 4 }, 5000, Phaser.Easing.Linear.None, true);
21+
22+
t.onComplete.add(tweenOver, this);
23+
24+
}
25+
26+
function tweenOver(a) {
27+
28+
console.log('over');
29+
console.log(a);
30+
31+
}
32+
33+
function update() {
34+
35+
}
36+
37+
function render() {
38+
39+
}

src/gameobjects/GameObjectCreator.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,12 @@ Phaser.GameObjectCreator.prototype = {
273273
* Creates a new Tilemap object.
274274
*
275275
* @method Phaser.GameObjectCreator#tilemap
276-
* @param {string} key - Asset key for the JSON or CSV map data in the cache.
277-
* @param {object|string} tilesets - An object mapping Cache.tileset keys with the tileset names in the JSON file. If a string is provided that will be used.
276+
* @param {string} [key] - The key of the tilemap data as stored in the Cache. If you're creating a blank map don't pass anything for this parameter.
278277
* @return {Phaser.Tilemap} The newly created tilemap object.
279278
*/
280-
tilemap: function (key, tilesets) {
279+
tilemap: function (key) {
281280

282-
return new Phaser.Tilemap(this.game, key, tilesets);
281+
return new Phaser.Tilemap(this.game, key);
283282

284283
},
285284

src/gameobjects/GameObjectFactory.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,12 @@ Phaser.GameObjectFactory.prototype = {
304304
* Creates a new Tilemap object.
305305
*
306306
* @method Phaser.GameObjectFactory#tilemap
307-
* @param {string} key - Asset key for the JSON or CSV map data in the cache.
308-
* @param {object|string} tilesets - An object mapping Cache.tileset keys with the tileset names in the JSON file. If a string is provided that will be used.
307+
* @param {string} [key] - The key of the tilemap data as stored in the Cache. If you're creating a blank map don't pass anything for this parameter.
309308
* @return {Phaser.Tilemap} The newly created tilemap object.
310309
*/
311-
tilemap: function (key, tilesets) {
310+
tilemap: function (key) {
312311

313-
return new Phaser.Tilemap(this.game, key, tilesets);
312+
return new Phaser.Tilemap(this.game, key);
314313

315314
},
316315

src/physics/World.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,10 @@ Phaser.Physics.World.prototype = {
367367

368368
if (this.bounds !== null)
369369
{
370-
this.world.removeBody(this.bounds);
370+
if (this.bounds.world)
371+
{
372+
this.world.removeBody(this.bounds);
373+
}
371374

372375
var i = this.bounds.shapes.length;
373376

src/tilemap/Tilemap.js

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @class Phaser.Tilemap
1212
* @constructor
1313
* @param {Phaser.Game} game - Game reference to the currently running game.
14-
* @param {string} [key] - The key of the tilemap data as stored in the Cache.
14+
* @param {string} [key] - The key of the tilemap data as stored in the Cache. If you're creating a blank map don't pass anything for this parameter.
1515
*/
1616
Phaser.Tilemap = function (game, key) {
1717

@@ -155,37 +155,52 @@ Phaser.Tilemap.prototype = {
155155
* Creates an empty map of the given dimensions.
156156
*
157157
* @method Phaser.Tilemap#create
158-
* @param {string} name - The name of the map (mostly used for debugging)
158+
* @param {string} name - The name of the default layer of the map
159159
* @param {number} width - The width of the map in tiles.
160160
* @param {number} height - The height of the map in tiles.
161+
* @param {number} tileWidth - The width of the tiles the map uses for calculations.
162+
* @param {number} tileHeight - The height of the tiles the map uses for calculations.
161163
*/
162-
create: function (name, width, height) {
164+
create: function (name, width, height, tileWidth, tileHeight) {
163165

164-
var data = [];
166+
this.width = width;
167+
this.height = height;
168+
this.tileWidth = tileWidth;
169+
this.tileHeight = tileHeight;
170+
this.widthInPixels = width * tileWidth;
171+
this.heightInPixels = height * tileHeight;
172+
173+
var row;
174+
var output = [];
165175

166176
for (var y = 0; y < height; y++)
167177
{
168-
data[y] = [];
178+
row = [];
169179

170180
for (var x = 0; x < width; x++)
171181
{
172-
data[y][x] = 0;
182+
row.push(null);
173183
}
184+
185+
output.push(row);
174186
}
175187

176188
this.layers.push({
177189

178190
name: name,
191+
x: 0,
192+
y: 0,
179193
width: width,
180194
height: height,
195+
widthInPixels: this.widthInPixels,
196+
heightInPixels: this.heightInPixels,
181197
alpha: 1,
182198
visible: true,
183-
tileMargin: 0,
184-
tileSpacing: 0,
185-
format: Phaser.Tilemap.CSV,
186-
data: data,
199+
properties: {},
187200
indexes: [],
188-
dirty: true
201+
callbacks: [],
202+
bodies: [],
203+
data: output
189204

190205
});
191206

@@ -200,8 +215,17 @@ Phaser.Tilemap.prototype = {
200215
* @method Phaser.Tilemap#addTilesetImage
201216
* @param {string} tileset - The name of the tileset as specified in the map data.
202217
* @param {string} [key] - The key of the Phaser.Cache image used for this tileset. If not specified it will look for an image with a key matching the tileset parameter.
218+
* @param {number} [tileWidth] - The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value.
219+
* @param {number} [tileHeight] - The height of the tiles in the Tileset Image. If not given it will default to the map.tileHeight value.
220+
* @param {number} [tileMargin=0] - The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value.
221+
* @param {number} [tileSpacing=0] - The height of the tiles in the Tileset Image. If not given it will default to the map.tileHeight value.
203222
*/
204-
addTilesetImage: function (tileset, key) {
223+
addTilesetImage: function (tileset, key, tileWidth, tileHeight, tileMargin, tileSpacing) {
224+
225+
if (typeof tileWidth === 'undefined') { tileWidth = this.tileWidth; }
226+
if (typeof tileHeight === 'undefined') { tileHeight = this.tileHeight; }
227+
if (typeof tileMargin === 'undefined') { tileMargin = 0; }
228+
if (typeof tileSpacing === 'undefined') { tileSpacing = 0; }
205229

206230
if (typeof key === 'undefined')
207231
{
@@ -222,10 +246,17 @@ Phaser.Tilemap.prototype = {
222246

223247
if (this.tilesets[tileset])
224248
{
225-
this.tilesets[tileset].image = this.game.cache.getImage(key);
226-
249+
this.tilesets[tileset].setImage(this.game.cache.getImage(key));
227250
return true;
228251
}
252+
else
253+
{
254+
var newSet = new Phaser.Tileset(key, 0, tileWidth, tileHeight, tileMargin, tileSpacing, {});
255+
256+
newSet.setImage(this.game.cache.getImage(key));
257+
258+
this.tilesets.push(newSet);
259+
}
229260

230261
return false;
231262

@@ -920,6 +951,12 @@ Phaser.Tilemap.prototype = {
920951

921952
},
922953

954+
hasTile: function (x, y, layer) {
955+
956+
return (this.layers[layer].data[y] !== null && this.layers[layer].data[y][x] !== null);
957+
958+
},
959+
923960
/**
924961
* Puts a tile of the given index value at the coordinate specified.
925962
*
@@ -937,14 +974,30 @@ Phaser.Tilemap.prototype = {
937974
{
938975
if (tile instanceof Phaser.Tile)
939976
{
940-
this.layers[layer].data[y][x].copy(tile);
977+
if (this.hasTile(x, y, layer))
978+
{
979+
this.layers[layer].data[y][x].copy(tile);
980+
}
981+
else
982+
{
983+
//Phaser.Tile = function (layer, index, x, y, width, height) {
984+
this.layers[layer].data[y][x] = new Phaser.Tile(layer, tile.index, x, y, tile.width, tile.height);
985+
}
941986
}
942987
else
943988
{
944-
this.layers[layer].data[y][x].index = tile;
989+
if (this.hasTile(x, y, layer))
990+
{
991+
this.layers[layer].data[y][x].index = tile;
992+
}
993+
else
994+
{
995+
this.layers[layer].data[y][x] = new Phaser.Tile(layer, tile, x, y, this.tileWidth, this.tileHeight);
996+
}
945997
}
946998

947999
this.layers[layer].dirty = true;
1000+
9481001
this.calculateFaces(layer);
9491002
}
9501003

0 commit comments

Comments
 (0)