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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Arcade Physics Tile map collision methods.
*
* @class Phaser.Physics.Arcade.TilemapCollision
* @constructor
*/
Phaser.Physics.Arcade.TilemapCollision = function () {};
Phaser.Physics.Arcade.TilemapCollision.prototype = {
/**
* @property {number} TILE_BIAS - A value added to the delta values during collision with tiles. Adjust this if you get tunneling.
*/
TILE_BIAS: 16,
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @method Phaser.Physics.Arcade#collideSpriteVsTilemapLayer
* @private
* @param {Phaser.Sprite} sprite - The sprite to check.
* @param {Phaser.TilemapLayer} tilemapLayer - The layer to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext, overlapOnly) {
if (!sprite.body)
{
return;
}
var mapData = tilemapLayer.getTiles(
sprite.body.position.x - sprite.body.tilePadding.x,
sprite.body.position.y - sprite.body.tilePadding.y,
sprite.body.width + sprite.body.tilePadding.x,
sprite.body.height + sprite.body.tilePadding.y,
false, false);
if (mapData.length === 0)
{
return;
}
for (var i = 0; i < mapData.length; i++)
{
if (processCallback)
{
if (processCallback.call(callbackContext, sprite, mapData[i]))
{
if (this.separateTile(i, sprite.body, mapData[i], tilemapLayer, overlapOnly))
{
this._total++;
if (collideCallback)
{
collideCallback.call(callbackContext, sprite, mapData[i]);
}
}
}
}
else
{
if (this.separateTile(i, sprite.body, mapData[i], tilemapLayer, overlapOnly))
{
this._total++;
if (collideCallback)
{
collideCallback.call(callbackContext, sprite, mapData[i]);
}
}
}
}
},
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @private
* @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer
* @param {Phaser.Group} group - The Group to check.
* @param {Phaser.TilemapLayer} tilemapLayer - The layer to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideGroupVsTilemapLayer: function (group, tilemapLayer, collideCallback, processCallback, callbackContext, overlapOnly) {
if (group.length === 0)
{
return;
}
for (var i = 0; i < group.children.length; i++)
{
if (group.children[i].exists)
{
this.collideSpriteVsTilemapLayer(group.children[i], tilemapLayer, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
},
/**
* The core separation function to separate a physics body and a tile.
*
* @private
* @method Phaser.Physics.Arcade#separateTile
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
* @param {Phaser.Tile} tile - The tile to collide against.
* @param {Phaser.TilemapLayer} tilemapLayer - The tilemapLayer to collide against.
* @return {boolean} Returns true if the body was separated, otherwise false.
*/
separateTile: function (i, body, tile, tilemapLayer, overlapOnly) {
if (!body.enable)
{
return false;
}
var tilemapLayerOffsetX = (!tilemapLayer.fixedToCamera) ? tilemapLayer.position.x : 0;
var tilemapLayerOffsetY = (!tilemapLayer.fixedToCamera) ? tilemapLayer.position.y : 0;
// We re-check for collision in case body was separated in a previous step
if (!tile.intersects((body.position.x - tilemapLayerOffsetX), (body.position.y - tilemapLayerOffsetY), (body.right - tilemapLayerOffsetX), (body.bottom - tilemapLayerOffsetY)))
{
// no collision so bail out (separated in a previous step)
return false;
}
else if (overlapOnly)
{
// There is an overlap, and we don't need to separate. Bail.
return true;
}
// They overlap. Any custom callbacks?
// A local callback always takes priority over a layer level callback
if (tile.collisionCallback && !tile.collisionCallback.call(tile.collisionCallbackContext, body.sprite, tile))
{
// If it returns true then we can carry on, otherwise we should abort.
return false;
}
else if (typeof tile.layer.callbacks !== 'undefined' && tile.layer.callbacks[tile.index] && !tile.layer.callbacks[tile.index].callback.call(tile.layer.callbacks[tile.index].callbackContext, body.sprite, tile))
{
// If it returns true then we can carry on, otherwise we should abort.
return false;
}
// We don't need to go any further if this tile doesn't actually separate
if (!tile.faceLeft && !tile.faceRight && !tile.faceTop && !tile.faceBottom)
{
// This could happen if the tile was meant to be collided with re: a callback, but otherwise isn't needed for separation
return false;
}
var ox = 0;
var oy = 0;
var minX = 0;
var minY = 1;
if (body.deltaAbsX() > body.deltaAbsY())
{
// Moving faster horizontally, check X axis first
minX = -1;
}
else if (body.deltaAbsX() < body.deltaAbsY())
{
// Moving faster vertically, check Y axis first
minY = -1;
}
if (body.deltaX() !== 0 && body.deltaY() !== 0 && (tile.faceLeft || tile.faceRight) && (tile.faceTop || tile.faceBottom))
{
// We only need do this if both axis have checking faces AND we're moving in both directions
minX = Math.min(Math.abs((body.position.x - tilemapLayerOffsetX) - tile.right), Math.abs((body.right - tilemapLayerOffsetX) - tile.left));
minY = Math.min(Math.abs((body.position.y - tilemapLayerOffsetY) - tile.bottom), Math.abs((body.bottom - tilemapLayerOffsetY) - tile.top));
}
if (minX < minY)
{
if (tile.faceLeft || tile.faceRight)
{
ox = this.tileCheckX(body, tile, tilemapLayer);
// That's horizontal done, check if we still intersects? If not then we can return now
if (ox !== 0 && !tile.intersects((body.position.x - tilemapLayerOffsetX), (body.position.y - tilemapLayerOffsetY), (body.right - tilemapLayerOffsetX), (body.bottom - tilemapLayerOffsetY)))
{
return true;
}
}
if (tile.faceTop || tile.faceBottom)
{
oy = this.tileCheckY(body, tile, tilemapLayer);
}
}
else
{
if (tile.faceTop || tile.faceBottom)
{
oy = this.tileCheckY(body, tile, tilemapLayer);
// That's vertical done, check if we still intersects? If not then we can return now
if (oy !== 0 && !tile.intersects((body.position.x - tilemapLayerOffsetX), (body.position.y - tilemapLayerOffsetY), (body.right - tilemapLayerOffsetX), (body.bottom - tilemapLayerOffsetY)))
{
return true;
}
}
if (tile.faceLeft || tile.faceRight)
{
ox = this.tileCheckX(body, tile, tilemapLayer);
}
}
return (ox !== 0 || oy !== 0);
},
/**
* Check the body against the given tile on the X axis.
*
* @private
* @method Phaser.Physics.Arcade#tileCheckX
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
* @param {Phaser.Tile} tile - The tile to check.
* @param {Phaser.TilemapLayer} tilemapLayer - The tilemapLayer to collide against.
* @return {number} The amount of separation that occurred.
*/
tileCheckX: function (body, tile, tilemapLayer) {
var ox = 0;
var tilemapLayerOffsetX = (!tilemapLayer.fixedToCamera) ? tilemapLayer.position.x : 0;
if (body.deltaX() < 0 && !body.blocked.left && tile.collideRight && body.checkCollision.left)
{
// Body is moving LEFT
if (tile.faceRight && (body.x - tilemapLayerOffsetX) < tile.right)
{
ox = (body.x - tilemapLayerOffsetX) - tile.right;
if (ox < -this.TILE_BIAS)
{
ox = 0;
}
}
}
else if (body.deltaX() > 0 && !body.blocked.right && tile.collideLeft && body.checkCollision.right)
{
// Body is moving RIGHT
if (tile.faceLeft && (body.right - tilemapLayerOffsetX) > tile.left)
{
ox = (body.right - tilemapLayerOffsetX) - tile.left;
if (ox > this.TILE_BIAS)
{
ox = 0;
}
}
}
if (ox !== 0)
{
if (body.customSeparateX)
{
body.overlapX = ox;
}
else
{
this.processTileSeparationX(body, ox);
}
}
return ox;
},
/**
* Check the body against the given tile on the Y axis.
*
* @private
* @method Phaser.Physics.Arcade#tileCheckY
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
* @param {Phaser.Tile} tile - The tile to check.
* @param {Phaser.TilemapLayer} tilemapLayer - The tilemapLayer to collide against.
* @return {number} The amount of separation that occurred.
*/
tileCheckY: function (body, tile, tilemapLayer) {
var oy = 0;
var tilemapLayerOffsetY = (!tilemapLayer.fixedToCamera) ? tilemapLayer.position.y : 0;
if (body.deltaY() < 0 && !body.blocked.up && tile.collideDown && body.checkCollision.up)
{
// Body is moving UP
if (tile.faceBottom && (body.y - tilemapLayerOffsetY) < tile.bottom)
{
oy = (body.y - tilemapLayerOffsetY) - tile.bottom;
if (oy < -this.TILE_BIAS)
{
oy = 0;
}
}
}
else if (body.deltaY() > 0 && !body.blocked.down && tile.collideUp && body.checkCollision.down)
{
// Body is moving DOWN
if (tile.faceTop && (body.bottom - tilemapLayerOffsetY) > tile.top)
{
oy = (body.bottom - tilemapLayerOffsetY) - tile.top;
if (oy > this.TILE_BIAS)
{
oy = 0;
}
}
}
if (oy !== 0)
{
if (body.customSeparateY)
{
body.overlapY = oy;
}
else
{
this.processTileSeparationY(body, oy);
}
}
return oy;
},
/**
* Internal function to process the separation of a physics body from a tile.
*
* @private
* @method Phaser.Physics.Arcade#processTileSeparationX
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
* @param {number} x - The x separation amount.
*/
processTileSeparationX: function (body, x) {
if (x < 0)
{
body.blocked.left = true;
}
else if (x > 0)
{
body.blocked.right = true;
}
body.position.x -= x;
if (body.bounce.x === 0)
{
body.velocity.x = 0;
}
else
{
body.velocity.x = -body.velocity.x * body.bounce.x;
}
},
/**
* Internal function to process the separation of a physics body from a tile.
*
* @private
* @method Phaser.Physics.Arcade#processTileSeparationY
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
* @param {number} y - The y separation amount.
*/
processTileSeparationY: function (body, y) {
if (y < 0)
{
body.blocked.up = true;
}
else if (y > 0)
{
body.blocked.down = true;
}
body.position.y -= y;
if (body.bounce.y === 0)
{
body.velocity.y = 0;
}
else
{
body.velocity.y = -body.velocity.y * body.bounce.y;
}
}
};
// Merge this with the Arcade Physics prototype
Phaser.Utils.mixinPrototype(Phaser.Physics.Arcade.prototype, Phaser.Physics.Arcade.TilemapCollision.prototype);