react-three-nephilim
Version:
3D SPA engine based on React and Three.js
161 lines (160 loc) • 7.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Action = void 0;
var _tween = _interopRequireDefault(require("@tweenjs/tween.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var Action = exports.Action = _createClass(function Action() {
var _this = this;
_classCallCheck(this, Action);
_defineProperty(this, "begin", function () {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
decorations = _ref.decorations,
easing = _ref.easing,
duration = _ref.duration,
affectAll = _ref.affectAll,
enforce = _ref.enforce,
callback = _ref.callback;
if (!_this.manager.isActionsEnabled) return false;
var from = [];
var to = [];
var ad = {};
if (decorations) {
for (var d in decorations) {
ad[d] = {
decoration: _this.decorations[d].decoration,
state: decorations[d].state
};
}
if (affectAll) {
for (var _d in _this.decorations) {
if (!decorations[_d]) ad[_d] = _objectSpread({}, _this.decorations[_d]);
}
;
}
} else {
ad = _this.decorations;
}
for (var _d2 in ad) {
var _deco$audio;
var deco = ad[_d2].decoration;
var state = ad[_d2].state;
from.push(deco.visual);
to.push(deco.calculateVisualState(state));
if (state.uniforms) {
for (var u in state.uniforms) {
from.push(deco.visual.material.uniforms[u]);
to.push(state.uniforms[u]);
}
;
}
if (state.audio && (_deco$audio = deco.audio) !== null && _deco$audio !== void 0 && _deco$audio[state.audio]) {
deco.audio[state.audio].play();
}
}
;
if (enforce) _this.manager.isActionsEnabled = false;
_this.animate({
from: from,
to: to,
easing: easing,
duration: duration,
callback: callback
});
});
_defineProperty(this, "animate", function (_ref2) {
var from = _ref2.from,
to = _ref2.to,
_ref2$easing = _ref2.easing,
easing = _ref2$easing === void 0 ? _tween["default"].Easing.Exponential.Out : _ref2$easing,
_ref2$duration = _ref2.duration,
duration = _ref2$duration === void 0 ? 500 : _ref2$duration,
callback = _ref2.callback;
var tweenPromises = [];
from.forEach(function (f, i) {
if (from[i].position && to[i].position) {
(function (from, to) {
tweenPromises.push(new Promise(function (resolve) {
new _tween["default"].Tween(from.position).to({
x: to.position.x,
y: to.position.y,
z: to.position.z
}, duration).easing(easing).start().onComplete(function () {
return resolve();
});
}));
})(from[i], to[i]);
}
if (from[i].rotation && to[i].rotation) {
(function (from, to) {
tweenPromises.push(new Promise(function (resolve) {
new _tween["default"].Tween(from.rotation).to({
x: to.rotation.x,
y: to.rotation.y,
z: to.rotation.z
}, duration).easing(easing).start().onComplete(function () {
return resolve();
});
}));
})(from[i], to[i]);
}
if (from[i].scale && to[i].scale) {
(function (from, to) {
tweenPromises.push(new Promise(function (resolve) {
new _tween["default"].Tween(from.scale).to({
x: to.scale.x,
y: to.scale.y,
z: to.scale.z
}, duration).easing(easing).start().onComplete(function () {
return resolve();
});
}));
})(from[i], to[i]);
}
if (from[i].value !== undefined) {
(function (from, to) {
tweenPromises.push(new Promise(function (resolve) {
new _tween["default"].Tween(from).to({
value: to.value
}, duration).easing(easing).start().onComplete(function () {
return resolve();
});
}));
})(from[i], to[i]);
}
});
Promise.all(tweenPromises).then(function () {
return _this.onActionComplete({
callback: callback
});
});
});
_defineProperty(this, "onActionComplete", function (_ref3) {
var callback = _ref3.callback;
_this.manager.isActionsEnabled = true;
if (callback) callback();
});
_defineProperty(this, "getDecorations", function () {
return _this.decorations;
});
_defineProperty(this, "addDecoration", function (decoration, state) {
return _this.decorations[decoration.id] = {
decoration: decoration,
state: state
};
});
_defineProperty(this, "removeDecoration", function (id) {
return delete _this.decorations[id];
});
this.decorations = {};
});