New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

phaser-tiled-hull

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

phaser-tiled-hull - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

896

dist/phaser-tiled-hull.js

@@ -1,2 +0,896 @@

!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.phaserTiledHull=n():t.phaserTiledHull=n()}(this,function(){return function(t){function n(e){if(r[e])return r[e].exports;var o=r[e]={i:e,l:!1,exports:{}};return t[e].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r={};return n.m=t,n.c=r,n.i=function(t){return t},n.d=function(t,r,e){n.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:e})},n.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(r,"a",r),r},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=1)}([function(t,n,r){"use strict";function e(t){if(Array.isArray(t)){for(var n=0,r=Array(t.length);n<t.length;n++)r[n]=t[n];return r}return Array.from(t)}function o(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.tileIndices,e=void 0===r?null:r,o=n.tileProperty,a=void 0===o?null:o,f=n.checkCollide,c=void 0!==f&&f,p=i(t,e,a,c),v=u(p),y=l(v),d=[],x=!0,g=!1,m=void 0;try{for(var b,w=y.entries()[Symbol.iterator]();!(x=(b=w.next()).done);x=!0){var _=b.value,P=h(_,2),S=P[0],M=P[1],j=[],O=!0,X=!1,z=void 0;try{for(var C,A=M[Symbol.iterator]();!(O=(C=A.next()).done);O=!0){var I=C.value;j.push(new s.a(I,S))}}catch(t){X=!0,z=t}finally{try{!O&&A.return&&A.return()}finally{if(X)throw z}}d.push(j)}}catch(t){g=!0,m=t}finally{try{!x&&w.return&&w.return()}finally{if(g)throw m}}return d}function i(t,n,r,e){function o(t){return!!t&&(!(!n||!n.includes(t.index))||(!(!r||!t.properties[r])||!(!e||!t.collides)))}function i(t,n,r){var e=a(t,n);o(e)&&-1===r.indexOf(e)&&(r.push(e),i(t,n-1,r),i(t,n+1,r),i(t+1,n,r),i(t-1,n,r))}for(var u=t.map,l=[],a=function(n,r){return u.getTile(n,r,t.index)},f=0;f<u.width;f++)for(var c=0;c<u.height;c++){var s=a(f,c);if(o(s)&&!function(t){var n=!0,r=!1,e=void 0;try{for(var o,i=l[Symbol.iterator]();!(n=(o=i.next()).done);n=!0){var u=o.value,a=!0,f=!1,c=void 0;try{for(var s,h=u[Symbol.iterator]();!(a=(s=h.next()).done);a=!0){if(t===s.value)return u}}catch(t){f=!0,c=t}finally{try{!a&&h.return&&h.return()}finally{if(f)throw c}}}}catch(t){r=!0,e=t}finally{try{!n&&i.return&&i.return()}finally{if(r)throw e}}return null}(s)){var h=[];i(f,c,h),l.push(h)}}return l}function u(t){var n=[],r=!0,e=!1,o=void 0;try{for(var i,u=t[Symbol.iterator]();!(r=(i=u.next()).done);r=!0){var l=i.value,a=[],f=!0,s=!1,h=void 0;try{for(var p,v=l[Symbol.iterator]();!(f=(p=v.next()).done);f=!0){var y=p.value;a.push([y.left,y.top],[y.right,y.top],[y.left,y.bottom],[y.right,y.bottom])}}catch(t){s=!0,h=t}finally{try{!f&&v.return&&v.return()}finally{if(s)throw h}}var d=c()(a,1);n.push(d)}}catch(t){e=!0,o=t}finally{try{!r&&u.return&&u.return()}finally{if(e)throw o}}return n}function l(t){var n=[],r=!0,o=!1,i=void 0;try{for(var u,l=t[Symbol.iterator]();!(r=(u=l.next()).done);r=!0){for(var f=u.value,c=[],s=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[0]),e(f[1])))),h=void 0,p=1;p<f.length;p++)h=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[p-1]),e(f[p])))),a(s,h)?s=new Phaser.Line(s.start.x,s.start.y,h.end.x,h.end.y):(c.push(s),s=h.clone());if(h=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[f.length-1]),e(f[0])))),a(s,h)?(s=new Phaser.Line(s.start.x,s.start.y,h.end.x,h.end.y),c.push(s)):(c.push(s),c.push(h)),a(c[0],c[c.length-1])){var v=c.shift(),y=c.pop(),d=new Phaser.Line(v.start.x,v.start.y,y.end.x,y.end.y);c.push(d)}n.push(c)}}catch(t){o=!0,i=t}finally{try{!r&&l.return&&l.return()}finally{if(o)throw i}}return n}function a(t,n){var r=t.end.x-t.start.x,e=t.end.y-t.start.y,o=n.end.x-n.start.x;return r*(n.end.y-n.start.y)-e*o==0}Object.defineProperty(n,"__esModule",{value:!0});var f=r(6),c=r.n(f),s=r(2),h=function(){function t(t,n){var r=[],e=!0,o=!1,i=void 0;try{for(var u,l=t[Symbol.iterator]();!(e=(u=l.next()).done)&&(r.push(u.value),!n||r.length!==n);e=!0);}catch(t){o=!0,i=t}finally{try{!e&&l.return&&l.return()}finally{if(o)throw i}}return r}return function(n,r){if(Array.isArray(n))return n;if(Symbol.iterator in Object(n))return t(n,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();n.default=o},function(t,n,r){t.exports=r(0).default},function(t,n,r){"use strict";function e(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}var o=function(){function t(t,n){for(var r=0;r<n.length;r++){var e=n[r];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}return function(n,r,e){return r&&t(n.prototype,r),e&&t(n,e),n}}(),i=function(){function t(n,r){e(this,t),this.line=n,this.length=n.length,this.midpoint=n.midPoint(),this.normal=this._getOutwardNormal(n),this.shapeId=r}return o(t,[{key:"_getOutwardNormal",value:function(t){return new Phaser.Point(t.end.y-t.start.y,-(t.end.x-t.start.x)).setMagnitude(1)}}]),t}();n.a=i},function(t,n){function r(t,n,r){return(n[0]-t[0])*(r[1]-t[1])-(n[1]-t[1])*(r[0]-t[0])}function e(t){for(var n=[],e=0;e<t.length;e++){for(;n.length>=2&&r(n[n.length-2],n[n.length-1],t[e])<=0;)n.pop();n.push(t[e])}return n.pop(),n}function o(t){for(var n=t.reverse(),e=[],o=0;o<n.length;o++){for(;e.length>=2&&r(e[e.length-2],e[e.length-1],n[o])<=0;)e.pop();e.push(n[o])}return e.pop(),e}function i(t){var n,r=e(t);return n=o(t).concat(r),n.push(t[0]),n}t.exports=i},function(t,n){t.exports={toXy:function(t,n){return void 0===n?t.slice():t.map(function(t){return new Function("pt","return [pt"+n[0]+",pt"+n[1]+"];")(t)})},fromXy:function(t,n){return void 0===n?t.slice():t.map(function(t){return new Function("pt","var o = {}; o"+n[0]+"= pt[0]; o"+n[1]+"= pt[1]; return o;")(t)})}}},function(t,n){function r(t,n){this._cells=[],this._cellSize=n,t.forEach(function(t){var n=this.point2CellXY(t),r=n[0],e=n[1];void 0===this._cells[r]&&(this._cells[r]=[]),void 0===this._cells[r][e]&&(this._cells[r][e]=[]),this._cells[r][e].push(t)},this)}function e(t,n){return new r(t,n)}r.prototype={cellPoints:function(t,n){return void 0!==this._cells[t]&&void 0!==this._cells[t][n]?this._cells[t][n]:[]},rangePoints:function(t){for(var n=this.point2CellXY([t[0],t[1]]),r=this.point2CellXY([t[2],t[3]]),e=[],o=n[0];o<=r[0];o++)for(var i=n[1];i<=r[1];i++)e=e.concat(this.cellPoints(o,i));return e},removePoint:function(t){for(var n,r=this.point2CellXY(t),e=this._cells[r[0]][r[1]],o=0;o<e.length;o++)if(e[o][0]===t[0]&&e[o][1]===t[1]){n=o;break}return e.splice(n,1),e},point2CellXY:function(t){return[parseInt(t[0]/this._cellSize),parseInt(t[1]/this._cellSize)]},extendBbox:function(t,n){return[t[0]-n*this._cellSize,t[1]-n*this._cellSize,t[2]+n*this._cellSize,t[3]+n*this._cellSize]}},t.exports=e},function(t,n,r){"use strict";function e(t){return t.filter(function(t,n,r){var e=r[n-1];return 0===n||!(e[0]===t[0]&&e[1]===t[1])})}function o(t){return t.sort(function(t,n){return t[0]==n[0]?t[1]-n[1]:t[0]-n[0]})}function i(t,n){return Math.pow(n[0]-t[0],2)+Math.pow(n[1]-t[1],2)}function u(t,n,r){var e=[n[0]-t[0],n[1]-t[1]],o=[r[0]-t[0],r[1]-t[1]],u=i(t,n),l=i(t,r);return(e[0]*o[0]+e[1]*o[1])/Math.sqrt(u*l)}function l(t,n){for(var r=0;r<n.length-1;r++){var e=[n[r],n[r+1]];if(!(t[0][0]===e[0][0]&&t[0][1]===e[0][1]||t[0][0]===e[1][0]&&t[0][1]===e[1][1])&&p(t,e))return!0}return!1}function a(t){for(var n=1/0,r=1/0,e=-1/0,o=-1/0,i=t.length-1;i>=0;i--)t[i][0]<n&&(n=t[i][0]),t[i][1]<r&&(r=t[i][1]),t[i][0]>e&&(e=t[i][0]),t[i][1]>o&&(o=t[i][1]);return[e-n,o-r]}function f(t){return[Math.min(t[0][0],t[1][0]),Math.min(t[0][1],t[1][1]),Math.max(t[0][0],t[1][0]),Math.max(t[0][1],t[1][1])]}function c(t,n,r){for(var e,o,i=null,a=x,f=x,c=0;c<n.length;c++)e=u(t[0],t[1],n[c]),o=u(t[1],t[0],n[c]),e>a&&o>f&&!l([t[0],n[c]],r)&&!l([t[1],n[c]],r)&&(a=e,f=o,i=n[c]);return i}function s(t,n,r,e,o){for(var u,l,a,h,p,v,y,d=!1,x=0;x<t.length-1;x++)if(u=[t[x],t[x+1]],l=u[0].join()+","+u[1].join(),!(i(u[0],u[1])<n||!0===o[l])){a=0,p=f(u);do{p=e.extendBbox(p,a),v=p[2]-p[0],y=p[3]-p[1],h=c(u,e.rangePoints(p),t),a++}while(null===h&&(r[0]>v||r[1]>y));v>=r[0]&&y>=r[1]&&(o[l]=!0),null!==h&&(t.splice(x+1,0,h),e.removePoint(h),d=!0)}return d?s(t,n,r,e,o):t}function h(t,n,r){var i,u,l,f,c,h,p,x=n||20;return t.length<4?t.slice():(p=e(o(y.toXy(t,r))),f=a(p),c=[f[0]*g,f[1]*g],i=d(p),l=p.filter(function(t){return i.indexOf(t)<0}),h=Math.ceil(1/(p.length/(f[0]*f[1]))),u=s(i,Math.pow(x,2),c,v(l,h),{}),y.fromXy(u,r))}var p=r(7),v=r(5),y=r(4),d=r(3),x=Math.cos(90/(180/Math.PI)),g=.6;t.exports=h},function(t,n){function r(t,n,r,e,o,i){var u=(i-n)*(r-t)-(e-n)*(o-t);return u>0||!(u<0)}function e(t,n){var e=t[0][0],o=t[0][1],i=t[1][0],u=t[1][1],l=n[0][0],a=n[0][1],f=n[1][0],c=n[1][1];return r(e,o,l,a,f,c)!==r(i,u,l,a,f,c)&&r(e,o,i,u,l,a)!==r(e,o,i,u,f,c)}t.exports=e}])});
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["phaserTiledHull"] = factory();
else
root["phaserTiledHull"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony export (immutable) */ __webpack_exports__["default"] = phaserTiledHull;
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_hull_js__ = __webpack_require__(6);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_hull_js___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_hull_js__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__polygon_edge__ = __webpack_require__(2);
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
// JS extension required
/**
* A function to take a tilemap layer and process its tiles into clusters. This returns an array of
* polygons - each polygon encloses a separate cluster of tiles in the tilemap layer.
*
* @param {Phaser.TilemapLayer} tilemapLayer The tilemap layer to use for hull calculation.
* @param {object} [options = {}] Options for filtering the tiles and only allowing certain tiles to
* be added to the final hulls. If no options specified, then all tiles in the layer will be
* processed and added to a hull. If multiple options are specified, then a tile only has to match
* ONE of the options to be added to a hull.
* @param {number[]} [options.tileIndices = null] An array of tile indices to use for determining
* which tiles should be clustered together. If a tile's index matches an index in the array, it
* will be added to a hull.
* @param {string} [options.tileProperty = null] The name of a property on tiles (set in Tiled) to
* use for determining which tiles should be clustered together. If the property is true (or truthy)
* on a tile, it will be added to a hull.
* @param {boolean} [options.checkCollide = false] Whether or not a tile's collide property should
* be used for determining which tiles should be clustered together. If true, then colliding tiles
* will be added to a hull.
* @returns {Array.<PolygonEdge[]>} An array where each element represents a polygon. The polygons
* are stored as an array of PolygonEdge instances.
*/
function phaserTiledHull(tilemapLayer) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$tileIndices = _ref.tileIndices,
tileIndices = _ref$tileIndices === undefined ? null : _ref$tileIndices,
_ref$tileProperty = _ref.tileProperty,
tileProperty = _ref$tileProperty === undefined ? null : _ref$tileProperty,
_ref$checkCollide = _ref.checkCollide,
checkCollide = _ref$checkCollide === undefined ? false : _ref$checkCollide;
// Separate the tilemap layer into an array of clustered tiles
var clusters = calculateClusters(tilemapLayer, tileIndices, tileProperty, checkCollide);
// Take the clustered tiles and calculate a hull for each cluster
var pointHulls = calculateHullPoints(clusters);
// Take the point hulls and turn them into polygon representations (i.e. connect the dots)
var polyHulls = buildPolygons(pointHulls);
// Turn the lines in polyHulls into PolygonEdge instances, pre-caching some helpful info like
// the edge normals
var hulls = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = polyHulls.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _ref2 = _step.value;
var _ref3 = _slicedToArray(_ref2, 2);
var i = _ref3[0];
var polyHull = _ref3[1];
var hull = [];
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = polyHull[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var edge = _step2.value;
hull.push(new __WEBPACK_IMPORTED_MODULE_1__polygon_edge__["a" /* default */](edge, i));
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
hulls.push(hull);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return hulls;
}
function calculateClusters(tilemapLayer, tileIndices, tileProperty, checkCollide) {
var tilemap = tilemapLayer.map;
var clusters = [];
var getTile = function getTile(tx, ty) {
return tilemap.getTile(tx, ty, tilemapLayer.index);
};
// Loop over all tiles in the map and kick off recursive cluster building
for (var x = 0; x < tilemap.width; x++) {
for (var y = 0; y < tilemap.height; y++) {
var tile = getTile(x, y);
if (checkTile(tile) && !findTileInClusters(tile)) {
var cluster = [];
recursivelySearchNeighbors(x, y, cluster);
clusters.push(cluster);
}
}
}
// Check to make sure the tile passes the checks, i.e. it is allowed to be in a cluster
function checkTile(tile) {
// No tile, ignore
if (!tile) return false;
// If an array of indices was provided, tile's index must be in that array
if (tileIndices && tileIndices.includes(tile.index)) return true;
// If a tile property was provided, the tile must have a truthy value for that property
if (tileProperty && tile.properties[tileProperty]) return true;
// If we only care about colliding tiles, make sure the tile collides
if (checkCollide && tile.collides) return true;
// Tile didn't pass any checks, ignore
return false;
}
function recursivelySearchNeighbors(x, y, cluster) {
// If tile passes the checks and is not already in the cluster, add it and recursively check
// the neighbors. Note: There's no chance of a tile being a member of two separate clusters.
var tile = getTile(x, y);
if (checkTile(tile) && cluster.indexOf(tile) === -1) {
cluster.push(tile); // Add the current tile
// Search the neighbors
recursivelySearchNeighbors(x, y - 1, cluster);
recursivelySearchNeighbors(x, y + 1, cluster);
recursivelySearchNeighbors(x + 1, y, cluster);
recursivelySearchNeighbors(x - 1, y, cluster);
}
}
function findTileInClusters(searchTile) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = clusters[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var _cluster = _step3.value;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = _cluster[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var _tile = _step4.value;
if (searchTile === _tile) return _cluster;
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return null;
}
return clusters;
}
function calculateHullPoints(clusters) {
var hulls = [];
// Loop over each cluster of tiles in clusters and calculate a polygon hull
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = clusters[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var cluster = _step5.value;
// Find all the points - i.e. the corners of each tile in the cluster
var points = [];
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = cluster[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var tile = _step6.value;
points.push([tile.left, tile.top], [tile.right, tile.top], [tile.left, tile.bottom], [tile.right, tile.bottom]);
}
// Use hull.js to find a hull (e.g. points in clockwise order). The second parameter is the
// concavity of the hull, with 1 being maximally concave.
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
}
var hull = __WEBPACK_IMPORTED_MODULE_0_hull_js___default()(points, 1);
hulls.push(hull);
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
return hulls;
}
function buildPolygons(hulls) {
var polygons = [];
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator7 = hulls[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var hullPoints = _step7.value;
var edges = [];
// Walk along the line segments of the hull, collapsing collinear lines into a single edge
var currentEdge = new (Function.prototype.bind.apply(Phaser.Line, [null].concat(_toConsumableArray(hullPoints[0]), _toConsumableArray(hullPoints[1]))))();
var segment = void 0;
for (var i = 1; i < hullPoints.length; i++) {
// Get the next line segment - starts from the endpoint of the last segment
segment = new (Function.prototype.bind.apply(Phaser.Line, [null].concat(_toConsumableArray(hullPoints[i - 1]), _toConsumableArray(hullPoints[i]))))();
if (checkIfCollinear(currentEdge, segment)) {
// If the current edge and line segment are collinear, then we haven't reached the
// end of the edge yet. Extend the edge to contain the segment.
currentEdge = new Phaser.Line(currentEdge.start.x, currentEdge.start.y, segment.end.x, segment.end.y);
} else {
// We've reached a corner, so the edge is done. Save it and start a new one.
edges.push(currentEdge);
currentEdge = segment.clone();
}
}
// Process the last line segment - connecting the last point back around to the first point
segment = new (Function.prototype.bind.apply(Phaser.Line, [null].concat(_toConsumableArray(hullPoints[hullPoints.length - 1]), _toConsumableArray(hullPoints[0]))))();
if (checkIfCollinear(currentEdge, segment)) {
// Extend the edge and add it (since it wasn't added by the loop above)
currentEdge = new Phaser.Line(currentEdge.start.x, currentEdge.start.y, segment.end.x, segment.end.y);
edges.push(currentEdge);
} else {
// Corner - add the edge and the next segment
edges.push(currentEdge);
edges.push(segment);
}
// Determine whether the last edge and the first edge need to be merged (if the points in
// the hull started midway through an edge)
if (checkIfCollinear(edges[0], edges[edges.length - 1])) {
var firstLine = edges.shift();
var lastLine = edges.pop();
var combinedLine = new Phaser.Line(firstLine.start.x, firstLine.start.y, lastLine.end.x, lastLine.end.y);
edges.push(combinedLine);
}
// Add the final lines to the polygon
polygons.push(edges);
}
} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError7) {
throw _iteratorError7;
}
}
}
return polygons;
}
function checkIfCollinear(line1, line2) {
// To check if two slopes are equal:
// lineDeltaY / lineDeltaX = segmentDeltaY / segmentDeltaX
// But to avoid dividing by zero:
// (lineDeltaX * segmentDeltaY) - (lineDeltaY * segmentDeltaX) = 0
var dx1 = line1.end.x - line1.start.x;
var dy1 = line1.end.y - line1.start.y;
var dx2 = line2.end.x - line2.start.x;
var dy2 = line2.end.y - line2.start.y;
return dx1 * dy2 - dy1 * dx2 === 0;
}
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
// Fix for webpack not exporting ES6 module default properly when using global mode of UMD. It ends
// up with something having to do "phaserTiledHull.default(...)" instead of "phaserTiledHull(...)"
module.exports = __webpack_require__(0).default;
/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Small class for precomuting some useful information about an edge of a polygon - the line,
* length, midpoint, normal. This class isn't exposed directly by the module, but is used in the
* return type of {@link phaserTiledHull}.
*/
var PolygonEdge = function () {
function PolygonEdge(line, hullIndex) {
_classCallCheck(this, PolygonEdge);
/**
* The line that makes up the edge
* @type {Phaser.Line}
*/
this.line = line;
/** @type {number} */
this.length = line.length;
/** @type {Phaser.Point} */
this.midpoint = line.midPoint();
/**
* Outward-facing normal vector as a point, normalized to unit length
* @type {Phaser.Point}
* */
this.normal = this._getOutwardNormal(line);
/**
* Identifies which hull index this edge is a part of
* @type {number}
* */
this.shapeId = hullIndex;
}
_createClass(PolygonEdge, [{
key: "_getOutwardNormal",
value: function _getOutwardNormal(line) {
// The wall lines are returned from hull.js in clockwise order, so the outward facing normal
// is the following
return new Phaser.Point(line.end.y - line.start.y, -(line.end.x - line.start.x)).setMagnitude(1);
}
}]);
return PolygonEdge;
}();
/* harmony default export */ __webpack_exports__["a"] = (PolygonEdge);
/***/ }),
/* 3 */
/***/ (function(module, exports) {
function _cross(o, a, b) {
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
}
function _upperTangent(pointset) {
var lower = [];
for (var l = 0; l < pointset.length; l++) {
while (lower.length >= 2 && (_cross(lower[lower.length - 2], lower[lower.length - 1], pointset[l]) <= 0)) {
lower.pop();
}
lower.push(pointset[l]);
}
lower.pop();
return lower;
}
function _lowerTangent(pointset) {
var reversed = pointset.reverse(),
upper = [];
for (var u = 0; u < reversed.length; u++) {
while (upper.length >= 2 && (_cross(upper[upper.length - 2], upper[upper.length - 1], reversed[u]) <= 0)) {
upper.pop();
}
upper.push(reversed[u]);
}
upper.pop();
return upper;
}
// pointset has to be sorted by X
function convex(pointset) {
var convex,
upper = _upperTangent(pointset),
lower = _lowerTangent(pointset);
convex = lower.concat(upper);
convex.push(pointset[0]);
return convex;
}
module.exports = convex;
/***/ }),
/* 4 */
/***/ (function(module, exports) {
module.exports = {
toXy: function(pointset, format) {
if (format === undefined) {
return pointset.slice();
}
return pointset.map(function(pt) {
/*jslint evil: true */
var _getXY = new Function('pt', 'return [pt' + format[0] + ',' + 'pt' + format[1] + '];');
return _getXY(pt);
});
},
fromXy: function(pointset, format) {
if (format === undefined) {
return pointset.slice();
}
return pointset.map(function(pt) {
/*jslint evil: true */
var _getObj = new Function('pt', 'var o = {}; o' + format[0] + '= pt[0]; o' + format[1] + '= pt[1]; return o;');
return _getObj(pt);
});
}
}
/***/ }),
/* 5 */
/***/ (function(module, exports) {
function Grid(points, cellSize) {
this._cells = [];
this._cellSize = cellSize;
points.forEach(function(point) {
var cellXY = this.point2CellXY(point),
x = cellXY[0],
y = cellXY[1];
if (this._cells[x] === undefined) {
this._cells[x] = [];
}
if (this._cells[x][y] === undefined) {
this._cells[x][y] = [];
}
this._cells[x][y].push(point);
}, this);
}
Grid.prototype = {
cellPoints: function(x, y) { // (Number, Number) -> Array
return (this._cells[x] !== undefined && this._cells[x][y] !== undefined) ? this._cells[x][y] : [];
},
rangePoints: function(bbox) { // (Array) -> Array
var tlCellXY = this.point2CellXY([bbox[0], bbox[1]]),
brCellXY = this.point2CellXY([bbox[2], bbox[3]]),
points = [];
for (var x = tlCellXY[0]; x <= brCellXY[0]; x++) {
for (var y = tlCellXY[1]; y <= brCellXY[1]; y++) {
points = points.concat(this.cellPoints(x, y));
}
}
return points;
},
removePoint: function(point) { // (Array) -> Array
var cellXY = this.point2CellXY(point),
cell = this._cells[cellXY[0]][cellXY[1]],
pointIdxInCell;
for (var i = 0; i < cell.length; i++) {
if (cell[i][0] === point[0] && cell[i][1] === point[1]) {
pointIdxInCell = i;
break;
}
}
cell.splice(pointIdxInCell, 1);
return cell;
},
point2CellXY: function(point) { // (Array) -> Array
var x = parseInt(point[0] / this._cellSize),
y = parseInt(point[1] / this._cellSize);
return [x, y];
},
extendBbox: function(bbox, scaleFactor) { // (Array, Number) -> Array
return [
bbox[0] - (scaleFactor * this._cellSize),
bbox[1] - (scaleFactor * this._cellSize),
bbox[2] + (scaleFactor * this._cellSize),
bbox[3] + (scaleFactor * this._cellSize)
];
}
};
function grid(points, cellSize) {
return new Grid(points, cellSize);
}
module.exports = grid;
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/*
(c) 2014-2016, Andrii Heonia
Hull.js, a JavaScript library for concave hull generation by set of points.
https://github.com/AndriiHeonia/hull
*/
var intersect = __webpack_require__(7);
var grid = __webpack_require__(5);
var formatUtil = __webpack_require__(4);
var convexHull = __webpack_require__(3);
function _filterDuplicates(pointset) {
return pointset.filter(function(el, idx, arr) {
var prevEl = arr[idx - 1];
return idx === 0 || !(prevEl[0] === el[0] && prevEl[1] === el[1]);
});
}
function _sortByX(pointset) {
return pointset.sort(function(a, b) {
if (a[0] == b[0]) {
return a[1] - b[1];
} else {
return a[0] - b[0];
}
});
}
function _sqLength(a, b) {
return Math.pow(b[0] - a[0], 2) + Math.pow(b[1] - a[1], 2);
}
function _cos(o, a, b) {
var aShifted = [a[0] - o[0], a[1] - o[1]],
bShifted = [b[0] - o[0], b[1] - o[1]],
sqALen = _sqLength(o, a),
sqBLen = _sqLength(o, b),
dot = aShifted[0] * bShifted[0] + aShifted[1] * bShifted[1];
return dot / Math.sqrt(sqALen * sqBLen);
}
function _intersect(segment, pointset) {
for (var i = 0; i < pointset.length - 1; i++) {
var seg = [pointset[i], pointset[i + 1]];
if (segment[0][0] === seg[0][0] && segment[0][1] === seg[0][1] ||
segment[0][0] === seg[1][0] && segment[0][1] === seg[1][1]) {
continue;
}
if (intersect(segment, seg)) {
return true;
}
}
return false;
}
function _occupiedArea(pointset) {
var minX = Infinity,
minY = Infinity,
maxX = -Infinity,
maxY = -Infinity;
for (var i = pointset.length - 1; i >= 0; i--) {
if (pointset[i][0] < minX) {
minX = pointset[i][0];
}
if (pointset[i][1] < minY) {
minY = pointset[i][1];
}
if (pointset[i][0] > maxX) {
maxX = pointset[i][0];
}
if (pointset[i][1] > maxY) {
maxY = pointset[i][1];
}
}
return [
maxX - minX, // width
maxY - minY // height
];
}
function _bBoxAround(edge) {
return [
Math.min(edge[0][0], edge[1][0]), // left
Math.min(edge[0][1], edge[1][1]), // top
Math.max(edge[0][0], edge[1][0]), // right
Math.max(edge[0][1], edge[1][1]) // bottom
];
}
function _midPoint(edge, innerPoints, convex) {
var point = null,
angle1Cos = MAX_CONCAVE_ANGLE_COS,
angle2Cos = MAX_CONCAVE_ANGLE_COS,
a1Cos, a2Cos;
for (var i = 0; i < innerPoints.length; i++) {
a1Cos = _cos(edge[0], edge[1], innerPoints[i]);
a2Cos = _cos(edge[1], edge[0], innerPoints[i]);
if (a1Cos > angle1Cos && a2Cos > angle2Cos &&
!_intersect([edge[0], innerPoints[i]], convex) &&
!_intersect([edge[1], innerPoints[i]], convex)) {
angle1Cos = a1Cos;
angle2Cos = a2Cos;
point = innerPoints[i];
}
}
return point;
}
function _concave(convex, maxSqEdgeLen, maxSearchArea, grid, edgeSkipList) {
var edge,
keyInSkipList,
scaleFactor,
midPoint,
bBoxAround,
bBoxWidth,
bBoxHeight,
midPointInserted = false;
for (var i = 0; i < convex.length - 1; i++) {
edge = [convex[i], convex[i + 1]];
keyInSkipList = edge[0].join() + ',' + edge[1].join();
if (_sqLength(edge[0], edge[1]) < maxSqEdgeLen ||
edgeSkipList[keyInSkipList] === true) { continue; }
scaleFactor = 0;
bBoxAround = _bBoxAround(edge);
do {
bBoxAround = grid.extendBbox(bBoxAround, scaleFactor);
bBoxWidth = bBoxAround[2] - bBoxAround[0];
bBoxHeight = bBoxAround[3] - bBoxAround[1];
midPoint = _midPoint(edge, grid.rangePoints(bBoxAround), convex);
scaleFactor++;
} while (midPoint === null && (maxSearchArea[0] > bBoxWidth || maxSearchArea[1] > bBoxHeight));
if (bBoxWidth >= maxSearchArea[0] && bBoxHeight >= maxSearchArea[1]) {
edgeSkipList[keyInSkipList] = true;
}
if (midPoint !== null) {
convex.splice(i + 1, 0, midPoint);
grid.removePoint(midPoint);
midPointInserted = true;
}
}
if (midPointInserted) {
return _concave(convex, maxSqEdgeLen, maxSearchArea, grid, edgeSkipList);
}
return convex;
}
function hull(pointset, concavity, format) {
var convex,
concave,
innerPoints,
occupiedArea,
maxSearchArea,
cellSize,
points,
maxEdgeLen = concavity || 20;
if (pointset.length < 4) {
return pointset.slice();
}
points = _filterDuplicates(_sortByX(formatUtil.toXy(pointset, format)));
occupiedArea = _occupiedArea(points);
maxSearchArea = [
occupiedArea[0] * MAX_SEARCH_BBOX_SIZE_PERCENT,
occupiedArea[1] * MAX_SEARCH_BBOX_SIZE_PERCENT
];
convex = convexHull(points);
innerPoints = points.filter(function(pt) {
return convex.indexOf(pt) < 0;
});
cellSize = Math.ceil(1 / (points.length / (occupiedArea[0] * occupiedArea[1])));
concave = _concave(
convex, Math.pow(maxEdgeLen, 2),
maxSearchArea, grid(innerPoints, cellSize), {});
return formatUtil.fromXy(concave, format);
}
var MAX_CONCAVE_ANGLE_COS = Math.cos(90 / (180 / Math.PI)); // angle = 90 deg
var MAX_SEARCH_BBOX_SIZE_PERCENT = 0.6;
module.exports = hull;
/***/ }),
/* 7 */
/***/ (function(module, exports) {
function ccw(x1, y1, x2, y2, x3, y3) {
var cw = ((y3 - y1) * (x2 - x1)) - ((y2 - y1) * (x3 - x1));
return cw > 0 ? true : cw < 0 ? false : true; // colinear
}
function intersect(seg1, seg2) {
var x1 = seg1[0][0], y1 = seg1[0][1],
x2 = seg1[1][0], y2 = seg1[1][1],
x3 = seg2[0][0], y3 = seg2[0][1],
x4 = seg2[1][0], y4 = seg2[1][1];
return ccw(x1, y1, x3, y3, x4, y4) !== ccw(x2, y2, x3, y3, x4, y4) && ccw(x1, y1, x2, y2, x3, y3) !== ccw(x1, y1, x2, y2, x4, y4);
}
module.exports = intersect;
/***/ })
/******/ ]);
});
//# sourceMappingURL=phaser-tiled-hull.js.map

