Skip to content

Commit 9654a4b

Browse files
committed
Consolidating Layers into View class.
1 parent 19e6091 commit 9654a4b

5 files changed

Lines changed: 280 additions & 25 deletions

File tree

src/Phaser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Phaser = Phaser || {
1515
* @constant
1616
* @type {string}
1717
*/
18-
VERSION: '2.7.0 Beta 3',
18+
VERSION: '2.7.0 Beta 4',
1919

2020
/**
2121
* An array of Phaser game instances.

src/physics/arcade/TilemapCollision.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ Phaser.Physics.Arcade.TilemapCollision.prototype = {
4040
}
4141

4242
var mapData = tilemapLayer.getTiles(
43-
(sprite.body.position.x - sprite.body.tilePadding.x) - tilemapLayer.x,
44-
(sprite.body.position.y - sprite.body.tilePadding.y) - tilemapLayer.y,
43+
(sprite.body.position.x - sprite.body.tilePadding.x) - tilemapLayer.offsetX,
44+
(sprite.body.position.y - sprite.body.tilePadding.y) - tilemapLayer.offsetY,
4545
sprite.body.width + sprite.body.tilePadding.x,
4646
sprite.body.height + sprite.body.tilePadding.y,
4747
false, false);
@@ -130,8 +130,8 @@ Phaser.Physics.Arcade.TilemapCollision.prototype = {
130130
return false;
131131
}
132132

133-
var tilemapLayerOffsetX = tilemapLayer.x;
134-
var tilemapLayerOffsetY = tilemapLayer.y;
133+
var tilemapLayerOffsetX = tilemapLayer.offsetX;
134+
var tilemapLayerOffsetY = tilemapLayer.offsetY;
135135

136136
// We re-check for collision in case body was separated in a previous step
137137
if (!tile.intersects((body.position.x - tilemapLayerOffsetX), (body.position.y - tilemapLayerOffsetY), (body.right - tilemapLayerOffsetX), (body.bottom - tilemapLayerOffsetY)))
@@ -243,7 +243,7 @@ Phaser.Physics.Arcade.TilemapCollision.prototype = {
243243
tileCheckX: function (body, tile, tilemapLayer) {
244244

245245
var ox = 0;
246-
var tilemapLayerOffsetX = tilemapLayer.x;
246+
var tilemapLayerOffsetX = tilemapLayer.offsetX;
247247

248248
if (body.deltaX() < 0 && !body.blocked.left && tile.collideRight && body.checkCollision.left)
249249
{
@@ -301,7 +301,7 @@ Phaser.Physics.Arcade.TilemapCollision.prototype = {
301301
tileCheckY: function (body, tile, tilemapLayer) {
302302

303303
var oy = 0;
304-
var tilemapLayerOffsetY = tilemapLayer.y;
304+
var tilemapLayerOffsetY = tilemapLayer.offsetY;
305305

306306
if (body.deltaY() < 0 && !body.blocked.up && tile.collideDown && body.checkCollision.up)
307307
{

src/tilemap/TilemapLayer.js

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
3535
*/
3636
this.map = tilemap;
3737

38+
/**
39+
* A custom view.
40+
*
41+
* @property {Phaser.Point} view
42+
*/
43+
this.view = game.camera;
44+
45+
this._camDetatched = false;
46+
3847
/**
3948
* The index of this layer within the Tilemap.
4049
* @property {number} index
@@ -232,7 +241,7 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
232241
this.renderSettings.copyCanvas = Phaser.TilemapLayer.ensureSharedCopyCanvas();
233242
}
234243

235-
this.fixedToCamera = true;
244+
// this.fixedToCamera = true;
236245

237246
};
238247

@@ -280,6 +289,20 @@ Phaser.TilemapLayer.prototype.preUpdate = function() {
280289

281290
};
282291

292+
Phaser.TilemapLayer.prototype.addCamera = function () {
293+
294+
this.view = this.game.camera;
295+
this._camDetatched = true;
296+
297+
};
298+
299+
Phaser.TilemapLayer.prototype.removeCamera = function (x, y) {
300+
301+
this.view = new Phaser.Point(x, y);
302+
this._camDetatched = true;
303+
304+
};
305+
283306
/**
284307
* Automatically called by World.postUpdate. Handles cache updates.
285308
*
@@ -290,12 +313,12 @@ Phaser.TilemapLayer.prototype.postUpdate = function () {
290313

291314
if (this.fixedToCamera)
292315
{
293-
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
294-
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
316+
// this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
317+
// this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
295318
}
296319

297-
this._scrollX = this.game.camera.view.x * this.scrollFactorX / this.scale.x;
298-
this._scrollY = this.game.camera.view.y * this.scrollFactorY / this.scale.y;
320+
this.scrollX = this.view.x * this.scrollFactorX / this.scale.x;
321+
this.scrollY = this.view.y * this.scrollFactorY / this.scale.y;
299322

300323
};
301324

@@ -314,8 +337,8 @@ Phaser.TilemapLayer.prototype._renderCanvas = function (renderSession) {
314337
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
315338
}
316339

317-
this._scrollX = this.game.camera.view.x * this.scrollFactorX / this.scale.x;
318-
this._scrollY = this.game.camera.view.y * this.scrollFactorY / this.scale.y;
340+
this.scrollX = this.view.x * this.scrollFactorX / this.scale.x;
341+
this.scrollY = this.view.y * this.scrollFactorY / this.scale.y;
319342

320343
this.render();
321344

@@ -977,7 +1000,7 @@ Phaser.TilemapLayer.prototype._fixX = function (x) {
9771000
x = 0;
9781001
}
9791002

980-
if (this.scrollFactorX === 1)
1003+
if (this.scrollFactorX === 1 && !this._camDetatched)
9811004
{
9821005
return x;
9831006
}
@@ -996,7 +1019,7 @@ Phaser.TilemapLayer.prototype._fixX = function (x) {
9961019
*/
9971020
Phaser.TilemapLayer.prototype._unfixX = function (x) {
9981021

999-
if (this.scrollFactorX === 1)
1022+
if (this.scrollFactorX === 1 && !this._camDetatched)
10001023
{
10011024
return x;
10021025
}
@@ -1020,7 +1043,7 @@ Phaser.TilemapLayer.prototype._fixY = function (y) {
10201043
y = 0;
10211044
}
10221045

1023-
if (this.scrollFactorY === 1)
1046+
if (this.scrollFactorY === 1 && !this._camDetatched)
10241047
{
10251048
return y;
10261049
}
@@ -1039,7 +1062,7 @@ Phaser.TilemapLayer.prototype._fixY = function (y) {
10391062
*/
10401063
Phaser.TilemapLayer.prototype._unfixY = function (y) {
10411064

1042-
if (this.scrollFactorY === 1)
1065+
if (this.scrollFactorY === 1 && !this._camDetatched)
10431066
{
10441067
return y;
10451068
}
@@ -1236,6 +1259,54 @@ Object.defineProperty(Phaser.TilemapLayer.prototype, "y", {
12361259

12371260
});
12381261

1262+
/**
1263+
* The x position of this Tilemap Layer.
1264+
*
1265+
* @property {integer} x
1266+
* @memberof Phaser.TilemapLayer
1267+
* @public
1268+
*/
1269+
Object.defineProperty(Phaser.TilemapLayer.prototype, "offsetX", {
1270+
1271+
get: function () {
1272+
1273+
if (this._camDetatched)
1274+
{
1275+
return this.cameraOffset.x - this.view.x;
1276+
}
1277+
else
1278+
{
1279+
return this.cameraOffset.x;
1280+
}
1281+
1282+
}
1283+
1284+
});
1285+
1286+
/**
1287+
* The y position of this Tilemap Layer.
1288+
*
1289+
* @property {integer} y
1290+
* @memberof Phaser.TilemapLayer
1291+
* @public
1292+
*/
1293+
Object.defineProperty(Phaser.TilemapLayer.prototype, "offsetY", {
1294+
1295+
get: function () {
1296+
1297+
if (this._camDetatched)
1298+
{
1299+
return this.cameraOffset.y - this.view.y;
1300+
}
1301+
else
1302+
{
1303+
return this.cameraOffset.y;
1304+
}
1305+
1306+
}
1307+
1308+
});
1309+
12391310
Object.defineProperty(Phaser.TilemapLayer.prototype, "bottom", {
12401311

12411312
get: function () {
@@ -1291,11 +1362,15 @@ Object.defineProperty(Phaser.TilemapLayer.prototype, "wrap", {
12911362
Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollX", {
12921363

12931364
get: function () {
1365+
12941366
return this._scrollX;
1367+
12951368
},
12961369

12971370
set: function (value) {
1371+
12981372
this._scrollX = value;
1373+
12991374
}
13001375

13011376
});
@@ -1310,11 +1385,15 @@ Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollX", {
13101385
Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollY", {
13111386

13121387
get: function () {
1388+
13131389
return this._scrollY;
1390+
13141391
},
13151392

13161393
set: function (value) {
1394+
13171395
this._scrollY = value;
1396+
13181397
}
13191398

13201399
});
@@ -1329,12 +1408,16 @@ Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollY", {
13291408
Object.defineProperty(Phaser.TilemapLayer.prototype, "collisionWidth", {
13301409

13311410
get: function () {
1411+
13321412
return this._mc.cw;
1413+
13331414
},
13341415

13351416
set: function (value) {
1417+
13361418
this._mc.cw = value | 0;
13371419
this.dirty = true;
1420+
13381421
}
13391422

13401423
});
@@ -1349,12 +1432,16 @@ Object.defineProperty(Phaser.TilemapLayer.prototype, "collisionWidth", {
13491432
Object.defineProperty(Phaser.TilemapLayer.prototype, "collisionHeight", {
13501433

13511434
get: function () {
1435+
13521436
return this._mc.ch;
1437+
13531438
},
13541439

13551440
set: function (value) {
1441+
13561442
this._mc.ch = value | 0;
13571443
this.dirty = true;
1444+
13581445
}
13591446

13601447
});

src/tilemap/TilemapLayerGL.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ Phaser.TilemapLayerGL = function (game, tilemap, index, width, height, tileset)
3232
*/
3333
this.game = game;
3434

35+
/**
36+
* A custom view.
37+
*
38+
* @property {Phaser.Point} view
39+
*/
40+
this.view = null;
41+
3542
/**
3643
* An Array of any linked layers.
3744
*
@@ -272,6 +279,18 @@ Phaser.TilemapLayerGL.prototype.preUpdate = function () {
272279

273280
};
274281

282+
Phaser.TilemapLayerGL.prototype.addCamera = function () {
283+
284+
this.view = null;
285+
286+
};
287+
288+
Phaser.TilemapLayerGL.prototype.removeCamera = function (x, y) {
289+
290+
this.view = new Phaser.Point(x, y);
291+
292+
};
293+
275294
/**
276295
* Automatically called by World.postUpdate. Handles camera scrolling.
277296
*
@@ -282,11 +301,16 @@ Phaser.TilemapLayerGL.prototype.postUpdate = function () {
282301

283302
Phaser.Component.FixedToCamera.postUpdate.call(this);
284303

285-
// Stops you being able to auto-scroll the camera if it's not following a sprite
286-
var camera = this.game.camera;
287-
288-
this.scrollX = camera.x * this.scrollFactorX / this.scale.x;
289-
this.scrollY = camera.y * this.scrollFactorY / this.scale.y;
304+
if (this.view)
305+
{
306+
this.scrollX = this.view.x * this.scrollFactorX / this.scale.x;
307+
this.scrollY = this.view.y * this.scrollFactorY / this.scale.y;
308+
}
309+
else
310+
{
311+
this.scrollX = this.game.camera.x * this.scrollFactorX / this.scale.x;
312+
this.scrollY = this.game.camera.y * this.scrollFactorY / this.scale.y;
313+
}
290314

291315
this.render();
292316

@@ -569,7 +593,7 @@ Phaser.TilemapLayerGL.prototype._fixX = function (x) {
569593
x = 0;
570594
}
571595

572-
if (this.scrollFactorX === 1)
596+
if (this.scrollFactorX === 1 && !this.view)
573597
{
574598
return x;
575599
}
@@ -588,7 +612,7 @@ Phaser.TilemapLayerGL.prototype._fixX = function (x) {
588612
*/
589613
Phaser.TilemapLayerGL.prototype._unfixX = function (x) {
590614

591-
if (this.scrollFactorX === 1)
615+
if (this.scrollFactorX === 1 && !this.view)
592616
{
593617
return x;
594618
}

0 commit comments

Comments
 (0)