2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-06-22 01:40:10 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var DefaultDefs = require('./DefaultDefs');
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class CollisionMap
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Physics.Impact
|
2018-02-09 16:04:43 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {integer} [tilesize=32] - [description]
|
2018-03-28 13:11:46 +00:00
|
|
|
* @param {array} [data] - [description]
|
2018-02-09 16:04:43 +00:00
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
var CollisionMap = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function CollisionMap (tilesize, data)
|
|
|
|
{
|
2017-09-13 12:15:47 +00:00
|
|
|
if (tilesize === undefined) { tilesize = 32; }
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Impact.CollisionMap#tilesize
|
|
|
|
* @type {integer}
|
|
|
|
* @default 32
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
this.tilesize = tilesize;
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Impact.CollisionMap#data
|
|
|
|
* @type {array}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-13 12:15:47 +00:00
|
|
|
this.data = (Array.isArray(data)) ? data : [];
|
2017-06-22 01:40:10 +00:00
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Impact.CollisionMap#width
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
this.width = (Array.isArray(data)) ? data[0].length : 0;
|
2018-02-09 16:04:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Impact.CollisionMap#height
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
this.height = (Array.isArray(data)) ? data.length : 0;
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Impact.CollisionMap#lastSlope
|
|
|
|
* @type {integer}
|
|
|
|
* @default 55
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-22 15:22:21 +00:00
|
|
|
this.lastSlope = 55;
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Impact.CollisionMap#tiledef
|
|
|
|
* @type {object}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
this.tiledef = DefaultDefs;
|
|
|
|
},
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Impact.CollisionMap#trace
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} x - [description]
|
|
|
|
* @param {number} y - [description]
|
|
|
|
* @param {number} vx - [description]
|
|
|
|
* @param {number} vy - [description]
|
|
|
|
* @param {number} objectWidth - [description]
|
|
|
|
* @param {number} objectHeight - [description]
|
|
|
|
*
|
|
|
|
* @return {boolean} [description]
|
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
trace: function (x, y, vx, vy, objectWidth, objectHeight)
|
|
|
|
{
|
|
|
|
// Set up the trace-result
|
|
|
|
var res = {
|
|
|
|
collision: { x: false, y: false, slope: false },
|
2017-08-16 13:02:10 +00:00
|
|
|
pos: { x: x + vx, y: y + vy },
|
2017-06-22 01:40:10 +00:00
|
|
|
tile: { x: 0, y: 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!this.data)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
var steps = Math.ceil(Math.max(Math.abs(vx), Math.abs(vy)) / this.tilesize);
|
|
|
|
|
|
|
|
if (steps > 1)
|
|
|
|
{
|
|
|
|
var sx = vx / steps;
|
|
|
|
var sy = vy / steps;
|
|
|
|
|
|
|
|
for (var i = 0; i < steps && (sx || sy); i++)
|
|
|
|
{
|
|
|
|
this.step(res, x, y, sx, sy, objectWidth, objectHeight, vx, vy, i);
|
|
|
|
|
|
|
|
x = res.pos.x;
|
|
|
|
y = res.pos.y;
|
|
|
|
|
|
|
|
if (res.collision.x)
|
|
|
|
{
|
|
|
|
sx = 0;
|
|
|
|
vx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.collision.y)
|
|
|
|
{
|
|
|
|
sy = 0;
|
|
|
|
vy = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.collision.slope)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.step(res, x, y, vx, vy, objectWidth, objectHeight, vx, vy, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Impact.CollisionMap#step
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {object} res - [description]
|
|
|
|
* @param {number} x - [description]
|
|
|
|
* @param {number} y - [description]
|
|
|
|
* @param {number} vx - [description]
|
|
|
|
* @param {number} vy - [description]
|
|
|
|
* @param {number} width - [description]
|
|
|
|
* @param {number} height - [description]
|
|
|
|
* @param {number} rvx - [description]
|
|
|
|
* @param {number} rvy - [description]
|
|
|
|
* @param {number} step - [description]
|
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
step: function (res, x, y, vx, vy, width, height, rvx, rvy, step)
|
|
|
|
{
|
|
|
|
var t = 0;
|
2017-08-18 16:01:51 +00:00
|
|
|
var tileX;
|
|
|
|
var tileY;
|
2017-06-22 01:40:10 +00:00
|
|
|
var tilesize = this.tilesize;
|
2017-06-22 03:56:50 +00:00
|
|
|
var mapWidth = this.width;
|
|
|
|
var mapHeight = this.height;
|
2017-06-22 01:40:10 +00:00
|
|
|
|
|
|
|
// Horizontal
|
|
|
|
if (vx)
|
|
|
|
{
|
2017-06-22 15:22:21 +00:00
|
|
|
var pxOffsetX = (vx > 0 ? width : 0);
|
|
|
|
var tileOffsetX = (vx < 0 ? tilesize : 0);
|
2017-06-22 01:40:10 +00:00
|
|
|
|
|
|
|
var firstTileY = Math.max(Math.floor(y / tilesize), 0);
|
2017-06-22 03:56:50 +00:00
|
|
|
var lastTileY = Math.min(Math.ceil((y + height) / tilesize), mapHeight);
|
2017-08-18 16:01:51 +00:00
|
|
|
|
|
|
|
tileX = Math.floor((res.pos.x + pxOffsetX) / tilesize);
|
2017-06-22 01:40:10 +00:00
|
|
|
|
|
|
|
var prevTileX = Math.floor((x + pxOffsetX) / tilesize);
|
|
|
|
|
2017-06-22 03:56:50 +00:00
|
|
|
if (step > 0 || tileX === prevTileX || prevTileX < 0 || prevTileX >= mapWidth)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
prevTileX = -1;
|
|
|
|
}
|
2017-08-18 16:01:51 +00:00
|
|
|
|
2017-06-22 03:56:50 +00:00
|
|
|
if (tileX >= 0 && tileX < mapWidth)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
2017-08-18 16:01:51 +00:00
|
|
|
for (tileY = firstTileY; tileY < lastTileY; tileY++)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
2017-06-22 15:22:21 +00:00
|
|
|
if (prevTileX !== -1)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
t = this.data[tileY][prevTileX];
|
|
|
|
|
2017-06-22 15:22:21 +00:00
|
|
|
if (t > 1 && t <= this.lastSlope && this.checkDef(res, t, x, y, rvx, rvy, width, height, prevTileX, tileY))
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t = this.data[tileY][tileX];
|
|
|
|
|
2017-06-22 15:22:21 +00:00
|
|
|
if (t === 1 || t > this.lastSlope || (t > 1 && this.checkDef(res, t, x, y, rvx, rvy, width, height, tileX, tileY)))
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
2017-06-22 15:22:21 +00:00
|
|
|
if (t > 1 && t <= this.lastSlope && res.collision.slope)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.collision.x = true;
|
|
|
|
res.tile.x = t;
|
2017-06-23 17:13:38 +00:00
|
|
|
res.pos.x = (tileX * tilesize) - pxOffsetX + tileOffsetX;
|
2017-06-22 15:22:21 +00:00
|
|
|
x = res.pos.x;
|
|
|
|
rvx = 0;
|
2017-06-22 01:40:10 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertical
|
|
|
|
if (vy)
|
|
|
|
{
|
2017-06-22 15:22:21 +00:00
|
|
|
var pxOffsetY = (vy > 0 ? height : 0);
|
|
|
|
var tileOffsetY = (vy < 0 ? tilesize : 0);
|
2017-06-22 01:40:10 +00:00
|
|
|
|
|
|
|
var firstTileX = Math.max(Math.floor(res.pos.x / tilesize), 0);
|
2017-06-22 03:56:50 +00:00
|
|
|
var lastTileX = Math.min(Math.ceil((res.pos.x + width) / tilesize), mapWidth);
|
2017-08-18 16:01:51 +00:00
|
|
|
|
|
|
|
tileY = Math.floor((res.pos.y + pxOffsetY) / tilesize);
|
2017-06-22 01:40:10 +00:00
|
|
|
|
|
|
|
var prevTileY = Math.floor((y + pxOffsetY) / tilesize);
|
|
|
|
|
2017-06-22 03:56:50 +00:00
|
|
|
if (step > 0 || tileY === prevTileY || prevTileY < 0 || prevTileY >= mapHeight)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
prevTileY = -1;
|
|
|
|
}
|
|
|
|
|
2017-06-22 03:56:50 +00:00
|
|
|
if (tileY >= 0 && tileY < mapHeight)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
2017-08-18 16:01:51 +00:00
|
|
|
for (tileX = firstTileX; tileX < lastTileX; tileX++)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
2017-06-22 15:22:21 +00:00
|
|
|
if (prevTileY !== -1)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
t = this.data[prevTileY][tileX];
|
|
|
|
|
2017-06-22 15:22:21 +00:00
|
|
|
if (t > 1 && t <= this.lastSlope && this.checkDef(res, t, x, y, rvx, rvy, width, height, tileX, prevTileY))
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t = this.data[tileY][tileX];
|
|
|
|
|
2017-06-22 15:22:21 +00:00
|
|
|
if (t === 1 || t > this.lastSlope || (t > 1 && this.checkDef(res, t, x, y, rvx, rvy, width, height, tileX, tileY)))
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
2017-06-22 15:22:21 +00:00
|
|
|
if (t > 1 && t <= this.lastSlope && res.collision.slope)
|
2017-06-22 01:40:10 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.collision.y = true;
|
|
|
|
res.tile.y = t;
|
|
|
|
res.pos.y = tileY * tilesize - pxOffsetY + tileOffsetY;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-09 16:04:43 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Impact.CollisionMap#checkDef
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {object} res - [description]
|
|
|
|
* @param {number} t - [description]
|
|
|
|
* @param {number} x - [description]
|
|
|
|
* @param {number} y - [description]
|
|
|
|
* @param {number} vx - [description]
|
|
|
|
* @param {number} vy - [description]
|
|
|
|
* @param {number} width - [description]
|
|
|
|
* @param {number} height - [description]
|
|
|
|
* @param {number} tileX - [description]
|
|
|
|
* @param {number} tileY - [description]
|
|
|
|
*
|
|
|
|
* @return {boolean} [description]
|
|
|
|
*/
|
2017-06-22 01:40:10 +00:00
|
|
|
checkDef: function (res, t, x, y, vx, vy, width, height, tileX, tileY)
|
|
|
|
{
|
|
|
|
var def = this.tiledef[t];
|
|
|
|
|
|
|
|
if (!def)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tilesize = this.tilesize;
|
|
|
|
|
|
|
|
var lx = (tileX + def[0]) * tilesize;
|
|
|
|
var ly = (tileY + def[1]) * tilesize;
|
|
|
|
var lvx = (def[2] - def[0]) * tilesize;
|
|
|
|
var lvy = (def[3] - def[1]) * tilesize;
|
|
|
|
var solid = def[4];
|
|
|
|
|
|
|
|
var tx = x + vx + (lvy < 0 ? width : 0) - lx;
|
|
|
|
var ty = y + vy + (lvx > 0 ? height : 0) - ly;
|
|
|
|
|
|
|
|
if (lvx * ty - lvy * tx > 0)
|
|
|
|
{
|
|
|
|
if (vx * -lvy + vy * lvx < 0)
|
|
|
|
{
|
|
|
|
return solid;
|
|
|
|
}
|
|
|
|
|
|
|
|
var length = Math.sqrt(lvx * lvx + lvy * lvy);
|
|
|
|
var nx = lvy / length;
|
|
|
|
var ny = -lvx / length;
|
|
|
|
|
|
|
|
var proj = tx * nx + ty * ny;
|
|
|
|
var px = nx * proj;
|
|
|
|
var py = ny * proj;
|
|
|
|
|
|
|
|
if (px * px + py * py >= vx * vx + vy * vy)
|
|
|
|
{
|
2017-06-22 15:22:21 +00:00
|
|
|
return solid || (lvx * (ty - vy) - lvy * (tx - vx) < 0.5);
|
2017-06-22 01:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.pos.x = x + vx - px;
|
|
|
|
res.pos.y = y + vy - py;
|
|
|
|
res.collision.slope = { x: lvx, y: lvy, nx: nx, ny: ny };
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = CollisionMap;
|