2

dist/phaser-tiled-hull.min.js

@@ -1,2 +0,2 @@

!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.phaserTiledHull=n():t.phaserTiledHull=n()}(this,function(){return function(t){function n(e){if(r[e])return r[e].exports;var o=r[e]={i:e,l:!1,exports:{}};return t[e].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r={};return n.m=t,n.c=r,n.i=function(t){return t},n.d=function(t,r,e){n.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:e})},n.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(r,"a",r),r},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=1)}([function(t,n,r){"use strict";function e(t){if(Array.isArray(t)){for(var n=0,r=Array(t.length);n<t.length;n++)r[n]=t[n];return r}return Array.from(t)}function o(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.tileIndices,e=void 0===r?null:r,o=n.tileProperty,a=void 0===o?null:o,f=n.checkCollide,c=void 0!==f&&f,p=i(t,e,a,c),v=u(p),y=l(v),d=[],x=!0,g=!1,m=void 0;try{for(var b,w=y.entries()[Symbol.iterator]();!(x=(b=w.next()).done);x=!0){var _=b.value,P=h(_,2),S=P[0],M=P[1],j=[],O=!0,X=!1,z=void 0;try{for(var C,A=M[Symbol.iterator]();!(O=(C=A.next()).done);O=!0){var I=C.value;j.push(new s.a(I,S))}}catch(t){X=!0,z=t}finally{try{!O&&A.return&&A.return()}finally{if(X)throw z}}d.push(j)}}catch(t){g=!0,m=t}finally{try{!x&&w.return&&w.return()}finally{if(g)throw m}}return d}function i(t,n,r,e){function o(t){return!!t&&(!(!n||!n.includes(t.index))||(!(!r||!t.properties[r])||!(!e||!t.collides)))}function i(t,n,r){var e=a(t,n);o(e)&&-1===r.indexOf(e)&&(r.push(e),i(t,n-1,r),i(t,n+1,r),i(t+1,n,r),i(t-1,n,r))}for(var u=t.map,l=[],a=function(n,r){return u.getTile(n,r,t.index)},f=0;f<u.width;f++)for(var c=0;c<u.height;c++){var s=a(f,c);if(o(s)&&!function(t){var n=!0,r=!1,e=void 0;try{for(var o,i=l[Symbol.iterator]();!(n=(o=i.next()).done);n=!0){var u=o.value,a=!0,f=!1,c=void 0;try{for(var s,h=u[Symbol.iterator]();!(a=(s=h.next()).done);a=!0){if(t===s.value)return u}}catch(t){f=!0,c=t}finally{try{!a&&h.return&&h.return()}finally{if(f)throw c}}}}catch(t){r=!0,e=t}finally{try{!n&&i.return&&i.return()}finally{if(r)throw e}}return null}(s)){var h=[];i(f,c,h),l.push(h)}}return l}function u(t){var n=[],r=!0,e=!1,o=void 0;try{for(var i,u=t[Symbol.iterator]();!(r=(i=u.next()).done);r=!0){var l=i.value,a=[],f=!0,s=!1,h=void 0;try{for(var p,v=l[Symbol.iterator]();!(f=(p=v.next()).done);f=!0){var y=p.value;a.push([y.left,y.top],[y.right,y.top],[y.left,y.bottom],[y.right,y.bottom])}}catch(t){s=!0,h=t}finally{try{!f&&v.return&&v.return()}finally{if(s)throw h}}var d=c()(a,1);n.push(d)}}catch(t){e=!0,o=t}finally{try{!r&&u.return&&u.return()}finally{if(e)throw o}}return n}function l(t){var n=[],r=!0,o=!1,i=void 0;try{for(var u,l=t[Symbol.iterator]();!(r=(u=l.next()).done);r=!0){for(var f=u.value,c=[],s=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[0]),e(f[1])))),h=void 0,p=1;p<f.length;p++)h=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[p-1]),e(f[p])))),a(s,h)?s=new Phaser.Line(s.start.x,s.start.y,h.end.x,h.end.y):(c.push(s),s=h.clone());if(h=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[f.length-1]),e(f[0])))),a(s,h)?(s=new Phaser.Line(s.start.x,s.start.y,h.end.x,h.end.y),c.push(s)):(c.push(s),c.push(h)),a(c[0],c[c.length-1])){var v=c.shift(),y=c.pop(),d=new Phaser.Line(v.start.x,v.start.y,y.end.x,y.end.y);c.push(d)}n.push(c)}}catch(t){o=!0,i=t}finally{try{!r&&l.return&&l.return()}finally{if(o)throw i}}return n}function a(t,n){var r=t.end.x-t.start.x,e=t.end.y-t.start.y,o=n.end.x-n.start.x;return r*(n.end.y-n.start.y)-e*o==0}Object.defineProperty(n,"__esModule",{value:!0});var f=r(6),c=r.n(f),s=r(2),h=function(){function t(t,n){var r=[],e=!0,o=!1,i=void 0;try{for(var u,l=t[Symbol.iterator]();!(e=(u=l.next()).done)&&(r.push(u.value),!n||r.length!==n);e=!0);}catch(t){o=!0,i=t}finally{try{!e&&l.return&&l.return()}finally{if(o)throw i}}return r}return function(n,r){if(Array.isArray(n))return n;if(Symbol.iterator in Object(n))return t(n,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();n.default=o},function(t,n,r){t.exports=r(0).default},function(t,n,r){"use strict";function e(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}var o=function(){function t(t,n){for(var r=0;r<n.length;r++){var e=n[r];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}return function(n,r,e){return r&&t(n.prototype,r),e&&t(n,e),n}}(),i=function(){function t(n,r){e(this,t),this.line=n,this.length=n.length,this.midpoint=n.midPoint(),this.normal=this._getOutwardNormal(n),this.shapeId=r}return o(t,[{key:"_getOutwardNormal",value:function(t){return new Phaser.Point(t.end.y-t.start.y,-(t.end.x-t.start.x)).setMagnitude(1)}}]),t}();n.a=i},function(t,n){function r(t,n,r){return(n[0]-t[0])*(r[1]-t[1])-(n[1]-t[1])*(r[0]-t[0])}function e(t){for(var n=[],e=0;e<t.length;e++){for(;n.length>=2&&r(n[n.length-2],n[n.length-1],t[e])<=0;)n.pop();n.push(t[e])}return n.pop(),n}function o(t){for(var n=t.reverse(),e=[],o=0;o<n.length;o++){for(;e.length>=2&&r(e[e.length-2],e[e.length-1],n[o])<=0;)e.pop();e.push(n[o])}return e.pop(),e}function i(t){var n,r=e(t);return n=o(t).concat(r),n.push(t[0]),n}t.exports=i},function(t,n){t.exports={toXy:function(t,n){return void 0===n?t.slice():t.map(function(t){return new Function("pt","return [pt"+n[0]+",pt"+n[1]+"];")(t)})},fromXy:function(t,n){return void 0===n?t.slice():t.map(function(t){return new Function("pt","var o = {}; o"+n[0]+"= pt[0]; o"+n[1]+"= pt[1]; return o;")(t)})}}},function(t,n){function r(t,n){this._cells=[],this._cellSize=n,t.forEach(function(t){var n=this.point2CellXY(t),r=n[0],e=n[1];void 0===this._cells[r]&&(this._cells[r]=[]),void 0===this._cells[r][e]&&(this._cells[r][e]=[]),this._cells[r][e].push(t)},this)}function e(t,n){return new r(t,n)}r.prototype={cellPoints:function(t,n){return void 0!==this._cells[t]&&void 0!==this._cells[t][n]?this._cells[t][n]:[]},rangePoints:function(t){for(var n=this.point2CellXY([t[0],t[1]]),r=this.point2CellXY([t[2],t[3]]),e=[],o=n[0];o<=r[0];o++)for(var i=n[1];i<=r[1];i++)e=e.concat(this.cellPoints(o,i));return e},removePoint:function(t){for(var n,r=this.point2CellXY(t),e=this._cells[r[0]][r[1]],o=0;o<e.length;o++)if(e[o][0]===t[0]&&e[o][1]===t[1]){n=o;break}return e.splice(n,1),e},point2CellXY:function(t){return[parseInt(t[0]/this._cellSize),parseInt(t[1]/this._cellSize)]},extendBbox:function(t,n){return[t[0]-n*this._cellSize,t[1]-n*this._cellSize,t[2]+n*this._cellSize,t[3]+n*this._cellSize]}},t.exports=e},function(t,n,r){"use strict";function e(t){return t.filter(function(t,n,r){var e=r[n-1];return 0===n||!(e[0]===t[0]&&e[1]===t[1])})}function o(t){return t.sort(function(t,n){return t[0]==n[0]?t[1]-n[1]:t[0]-n[0]})}function i(t,n){return Math.pow(n[0]-t[0],2)+Math.pow(n[1]-t[1],2)}function u(t,n,r){var e=[n[0]-t[0],n[1]-t[1]],o=[r[0]-t[0],r[1]-t[1]],u=i(t,n),l=i(t,r);return(e[0]*o[0]+e[1]*o[1])/Math.sqrt(u*l)}function l(t,n){for(var r=0;r<n.length-1;r++){var e=[n[r],n[r+1]];if(!(t[0][0]===e[0][0]&&t[0][1]===e[0][1]||t[0][0]===e[1][0]&&t[0][1]===e[1][1])&&p(t,e))return!0}return!1}function a(t){for(var n=1/0,r=1/0,e=-1/0,o=-1/0,i=t.length-1;i>=0;i--)t[i][0]<n&&(n=t[i][0]),t[i][1]<r&&(r=t[i][1]),t[i][0]>e&&(e=t[i][0]),t[i][1]>o&&(o=t[i][1]);return[e-n,o-r]}function f(t){return[Math.min(t[0][0],t[1][0]),Math.min(t[0][1],t[1][1]),Math.max(t[0][0],t[1][0]),Math.max(t[0][1],t[1][1])]}function c(t,n,r){for(var e,o,i=null,a=x,f=x,c=0;c<n.length;c++)e=u(t[0],t[1],n[c]),o=u(t[1],t[0],n[c]),e>a&&o>f&&!l([t[0],n[c]],r)&&!l([t[1],n[c]],r)&&(a=e,f=o,i=n[c]);return i}function s(t,n,r,e,o){for(var u,l,a,h,p,v,y,d=!1,x=0;x<t.length-1;x++)if(u=[t[x],t[x+1]],l=u[0].join()+","+u[1].join(),!(i(u[0],u[1])<n||!0===o[l])){a=0,p=f(u);do{p=e.extendBbox(p,a),v=p[2]-p[0],y=p[3]-p[1],h=c(u,e.rangePoints(p),t),a++}while(null===h&&(r[0]>v||r[1]>y));v>=r[0]&&y>=r[1]&&(o[l]=!0),null!==h&&(t.splice(x+1,0,h),e.removePoint(h),d=!0)}return d?s(t,n,r,e,o):t}function h(t,n,r){var i,u,l,f,c,h,p,x=n||20;return t.length<4?t.slice():(p=e(o(y.toXy(t,r))),f=a(p),c=[f[0]*g,f[1]*g],i=d(p),l=p.filter(function(t){return i.indexOf(t)<0}),h=Math.ceil(1/(p.length/(f[0]*f[1]))),u=s(i,Math.pow(x,2),c,v(l,h),{}),y.fromXy(u,r))}var p=r(7),v=r(5),y=r(4),d=r(3),x=Math.cos(90/(180/Math.PI)),g=.6;t.exports=h},function(t,n){function r(t,n,r,e,o,i){var u=(i-n)*(r-t)-(e-n)*(o-t);return u>0||!(u<0)}function e(t,n){var e=t[0][0],o=t[0][1],i=t[1][0],u=t[1][1],l=n[0][0],a=n[0][1],f=n[1][0],c=n[1][1];return r(e,o,l,a,f,c)!==r(i,u,l,a,f,c)&&r(e,o,i,u,l,a)!==r(e,o,i,u,f,c)}t.exports=e}])});
!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.phaserTiledHull=n():t.phaserTiledHull=n()}(this,function(){return function(t){function n(e){if(r[e])return r[e].exports;var o=r[e]={i:e,l:!1,exports:{}};return t[e].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r={};return n.m=t,n.c=r,n.i=function(t){return t},n.d=function(t,r,e){n.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:e})},n.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(r,"a",r),r},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=1)}([function(t,n,r){"use strict";function e(t){if(Array.isArray(t)){for(var n=0,r=Array(t.length);n<t.length;n++)r[n]=t[n];return r}return Array.from(t)}function o(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.tileIndices,e=void 0===r?null:r,o=n.tileProperty,a=void 0===o?null:o,f=n.checkCollide,c=void 0!==f&&f,p=i(t,e,a,c),v=u(p),y=l(v),d=[],x=!0,g=!1,m=void 0;try{for(var b,w=y.entries()[Symbol.iterator]();!(x=(b=w.next()).done);x=!0){var _=b.value,P=h(_,2),S=P[0],M=P[1],j=[],O=!0,X=!1,z=void 0;try{for(var C,A=M[Symbol.iterator]();!(O=(C=A.next()).done);O=!0){var I=C.value;j.push(new s.a(I,S))}}catch(t){X=!0,z=t}finally{try{!O&&A.return&&A.return()}finally{if(X)throw z}}d.push(j)}}catch(t){g=!0,m=t}finally{try{!x&&w.return&&w.return()}finally{if(g)throw m}}return d}function i(t,n,r,e){function o(t){return!!t&&(!(!n||!n.includes(t.index))||(!(!r||!t.properties[r])||!(!e||!t.collides)))}function i(t,n,r){var e=a(t,n);o(e)&&-1===r.indexOf(e)&&(r.push(e),i(t,n-1,r),i(t,n+1,r),i(t+1,n,r),i(t-1,n,r))}for(var u=t.map,l=[],a=function(n,r){return u.getTile(n,r,t.index)},f=0;f<u.width;f++)for(var c=0;c<u.height;c++){var s=a(f,c);if(o(s)&&!function(t){var n=!0,r=!1,e=void 0;try{for(var o,i=l[Symbol.iterator]();!(n=(o=i.next()).done);n=!0){var u=o.value,a=!0,f=!1,c=void 0;try{for(var s,h=u[Symbol.iterator]();!(a=(s=h.next()).done);a=!0){if(t===s.value)return u}}catch(t){f=!0,c=t}finally{try{!a&&h.return&&h.return()}finally{if(f)throw c}}}}catch(t){r=!0,e=t}finally{try{!n&&i.return&&i.return()}finally{if(r)throw e}}return null}(s)){var h=[];i(f,c,h),l.push(h)}}return l}function u(t){var n=[],r=!0,e=!1,o=void 0;try{for(var i,u=t[Symbol.iterator]();!(r=(i=u.next()).done);r=!0){var l=i.value,a=[],f=!0,s=!1,h=void 0;try{for(var p,v=l[Symbol.iterator]();!(f=(p=v.next()).done);f=!0){var y=p.value;a.push([y.left,y.top],[y.right,y.top],[y.left,y.bottom],[y.right,y.bottom])}}catch(t){s=!0,h=t}finally{try{!f&&v.return&&v.return()}finally{if(s)throw h}}var d=c()(a,1);n.push(d)}}catch(t){e=!0,o=t}finally{try{!r&&u.return&&u.return()}finally{if(e)throw o}}return n}function l(t){var n=[],r=!0,o=!1,i=void 0;try{for(var u,l=t[Symbol.iterator]();!(r=(u=l.next()).done);r=!0){for(var f=u.value,c=[],s=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[0]),e(f[1])))),h=void 0,p=1;p<f.length;p++)h=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[p-1]),e(f[p])))),a(s,h)?s=new Phaser.Line(s.start.x,s.start.y,h.end.x,h.end.y):(c.push(s),s=h.clone());if(h=new(Function.prototype.bind.apply(Phaser.Line,[null].concat(e(f[f.length-1]),e(f[0])))),a(s,h)?(s=new Phaser.Line(s.start.x,s.start.y,h.end.x,h.end.y),c.push(s)):(c.push(s),c.push(h)),a(c[0],c[c.length-1])){var v=c.shift(),y=c.pop(),d=new Phaser.Line(v.start.x,v.start.y,y.end.x,y.end.y);c.push(d)}n.push(c)}}catch(t){o=!0,i=t}finally{try{!r&&l.return&&l.return()}finally{if(o)throw i}}return n}function a(t,n){var r=t.end.x-t.start.x,e=t.end.y-t.start.y,o=n.end.x-n.start.x;return r*(n.end.y-n.start.y)-e*o==0}Object.defineProperty(n,"__esModule",{value:!0}),n.default=o;var f=r(6),c=r.n(f),s=r(2),h=function(){function t(t,n){var r=[],e=!0,o=!1,i=void 0;try{for(var u,l=t[Symbol.iterator]();!(e=(u=l.next()).done)&&(r.push(u.value),!n||r.length!==n);e=!0);}catch(t){o=!0,i=t}finally{try{!e&&l.return&&l.return()}finally{if(o)throw i}}return r}return function(n,r){if(Array.isArray(n))return n;if(Symbol.iterator in Object(n))return t(n,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}()},function(t,n,r){t.exports=r(0).default},function(t,n,r){"use strict";function e(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}var o=function(){function t(t,n){for(var r=0;r<n.length;r++){var e=n[r];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}return function(n,r,e){return r&&t(n.prototype,r),e&&t(n,e),n}}(),i=function(){function t(n,r){e(this,t),this.line=n,this.length=n.length,this.midpoint=n.midPoint(),this.normal=this._getOutwardNormal(n),this.shapeId=r}return o(t,[{key:"_getOutwardNormal",value:function(t){return new Phaser.Point(t.end.y-t.start.y,-(t.end.x-t.start.x)).setMagnitude(1)}}]),t}();n.a=i},function(t,n){function r(t,n,r){return(n[0]-t[0])*(r[1]-t[1])-(n[1]-t[1])*(r[0]-t[0])}function e(t){for(var n=[],e=0;e<t.length;e++){for(;n.length>=2&&r(n[n.length-2],n[n.length-1],t[e])<=0;)n.pop();n.push(t[e])}return n.pop(),n}function o(t){for(var n=t.reverse(),e=[],o=0;o<n.length;o++){for(;e.length>=2&&r(e[e.length-2],e[e.length-1],n[o])<=0;)e.pop();e.push(n[o])}return e.pop(),e}function i(t){var n,r=e(t);return n=o(t).concat(r),n.push(t[0]),n}t.exports=i},function(t,n){t.exports={toXy:function(t,n){return void 0===n?t.slice():t.map(function(t){return new Function("pt","return [pt"+n[0]+",pt"+n[1]+"];")(t)})},fromXy:function(t,n){return void 0===n?t.slice():t.map(function(t){return new Function("pt","var o = {}; o"+n[0]+"= pt[0]; o"+n[1]+"= pt[1]; return o;")(t)})}}},function(t,n){function r(t,n){this._cells=[],this._cellSize=n,t.forEach(function(t){var n=this.point2CellXY(t),r=n[0],e=n[1];void 0===this._cells[r]&&(this._cells[r]=[]),void 0===this._cells[r][e]&&(this._cells[r][e]=[]),this._cells[r][e].push(t)},this)}function e(t,n){return new r(t,n)}r.prototype={cellPoints:function(t,n){return void 0!==this._cells[t]&&void 0!==this._cells[t][n]?this._cells[t][n]:[]},rangePoints:function(t){for(var n=this.point2CellXY([t[0],t[1]]),r=this.point2CellXY([t[2],t[3]]),e=[],o=n[0];o<=r[0];o++)for(var i=n[1];i<=r[1];i++)e=e.concat(this.cellPoints(o,i));return e},removePoint:function(t){for(var n,r=this.point2CellXY(t),e=this._cells[r[0]][r[1]],o=0;o<e.length;o++)if(e[o][0]===t[0]&&e[o][1]===t[1]){n=o;break}return e.splice(n,1),e},point2CellXY:function(t){return[parseInt(t[0]/this._cellSize),parseInt(t[1]/this._cellSize)]},extendBbox:function(t,n){return[t[0]-n*this._cellSize,t[1]-n*this._cellSize,t[2]+n*this._cellSize,t[3]+n*this._cellSize]}},t.exports=e},function(t,n,r){"use strict";function e(t){return t.filter(function(t,n,r){var e=r[n-1];return 0===n||!(e[0]===t[0]&&e[1]===t[1])})}function o(t){return t.sort(function(t,n){return t[0]==n[0]?t[1]-n[1]:t[0]-n[0]})}function i(t,n){return Math.pow(n[0]-t[0],2)+Math.pow(n[1]-t[1],2)}function u(t,n,r){var e=[n[0]-t[0],n[1]-t[1]],o=[r[0]-t[0],r[1]-t[1]],u=i(t,n),l=i(t,r);return(e[0]*o[0]+e[1]*o[1])/Math.sqrt(u*l)}function l(t,n){for(var r=0;r<n.length-1;r++){var e=[n[r],n[r+1]];if(!(t[0][0]===e[0][0]&&t[0][1]===e[0][1]||t[0][0]===e[1][0]&&t[0][1]===e[1][1])&&p(t,e))return!0}return!1}function a(t){for(var n=1/0,r=1/0,e=-1/0,o=-1/0,i=t.length-1;i>=0;i--)t[i][0]<n&&(n=t[i][0]),t[i][1]<r&&(r=t[i][1]),t[i][0]>e&&(e=t[i][0]),t[i][1]>o&&(o=t[i][1]);return[e-n,o-r]}function f(t){return[Math.min(t[0][0],t[1][0]),Math.min(t[0][1],t[1][1]),Math.max(t[0][0],t[1][0]),Math.max(t[0][1],t[1][1])]}function c(t,n,r){for(var e,o,i=null,a=x,f=x,c=0;c<n.length;c++)e=u(t[0],t[1],n[c]),o=u(t[1],t[0],n[c]),e>a&&o>f&&!l([t[0],n[c]],r)&&!l([t[1],n[c]],r)&&(a=e,f=o,i=n[c]);return i}function s(t,n,r,e,o){for(var u,l,a,h,p,v,y,d=!1,x=0;x<t.length-1;x++)if(u=[t[x],t[x+1]],l=u[0].join()+","+u[1].join(),!(i(u[0],u[1])<n||!0===o[l])){a=0,p=f(u);do{p=e.extendBbox(p,a),v=p[2]-p[0],y=p[3]-p[1],h=c(u,e.rangePoints(p),t),a++}while(null===h&&(r[0]>v||r[1]>y));v>=r[0]&&y>=r[1]&&(o[l]=!0),null!==h&&(t.splice(x+1,0,h),e.removePoint(h),d=!0)}return d?s(t,n,r,e,o):t}function h(t,n,r){var i,u,l,f,c,h,p,x=n||20;return t.length<4?t.slice():(p=e(o(y.toXy(t,r))),f=a(p),c=[f[0]*g,f[1]*g],i=d(p),l=p.filter(function(t){return i.indexOf(t)<0}),h=Math.ceil(1/(p.length/(f[0]*f[1]))),u=s(i,Math.pow(x,2),c,v(l,h),{}),y.fromXy(u,r))}var p=r(7),v=r(5),y=r(4),d=r(3),x=Math.cos(90/(180/Math.PI)),g=.6;t.exports=h},function(t,n){function r(t,n,r,e,o,i){var u=(i-n)*(r-t)-(e-n)*(o-t);return u>0||!(u<0)}function e(t,n){var e=t[0][0],o=t[0][1],i=t[1][0],u=t[1][1],l=n[0][0],a=n[0][1],f=n[1][0],c=n[1][1];return r(e,o,l,a,f,c)!==r(i,u,l,a,f,c)&&r(e,o,i,u,l,a)!==r(e,o,i,u,f,c)}t.exports=e}])});
//# sourceMappingURL=phaser-tiled-hull.min.js.map
{
"name": "phaser-tiled-hull",
"version": "1.0.1",
"description": "A plugin for Phaser for reducing neighboring tiles into a single shape (a hull)",
"version": "1.0.2",
"description": "A module for Phaser for reducing neighboring tiles into a single shape (a hull)",
"main": "dist/phaser-tiled-hull.min.js",
"scripts": {
"build:library": "webpack -p --config config/webpack.config.library.js",
"build:example": "webpack -p --config config/webpack.config.example.js",
"build:library": "webpack --config config/webpack.config.library.js",
"build:example": "webpack --config config/webpack.config.example.js",
"build:all": "concurrently \"npm run build:library\" \"npm run build:example\"",

@@ -16,7 +16,7 @@ "watch:library": "webpack --watch --config config/webpack.config.library.js",

"clean:builds": "rimraf dist public",
"clean:docs": "rimraf out",
"clean:all": "rimraf dist public out",
"doc": "jsdoc -c config/jsdoc.config.json",
"postdoc": "cp -r doc-images/ jsdocs/",
"deploy:doc": "gh-pages --branch gh-pages --dist jsdocs --dest docs",
"clean:docs": "rimraf docs",
"clean:all": "concurrently \"npm run clean:builds\" \"npm run clean:docs\"",
"doc": "esdoc -c config/esdoc.json",
"postdoc": "cp -r doc-source/ docs/",
"deploy:doc": "gh-pages --branch gh-pages --dist docs --dest docs",
"predeploy:doc": "npm run doc"

@@ -30,3 +30,8 @@ },

"transform": [
["babelify", {"only": "src/**/*"}]
[
"babelify",
{
"only": "src/**/*"
}
]
]

@@ -64,6 +69,5 @@ },

