phaser-jsx
Version:
Use JSX in Phaser.
161 lines • 7.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addGameObject = addGameObject;
var phaser_1 = __importDefault(require("phaser"));
var components_1 = require("../components");
var GameObjects = __importStar(require("../components/GameObjects"));
var element_1 = require("../element");
var props_1 = require("./props");
var ref_1 = require("./ref");
/**
* Instantiates Phaser Game Object in the Scene.
*
* @param element - Element that you want to add.
* @param scene - Phaser Scene.
* @param parent - Phaser Container or Layer.
*/
function addGameObject(element, scene, parent) {
if (Array.isArray(element)) {
element.forEach(function (current) { return addGameObject(current, scene, parent); });
return;
}
if (!(0, element_1.isValidElement)(element)) {
return;
}
var _a = element.props, children = _a.children, color = _a.color, frame = _a.frame, key = _a.key, // eslint-disable-line @typescript-eslint/no-unused-vars
points = _a.points, ref = _a.ref, shader = _a.shader, style = _a.style, texture = _a.texture, props = __rest(_a, ["children", "color", "frame", "key", "points", "ref", "shader", "style", "texture"]);
var gameObject;
switch (true) {
case element.type === components_1.Fragment:
/* istanbul ignore else */
if (children) {
toArray(children).forEach(function (child) {
addGameObject(child, scene, parent);
});
}
return;
case element.type === phaser_1.default.GameObjects.BitmapText:
case element.type === phaser_1.default.GameObjects.DynamicBitmapText:
gameObject = new element.type(scene, props.x, props.y, props.font);
break;
case element.type === phaser_1.default.GameObjects.Bob:
gameObject = new element.type(scene, props.x, props.y, frame, props.visible);
break;
case element.type === phaser_1.default.GameObjects.Container:
case element.type === phaser_1.default.GameObjects.Layer:
gameObject = new element.type(scene);
if (children) {
toArray(children).forEach(function (child) {
addGameObject(child, scene, gameObject);
});
}
break;
case element.type === phaser_1.default.GameObjects.GameObject:
gameObject = new element.type(scene, props.type);
break;
case element.type === phaser_1.default.GameObjects.Image:
case element.type === phaser_1.default.GameObjects.Sprite:
case element.type === phaser_1.default.GameObjects.NineSlice:
gameObject = new element.type(scene, props.x, props.y, texture, frame);
break;
case element.type === phaser_1.default.GameObjects.Light:
gameObject = new element.type(scene, props.x, props.y, props.radius, color === null || color === void 0 ? void 0 : color.r, color === null || color === void 0 ? void 0 : color.g, color === null || color === void 0 ? void 0 : color.b, props.intensity);
break;
case element.type === phaser_1.default.GameObjects.PathFollower:
gameObject = new element.type(scene, props.path, props.x, props.y, texture, frame);
break;
case element.type === phaser_1.default.GameObjects.Plane:
gameObject = new element.type(scene, props.x, props.y, texture, frame, props.width, props.height, props.isTiled);
break;
case element.type === phaser_1.default.GameObjects.PointLight:
gameObject = new element.type(scene, props.x, props.y, color);
break;
case element.type === phaser_1.default.GameObjects.Rectangle:
case element.type === phaser_1.default.GameObjects.Zone:
gameObject = new element.type(scene, props.x, props.y);
break;
case element.type === phaser_1.default.GameObjects.Rope:
gameObject = new element.type(scene, props.x, props.y, texture, frame, points);
break;
case element.type === phaser_1.default.GameObjects.Shader:
gameObject = new element.type(scene, shader);
break;
case element.type === phaser_1.default.GameObjects.Text:
gameObject = new element.type(scene, props.x, props.y, props.text, style);
break;
case element.type === phaser_1.default.GameObjects.TileSprite:
gameObject = new element.type(scene, props.x, props.y, props.width, props.height, texture, frame);
break;
case element.type === phaser_1.default.GameObjects.Video:
gameObject = new element.type(scene, props.x, props.y, props.cacheKey);
break;
// Phaser component
case gameObjects.indexOf(element.type) !== -1:
gameObject = new element.type(scene);
break;
// composite component (class/function)
default:
addGameObject(new element.type(element.props), scene, parent);
return;
}
(0, props_1.setProps)(gameObject, props, scene);
(0, ref_1.attachRef)(gameObject, ref);
if (typeof (parent === null || parent === void 0 ? void 0 : parent.add) === 'function') {
parent.add(gameObject);
}
else {
scene.add.existing(gameObject);
}
}
var gameObjects = Object.keys(GameObjects).map(function (key) { return GameObjects[key]; });
function toArray(item) {
return Array.isArray(item) ? item : [item];
}
//# sourceMappingURL=gameobject.js.map