pixi-tiled-utils
Version:
PIXI.js Tiled utilities
1,064 lines (876 loc) • 100 kB
JavaScript
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./lib/app.js":
/*!********************!*\
!*** ./lib/app.js ***!
\********************/
/***/ ((module) => {
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var PIXI = window.PIXI;
/**
* class FullscreenApplication
*/
var FullscreenApplication = /*#__PURE__*/function (_PIXI$Application) {
_inherits(FullscreenApplication, _PIXI$Application);
var _super = _createSuper(FullscreenApplication);
/**
* @param {function} [everyTick]
* @param {PIXI.ApplicationOptions} [pixiApplicationOptions]
*/
function FullscreenApplication(everyTick, pixiApplicationOptions) {
var _this;
_classCallCheck(this, FullscreenApplication);
_this = _super.call(this, pixiApplicationOptions);
_this.everyTick = (everyTick || function () {}).bind(_assertThisInitialized(_this));
document.body.style.fontSize = 0;
document.body.style.margin = 0;
_this.renderer && document.body.appendChild(_this.renderer.view);
window.addEventListener("resize", _this.requestResize.bind(_assertThisInitialized(_this)));
_this.requestResize();
_this.renderLoop(performance.now());
return _this;
}
/**
* main loop gets initialized at constructor, runs everyTick function
* @param {number} time
*/
_createClass(FullscreenApplication, [{
key: "renderLoop",
value: function renderLoop(time) {
requestAnimationFrame(this.renderLoop.bind(this));
this.everyTick(time);
}
/**
* resizes to window
*/
}, {
key: "requestResize",
value: function requestResize() {
this.renderer && this.renderer.resize(innerWidth, innerHeight);
}
}]);
return FullscreenApplication;
}(PIXI.Application);
module.exports = FullscreenApplication;
/***/ }),
/***/ "./lib/extract.js":
/*!************************!*\
!*** ./lib/extract.js ***!
\************************/
/***/ ((module) => {
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var PIXI = window.PIXI;
/**
* class TextureExtractor
*/
var TextureExtractor = /*#__PURE__*/function () {
function TextureExtractor(_ref) {
var tilewidth = _ref.tilewidth,
tileheight = _ref.tileheight,
texture = _ref.texture,
offset = _ref.offset,
count = _ref.count,
scaleMode = _ref.scaleMode;
_classCallCheck(this, TextureExtractor);
this.tilewidth = tilewidth;
this.tileheight = tileheight;
this.offset = offset || 0;
this.texture = texture;
this.textureCache = [];
this.scaleMode = scaleMode || PIXI.SCALE_MODES.NEAREST;
this._prepareTextures(count);
}
/**
* get inner texture width
* @returns number
*/
_createClass(TextureExtractor, [{
key: "width",
get: function get() {
return this.texture.width;
}
/**
* get inner texture height
* @returns number
*/
}, {
key: "height",
get: function get() {
return this.texture.height;
}
/**
* prepares n-th frame (zero indexed)
* @param {number} frame
* @returns PIXI.Texture
*/
}, {
key: "prepareTexture",
value: function prepareTexture(frame) {
var cols = Math.floor(this.width / this.tilewidth);
var x = (frame - this.offset) % cols * this.tilewidth;
var y = Math.floor((frame - this.offset) / cols) * this.tileheight;
var rect = new PIXI.Rectangle(x, y, this.tilewidth, this.tileheight);
var texture = new PIXI.Texture(this.texture, rect);
texture.baseTexture.scaleMode = this.scaleMode;
texture.cacheAsBitmap = true;
return texture;
}
/**
* gets n-th frame (zero indexed)
* @param {number} frame
* @returns PIXI.Texture
*/
}, {
key: "getFrame",
value: function getFrame(frame) {
if (!this.textureCache[frame]) {
this.prepareTexture(frame);
}
return this.textureCache[frame];
}
/**
* private prepares cache up to count, used in constructor
* @param {number} count
*/
}, {
key: "_prepareTextures",
value: function _prepareTextures(count) {
var _this = this;
var size = count || this.width / this.tilewidth * (this.height / this.tileheight);
this.textureCache = new Array(size).fill(0).map(function (_, frame) {
return _this.prepareTexture(frame);
});
}
}]);
return TextureExtractor;
}();
module.exports = TextureExtractor;
/***/ }),
/***/ "./lib/index.js":
/*!**********************!*\
!*** ./lib/index.js ***!
\**********************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
var FullscreenApplication = __webpack_require__(/*! ./app */ "./lib/app.js");
var World = __webpack_require__(/*! ./world */ "./lib/world.js");
var TextureExtractor = __webpack_require__(/*! ./extract */ "./lib/extract.js");
var utils = __webpack_require__(/*! ./utils */ "./lib/utils.js");
module.exports = {
FullscreenApplication: FullscreenApplication,
World: World,
TextureExtractor: TextureExtractor,
utils: utils
};
/* global window */
window.PIXI.Tiled = {
FullscreenApplication: FullscreenApplication,
World: World,
TextureExtractor: TextureExtractor,
utils: utils
};
/***/ }),
/***/ "./lib/tiled.js":
/*!**********************!*\
!*** ./lib/tiled.js ***!
\**********************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
var PIXI = window.PIXI;
var TiledUtils = __webpack_require__(/*! tiled-utils */ "./node_modules/tiled-utils/es2015/index.js");
var tu = new TiledUtils(PIXI);
module.exports = tu;
/***/ }),
/***/ "./lib/utils.js":
/*!**********************!*\
!*** ./lib/utils.js ***!
\**********************/
/***/ ((module) => {
var PIXI = window.PIXI;
/**
* inner utils, maybe useful for you too
*/
var utils = {
/**
* get texture from tiles
* @param {number} frame
* @param {inner} tiles
* @returns PIXI.Texture
*/
getTexture: function getTexture(frame, tiles) {
if (tiles && typeof tiles.getFrame === "function") {
return tiles.getFrame(frame);
} else {
console.warn("! tiles not set @ getTexture");
}
return PIXI.Texture.EMPTY;
},
/**
* create sprite from frame, tileSizeConfig and tiles
* @param {number} frame
* @param {{ tilewidth, tileheight }} tileSizeConfig
* @param {inner} tiles
* @returns PIXI.Sprite | undefined
*/
createSprite: function createSprite(frame, _ref) {
var tilewidth = _ref.tilewidth,
tileheight = _ref.tileheight;
var tiles = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var sprite;
if (tiles) {
if (!isNaN(frame) && isFinite(frame)) {
sprite = new PIXI.Sprite(utils.getTexture(~~frame, tiles));
} else if (frame) {
sprite = PIXI.Sprite.fromFrame(frame);
}
} else {
sprite = new PIXI.Sprite();
}
if (sprite) {
sprite.width = tilewidth;
sprite.height = tileheight;
if (sprite.texture) {
sprite.texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
}
}
return sprite;
},
/**
* works like lodash.get
* @param {any} source
* @param {string} url
* @returns any
*/
getModel: function getModel(source, url) {
var cursor = source;
var propList = url.split(".");
while (cursor && propList.length) {
cursor = cursor[propList.shift()];
}
return cursor;
},
/**
* checks does array contain keys starting with value
* @param {any[]} array
* @param {any} value
* @returns boolean
*/
contains: function contains(array, value) {
return value && array.some(function (string) {
return value.startsWith(string);
});
},
/**
* groups objects array into object
* @param {any[]} array
* @param {string} groupName
* @returns object
*/
groupObjects: function groupObjects(array) {
var groupName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "group.name";
return array ? array.reduce(function (groups, item) {
var cursor = utils.getModel(item, groupName);
if (cursor) {
if (!groups[cursor]) {
groups[cursor] = [];
}
groups[cursor].push(item);
}
return groups;
}, {}) : {};
},
/**
* copies props like name, type from another object
* @param {object} target
* @param {object} source
* @param {string[]} props
* @returns void
*/
nameObject: function nameObject(target) {
var source = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ["name", "type"];
if (!target) return;
props.forEach(function (prop) {
if (source[prop] && !target[prop]) {
target[prop] = source[prop];
}
});
},
/**
* adds object to array, also names it
* @param {any} source
* @param {any[]} target
* @param {string[]} props
* @returns void
*/
pushObject: function pushObject(source, target, props) {
if (target.indexOf(source) !== -1) return;
if (Array.isArray(source.objects)) {
source.objects.forEach(function (sprite) {
utils.nameObject(sprite, source, props);
});
}
target.push(source);
},
/**
* gets bounds of object, fast
* @param {any[]} array
* @returns object
*/
getBounds: function getBounds(array) {
if (Array.isArray(array) && array.length) {
var up = Infinity;
var left = Infinity;
var right = -Infinity;
var down = -Infinity;
var temp;
array.forEach(function (sprite) {
var w = sprite.width;
var h = sprite.height;
if ((temp = sprite.x) < left) {
left = temp;
}
if ((temp = sprite.y) < up) {
up = temp;
}
if ((temp = sprite.x + w) > right) {
right = temp;
}
if ((temp = sprite.y + h) > down) {
down = temp;
}
});
return {
up: up,
down: down,
left: left,
right: right
};
}
return {
up: 0,
down: 0,
left: 0,
right: 0
};
}
};
module.exports = utils;
/***/ }),
/***/ "./lib/world.js":
/*!**********************!*\
!*** ./lib/world.js ***!
\**********************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var PIXI = window.PIXI;
var TextureExtractor = __webpack_require__(/*! ./extract */ "./lib/extract.js");
var tu = __webpack_require__(/*! ./tiled */ "./lib/tiled.js");
var utils = __webpack_require__(/*! ./utils */ "./lib/utils.js");
/**
* class World
*/
var World = /*#__PURE__*/function () {
/**
* step 1 - extract tiles
* @param {object} config
*/
function World(config) {
_classCallCheck(this, World);
if (config) {
this.tiles = new TextureExtractor(config);
} else {
console.warn("Creating tile-less world");
}
}
/**
* step 2 - create world
* @param {object | string} jsonTiledMap
* @param {PIXI.Sprite} tileset
* @param {object} [layersConfiguration]
* @returns World
*/
_createClass(World, [{
key: "create",
value: function () {
var _create = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(jsonTiledMap, tileset) {
var layersConfiguration,
world,
_args = arguments;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
layersConfiguration = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
this.world = tu.makeTiledWorld(jsonTiledMap, tileset);
this.objects = this._createObjects(layersConfiguration);
this.sprites = this._createSprites(layersConfiguration.clear);
this.ground = this.world.children.filter(function (_ref) {
var type = _ref.type;
return type === "tilelayer";
});
world = new PIXI.Container();
this.ground.length && world.addChild.apply(world, _toConsumableArray(this.ground));
this.sprites.length && world.addChild.apply(world, _toConsumableArray(this.sprites));
return _context.abrupt("return", world);
case 9:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function create(_x, _x2) {
return _create.apply(this, arguments);
}
return create;
}()
/**
* private createObjects from config: { group = [], clear = [], pickable = [] }
* @param {object} options
* @returns object[]
*/
}, {
key: "_createObjects",
value: function _createObjects(_ref2) {
var _this = this;
var _ref2$group = _ref2.group,
group = _ref2$group === void 0 ? [] : _ref2$group,
_ref2$clear = _ref2.clear,
clear = _ref2$clear === void 0 ? [] : _ref2$clear,
_ref2$pickable = _ref2.pickable,
pickable = _ref2$pickable === void 0 ? [] : _ref2$pickable;
var objects = [];
var groups = utils.groupObjects(this.world.objects);
var groupRegExp = group.map(function (string) {
return new RegExp(string);
});
Object.keys(groups).filter(function (name) {
var markedForRemove = utils.contains(clear, name);
return !markedForRemove && !utils.contains(pickable, name);
}).forEach(function (name) {
console.log("\u2523\u2501 parse object: ".concat(name));
var next = groups[name];
var group = groupRegExp.find(function (regExp) {
return name.match(regExp);
});
var container;
if (group) {
container = new PIXI.Container();
utils.pushObject(container, objects);
utils.nameObject(container, {
name: name,
type: "layer"
});
console.log("\u2523\u2501 add layer: ".concat(name));
}
if (Array.isArray(next)) {
next.forEach(function (object) {
if (object.gid) {
var sprite = utils.createSprite(object.gid, _this.world, _this.tiles);
if (sprite) {
utils.nameObject(sprite, object, ["x", "y", "gid", "name"]);
if (group) {
console.log("\u2523\u2501 add object ".concat(object.name, " (").concat(~~sprite.x, "/").concat(~~sprite.y, ") to layer: ").concat(name));
container.addChild(sprite);
} else {
utils.pushObject(sprite, objects);
}
}
}
});
}
});
return objects;
}
/**
* private createSprites with string[] layers exceptions
* @param {string[]} [clear]
* @returns object[]
*/
}, {
key: "_createSprites",
value: function _createSprites() {
var _this2 = this;
var clear = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return this.objects.filter(function (sprite) {
return !utils.contains(clear, sprite.name);
}).map(function (sprite) {
try {
var x = Math.round((sprite.x || 0) + (sprite.offsetx || 0));
var y = Math.round((sprite.y || 0) + (sprite.offsety || 0));
var objects = sprite.children.length ? sprite.children : [sprite];
var bounds = utils.getBounds(objects);
sprite.x = sprite.basex = bounds.left + x;
sprite.y = sprite.basey = bounds.down + y - _this2.world.tileheight;
objects.forEach(function (child) {
child.x -= bounds.left - child.width * 0.5;
child.y -= bounds.down - _this2.world.tileheight;
child.anchor.set(0.5, 1);
});
return sprite;
} catch (err) {
console.error(err.message, err.stack);
}
});
}
}]);
return World;
}();
module.exports = World;
/***/ }),
/***/ "./node_modules/tiled-utils/es2015/index.js":
/*!**************************************************!*\
!*** ./node_modules/tiled-utils/es2015/index.js ***!
\**************************************************/
/***/ ((module) => {
/* global PIXI */
module.exports = module.exports["default"] = class TileUtilities {
constructor(renderingEngine = PIXI) {
if (renderingEngine === undefined) throw new Error('Please assign a rendering engine in the constructor before using bump.js') // Find out which rendering engine is being used (the default is Pixi)
this.renderer = '' // If the `renderingEngine` is Pixi, set up Pixi object aliases
if (renderingEngine.Container && renderingEngine.Sprite) {
this.renderingEngine = renderingEngine
this.renderer = 'pixi'
this.Container = this.renderingEngine.Container
this.TextureCache = this.renderingEngine.utils.TextureCache
this.Texture = this.renderingEngine.Texture
this.Sprite = this.renderingEngine.Sprite
this.Rectangle = this.renderingEngine.Rectangle
this.Graphics = this.renderingEngine.Graphics
this.loader = this.renderingEngine.Loader.shared
this.resources = this.loader.resources
}
} // Make a texture from a frame in another texture or image
frame(source, x, y, width, height) {
// for backend use (with pixi-shim)
// return without frame retangle
if (!source) {
return this.Texture.EMPTY
}
let texture // If the source is a string, it's either a texture in the
// cache or an image file
if (typeof source === 'string') {
if (this.TextureCache[source]) {
texture = this.TextureCache[source].clone()
}
} // If the `source` is a texture, use it
else if (typeof source.clone === 'function') {
texture = source.clone()
} else {
texture = source
}
if (texture) {
// Make a rectangle the size of the sub-image
texture.frame = new this.Rectangle(x, y, width, height)
return texture
}
} // #### getIndex
// The `getIndex` helper method
// converts a sprite's x and y position to an array index number.
// It returns a single index value that tells you the map array
// index number that the sprite is in
getIndex(x, y, tilewidth, tileheight, mapWidthInTiles) {
const index = {} // Convert pixel coordinates to map index coordinates
index.x = Math.floor(x / tilewidth)
index.y = Math.floor(y / tileheight) // Return the index number
return index.x + index.y * mapWidthInTiles
}
/*
#### getTile
The `getTile` helper method
converts a tile's index number into x/y screen
coordinates, and capture's the tile's grid index (`gid`) number.
It returns an object with `x`, `y`, `centerX`, `centerY`, `width`, `height`, `halfWidth`
`halffHeight` and `gid` properties. (The `gid` number is the value that the tile has in the
mapArray) This lets you use the returned object
with the 2d geometric collision functions like `hitTestRectangle`
or `rectangleCollision`
The `world` object requires these properties:
`x`, `y`, `tilewidth`, `tileheight` and `widthInTiles`
*/
getTile(index, mapArray, world) {
const tile = {}
tile.gid = mapArray[index]
tile.width = world.tilewidth
tile.height = world.tileheight
tile.halfWidth = world.tilewidth / 2
tile.halfHeight = world.tileheight / 2
tile.x = index % world.widthInTiles * world.tilewidth + world.x
tile.y = Math.floor(index / world.widthInTiles) * world.tileheight + world.y
tile.gx = tile.x
tile.gy = tile.y
tile.centerX = tile.x + world.tilewidth / 2
tile.centery = tile.y + world.tileheight / 2 // Return the tile object
return tile
}
/*
#### surroundingCells
The `surroundingCells` helper method returns an array containing 9
index numbers of map array cells around any given index number.
Use it for an efficient broadphase/narrowphase collision test.
The 2 arguments are the index number that represents the center cell,
and the width of the map array.
*/
surroundingCells(index, widthInTiles) {
return [index - widthInTiles - 1, index - widthInTiles, index - widthInTiles + 1, index - 1, index, index + 1, index + widthInTiles - 1, index + widthInTiles, index + widthInTiles + 1]
} // #### getPoints
/*
The `getPoints` method takes a sprite and returns
an object that tells you what all its corner points are. The return
object has four properties, each of which is an object with `x` and `y` properties:
- `topLeft`: `x` and `y` properties describing the top left corner
point.
- `topRight`: `x` and `y` properties describing the top right corner
point.
- `bottomLeft`: `x` and `y` properties describing the bottom left corner
point.
- `bottomRight`: `x` and `y` properties describing the bottom right corner
point.
If the sprite has a `collisionArea` property that defines a
smaller rectangular area inside the sprite, that collision
area can be used instead for collisions instead of the sprite's dimensions. Here's
How you could define a `collsionArea` on a sprite called `elf`:
```js
elf.collisionArea = {x: 22, y: 44, width: 20, height: 20};
```
Here's how you could use the `getPoints` method to find all the collision area's corner points.
```js
let cornerPoints = tu.getPoints(elf.collisionArea);
```
*/
getPoints(s) {
const ca = s.collisionArea
if (ca !== undefined) {
return {
topLeft: {
x: s.x + ca.x,
y: s.y + ca.y
},
topRight: {
x: s.x + ca.x + ca.width,
y: s.y + ca.y
},
bottomLeft: {
x: s.x + ca.x,
y: s.y + ca.y + ca.height
},
bottomRight: {
x: s.x + ca.x + ca.width,
y: s.y + ca.y + ca.height
}
}
}
return {
topLeft: {
x: s.x,
y: s.y
},
topRight: {
x: s.x + s.width - 1,
y: s.y
},
bottomLeft: {
x: s.x,
y: s.y + s.height - 1
},
bottomRight: {
x: s.x + s.width - 1,
y: s.y + s.height - 1
}
}
} // ### hitTestTile
/*
`hitTestTile` checks for a
collision between a sprite and a tile in any map array that you
specify. It returns a `collision` object.
`collision.hit` is a Boolean that tells you if a sprite is colliding
with the tile that you're checking. `collision.index` tells you the
map array's index number of the colliding sprite. You can check for
a collision with the tile against "every" corner point on the
sprite, "some" corner points, or the sprite's "center" point.
`hitTestTile` arguments:
sprite, array, collisionTileGridIdNumber, worldObject, spritesPointsToCheck
```js
tu.hitTestTile(sprite, array, collisioGid, world, pointsToCheck);
```
The `world` object (the 4th argument) has to have these properties:
`tileheight`, `tilewidth`, `widthInTiles`.
Here's how you could use `hitTestTile` to check for a collision between a sprite
called `alien` and an array of wall sprites with map gid numbers of 0.
```js
let alienVsFloor = g.hitTestTile(alien, wallMapArray, 0, world, "every");
```
*/
hitTestTile(sprite, mapArray, gidToCheck, world, pointsToCheck) {
// The `checkPoints` helper function Loop through the sprite's corner points to
// find out if they are inside an array cell that you're interested in.
// Return `true` if they are
const checkPoints = key => {
// Get a reference to the current point to check.
// (`topLeft`, `topRight`, `bottomLeft` or `bottomRight` )
const point = sprite.collisionPoints[key] // Find the point's index number in the map array
collision.index = this.getIndex(point.x, point.y, world.tilewidth, world.tileheight, world.widthInTiles) // Find out what the gid value is in the map position
// that the point is currently over
collision.gid = mapArray[collision.index] // If it matches the value of the gid that we're interested, in
// then there's been a collision
if (collision.gid === gidToCheck) {
return true
}
return false
} // Assign "some" as the default value for `pointsToCheck`
pointsToCheck = pointsToCheck || 'some' // The collision object that will be returned by this function
let collision = {} // Which points do you want to check?
// "every", "some" or "center"?
switch (pointsToCheck) {
case 'center':
// `hit` will be true only if the center point is touching
sprite.collisionPoints = {
center: {
x: sprite.centerX,
y: sprite.centerY
}
}
collision.hit = Object.keys(sprite.collisionPoints).some(checkPoints)
break
case 'every':
// `hit` will be true if every point is touching
sprite.collisionPoints = this.getPoints(sprite)
collision.hit = Object.keys(sprite.collisionPoints).every(checkPoints)
break
case 'some':
// `hit` will be true only if some points are touching
sprite.collisionPoints = this.getPoints(sprite)
collision.hit = Object.keys(sprite.collisionPoints).some(checkPoints)
break
} // Return the collision object.
// `collision.hit` will be true if a collision is detected.
// `collision.index` tells you the map array index number where the
// collision occured
return collision
} // ### updateMap
/*
`updateMap` takes a map array and adds a sprite's grid index number (`gid`) to it.
It finds the sprite's new index position, and retuns the new map array.
You can use it to do very efficient collision detection in tile based game worlds.
`updateMap` arguments:
array, singleSpriteOrArrayOfSprites, worldObject
The `world` object (the 4th argument) has to have these properties:
`tileheight`, `tilewidth`, `widthInTiles`.
The sprite objects have to have have these properties:
`centerX`, `centerY`, `index`, `gid` (The number in the array that represpents the sprite)
Here's an example of how you could use `updateMap` in your game code like this:
blockLayer.data = updateMap(blockLayer.data, blockLayer.children, world);
The `blockLayer.data` array would now contain the new index position numbers of all the
child sprites on that layer.
*/
updateMap(mapArray, spritesToUpdate, world) {
// First create a map a new array filled with zeros.
// The new map array will be exactly the same size as the original
const newMapArray = mapArray.map(gid => {
gid = 0
return gid
}) // Is `spriteToUpdate` an array of sprites?
if (spritesToUpdate instanceof Array) {
// Get the index number of each sprite in the `spritesToUpdate` array
// and add the sprite's `gid` to the matching index on the map
const self = this
spritesToUpdate.forEach(sprite => {
// Find the new index number
sprite.index = self.getIndex(sprite.centerX, sprite.centerY, world.tilewidth, world.tileheight, world.widthInTiles) // Add the sprite's `gid` number to the correct index on the map
newMapArray[sprite.index] = sprite.gid
})
} // Is `spritesToUpdate` just a single sprite?
else {
const sprite = spritesToUpdate // Find the new index number
sprite.index = this.getIndex(sprite.centerX, sprite.centerY, world.tilewidth, world.tileheight, world.widthInTiles) // Add the sprite's `gid` number to the correct index on the map
newMapArray[sprite.index] = sprite.gid
} // Return the new map array to replace the previous one
return newMapArray
}
/*
###makeTiledWorld
`makeTiledWorld` is a quick and easy way to display a game world designed in
Tiled Editor. Supply `makeTiledWorld` with 1 **string** argument for map json:
1. A JSON file generated by Tiled Editor.
2. A source image that represents the tile set you used to create the Tiled Editor world.
```js
let world = makeTiledWorld("tiledEditorMapData.json");
```
(Note: `makeTiledWorld` looks for the JSON data file in Pixi's `loader.resources` object. So,
make sure you've loaded the JSON file using Pixi's `loader`.)
`makeTiledWorld` will return a Pixi `Container` that contains all the things in your Tiled Editor
map as Pixi sprites.
All the image tiles you create in Tiled Editor are automatically converted into Pixi sprites
for you by `makeTiledWorld`. You can access all of them using two methods: `getObject` (for
single sprites) and `getObjects` (with an "s") for multiple sprites. Let's find out how they work.
####world.getObject
Tile Editor lets you assign a "name" properties any object.
You can access any sprite by this name using the `getObject` method. `getObject` searches for and
returns a sprite in the `world` that has the same `name` property that you assigned
in Tiled Editor. Here's how to use `getObject` to look for an object called "alien"
in the Tiled map data and assign it to a variable called `alien`
```js
let alien = world.getObject("alien");
```
`alien` is now an ordinary Pixi sprite that you can control just like any other Pixi
sprite in your games.
#### Creating sprites from generic objects
Tiled Editor lets you create generic objects. These are objects that don't have images associated
with them. Generic objects are handy to use, because they let you create complex game objects inside
Tiled Editor, as pure data. You can then use that data your game code to build complex game objects.
For example, imagine that you want to create a complex animated walking sprite called "elf".
First, create the elf object in Tiled Editor as a generic object, but don't assign any image tiles
to it. Next, in your game code, create a new Pixi MovieClip called `elf` and give it any textures you want
to use for its animation states.
```js
//Create a new Pixi MovieClip sprite
let elf = new PIXI.MovieClip(elfSpriteTextures);
```
Then use the `x` and `y` data from the generic "elf" object you created in Tiled Editor to position the
`elf` sprite.
```js
elf.x = world.getObject("elf").x;
elf.y = world.getObject("elf").y;
```
This is a simple example, but you could make very complex data objects in Tiled Editor and
use them to build complex sprites in the same way.
####Accessing Tiled Editor layer groups
Tiled Editor lets you create **layer groups**. Each layer group you create
in Tiled Editor is automatically converted by `makeTiledWorld` into a Pixi `Container`
object. You can access those containers using `getObject` to extract the layer group
container.
Here's how you could extract the layer group called "objects" and add the
`elf` sprite to it.
```js
let objectsLayer = world.getObject("objects");
objectsLayer.addChild(elf);
```
If you want to add the sprite to a different world layer, you can do it like this:
```js
world.getObject("treeTops").addChild(elf);
```
If you want to access all the sprites in a specific Tiled Editor la