"copy-webpack-plugin": "^4.0.1",
"docdash": "^0.4.0",
"esdoc": "^0.5.2",
"expose-loader": "^0.7.3",
"gh-pages": "^1.0.0",
"jsdoc": "^3.4.3",
"phaser-ce": "^2.8.0",

@@ -70,0 +74,0 @@ "rimraf": "^2.6.1",

@@ -7,3 +7,3 @@ # Hulls from Tilemaps in Phaser

<img src="./doc-images/demo.png" style="width: 100%">
<img src="./doc-source/demo.png" style="width: 100%">

@@ -20,5 +20,5 @@ (Note: if you are viewing this on GitHub or NPM, you might want to check out the HTML documentation [here](https://www.mikewesthad.com/phaser-tiled-hull/docs/).)

```html
<script src="dist/phaser-tiled-hull.min.js"></script>
```
<script src="dist/phaser-tiled-hull.min.js><script>
```

@@ -101,4 +101,3 @@ Inside of your own script, you can now use the global `phaserTiledHull`:

- Publish to npm
- Add Phaser as an external dependency so that it doesn't have to be on the global. Relevant mostly for when Phaser v3 is released.
- When a version of Phaser is released that doesn't require a global variable (v3?), then modify the library to include Phaser as an external dependency (vs assuming it is on the global object).
- hull.js can't handle 1x tile concave gaps. Submit PR to hull.js to fix that. For example:

@@ -138,2 +137,2 @@

@mikewesthad, @retwedt
@mikewesthad, @retwedt
/**
* Small class for precomuting some useful information about an edge of a polygon - the line,
* length, midpoint, normal. This class isn't exposed directly by the module, but is used in the
* return type of {@link module:phaser-tiled-hull~phaserTiledHull}.
*
* @class PolygonEdge
* return type of {@link phaserTiledHull}.
*/
class PolygonEdge {
export default class PolygonEdge {
constructor(line, hullIndex) {

@@ -41,4 +39,2 @@ /**

}
}
export default PolygonEdge;
}

@@ -1,6 +0,1 @@

/**
* Phaser tiled hull module
* @module phaser-tiled-hull
*/
import hulljs from "hull.js"; // JS extension required

@@ -30,3 +25,3 @@ import PolygonEdge from "./polygon-edge";

*/
function phaserTiledHull(tilemapLayer, {tileIndices = null, tileProperty = null,
export default function phaserTiledHull(tilemapLayer, {tileIndices = null, tileProperty = null,
checkCollide = false} = {}) {

@@ -202,4 +197,2 @@ // Separate the tilemap layer into an array of clustered tiles

return ((dx1 * dy2) - (dy1 * dx2)) === 0;
}
export default phaserTiledHull;
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc