@gamelearn/arcade-components 0.25.38 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -68,6 +68,7 @@ var ConversationProViewer = function ConversationProViewer(_ref) {
68
68
  finished = _useState4[0],
69
69
  setFinished = _useState4[1];
70
70
 
71
+ var emitEventRef = (0, _react.useRef)(emitEvent);
71
72
  var backgroundImage = (background === null || background === void 0 ? void 0 : (_background$img = background.img) === null || _background$img === void 0 ? void 0 : _background$img.url) || null;
72
73
  var currentLineData = lines[currentLine];
73
74
  var voiceOverSlots = [];
@@ -75,11 +76,11 @@ var ConversationProViewer = function ConversationProViewer(_ref) {
75
76
  decision = currentLineData.decision;
76
77
  var isFlex = currentLineData.flex;
77
78
  var translate = (0, _react.useCallback)(function (id) {
78
- return emitEvent({
79
+ return emitEventRef.current({
79
80
  type: 'translate',
80
81
  payload: id
81
82
  });
82
- }, [emitEvent]);
83
+ }, []);
83
84
  var getCurrentMessage = (0, _react.useCallback)(function () {
84
85
  if (voiceOver) {
85
86
  return currentLineData;
@@ -140,7 +141,7 @@ var ConversationProViewer = function ConversationProViewer(_ref) {
140
141
  }
141
142
 
142
143
  _context.next = 5;
143
- return emitEvent({
144
+ return emitEventRef.current({
144
145
  type: 'playSpeech',
145
146
  payload: payload
146
147
  });
@@ -155,7 +156,7 @@ var ConversationProViewer = function ConversationProViewer(_ref) {
155
156
  }
156
157
  }
157
158
  }, _callee);
158
- })), [currentLineData.voice, getVoice, currentMessage, emitEvent, playSound]); // Recupera la antigua linea de la conversacion para conservar los personajes en voice-over
159
+ })), [currentLineData.voice, currentMessage.text, getVoice, playSound]); // Recupera la antigua linea de la conversacion para conservar los personajes en voice-over
159
160
 
160
161
  var leftWithSlots = lines.slice(0, currentLine + 1).reverse().find(function (line) {
161
162
  return line.slots;
@@ -241,7 +242,7 @@ var ConversationProViewer = function ConversationProViewer(_ref) {
241
242
  setCurrentLine(line + 1);
242
243
  } else {
243
244
  setFinished(true);
244
- emitEvent({
245
+ emitEventRef.current({
245
246
  type: 'success'
246
247
  });
247
248
  }
@@ -23,6 +23,8 @@ var _helpers = require("../../../helpers");
23
23
 
24
24
  var _HtmlPro = _interopRequireDefault(require("../../../helpers/HtmlPro"));
25
25
 
26
+ var _deviceDetection = require("../../../helpers/deviceDetection");
27
+
26
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
29
 
28
30
  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
@@ -37,6 +39,10 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
37
39
 
38
40
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
39
41
 
42
+ 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); } }
43
+
44
+ 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); }); }; }
45
+
40
46
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
41
47
 
42
48
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -49,9 +55,12 @@ function _iterableToArrayLimit(arr, i) { var _i = arr && (typeof Symbol !== "und
49
55
 
50
56
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
51
57
 
52
- var crossFadeDuration = 0.5;
58
+ var isMobile = (0, _deviceDetection.DeviceDetection)();
59
+ var crossFadeDuration = 0.5; // Mobile always zoom in?
53
60
 
54
61
  var DialogComponent = function DialogComponent(_ref) {
62
+ var _currentLine$audio3, _currentLine$audio4;
63
+
55
64
  var emitEvent = _ref.emitEvent,
56
65
  lines = _ref.lines,
57
66
  actors = _ref.actors,
@@ -82,6 +91,8 @@ var DialogComponent = function DialogComponent(_ref) {
82
91
  currentMessage = _useState6[0],
83
92
  setCurrentMessage = _useState6[1];
84
93
 
94
+ var emitRef = (0, _react.useRef)(emitEvent);
95
+
85
96
  var _useThree = (0, _fiber.useThree)(function (state) {
86
97
  return [state.camera, state.scene];
87
98
  }),
@@ -93,6 +104,11 @@ var DialogComponent = function DialogComponent(_ref) {
93
104
  return new _GLTFLoader.GLTFLoader();
94
105
  }, []);
95
106
  var defaultCamera = (0, _react.useRef)(camera.clone());
107
+
108
+ var _soundActions = _slicedToArray(soundActions, 2),
109
+ playSound = _soundActions[0],
110
+ stop = _soundActions[1];
111
+
96
112
  var messagePosition = (0, _react.useRef)({
97
113
  pos: [0, 0, 0],
98
114
  child: null
@@ -105,11 +121,66 @@ var DialogComponent = function DialogComponent(_ref) {
105
121
  var lastLine = lines[line - 1];
106
122
  var lastSet = (0, _react.useRef)(false);
107
123
  var translate = (0, _react.useCallback)(function (id) {
108
- return emitEvent({
124
+ return emitRef.current({
109
125
  type: 'translate',
110
126
  payload: id
111
127
  });
112
- }, [emitEvent]);
128
+ }, []);
129
+ var getVoice = (0, _react.useCallback)(function (_ref2) {
130
+ var id = _ref2.id,
131
+ _ref2$type = _ref2.type,
132
+ type = _ref2$type === void 0 ? 'base' : _ref2$type;
133
+ var usedVoices = translate('usedVoices');
134
+ var gameChars = translate('gameCharacters');
135
+
136
+ if (type === 'base') {
137
+ return usedVoices === null || usedVoices === void 0 ? void 0 : usedVoices[id];
138
+ } else if (type === 'character') {
139
+ var _gameChars$id;
140
+
141
+ return gameChars === null || gameChars === void 0 ? void 0 : (_gameChars$id = gameChars[id]) === null || _gameChars$id === void 0 ? void 0 : _gameChars$id.voiceId;
142
+ }
143
+
144
+ return null;
145
+ }, [translate]);
146
+ var playSpeech = (0, _react.useCallback)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
147
+ var payload, speechUrl;
148
+ return regeneratorRuntime.wrap(function _callee$(_context) {
149
+ while (1) {
150
+ switch (_context.prev = _context.next) {
151
+ case 0:
152
+ if (!currentLine.voice.id) {
153
+ _context.next = 7;
154
+ break;
155
+ }
156
+
157
+ payload = {
158
+ voiceId: getVoice(currentLine.voice),
159
+ text: currentMessage.text
160
+ };
161
+
162
+ if (!(payload.voiceId && payload.text)) {
163
+ _context.next = 7;
164
+ break;
165
+ }
166
+
167
+ _context.next = 5;
168
+ return emitRef.current({
169
+ type: 'playSpeech',
170
+ payload: payload
171
+ });
172
+
173
+ case 5:
174
+ speechUrl = _context.sent;
175
+ playSound(speechUrl);
176
+
177
+ case 7:
178
+ case "end":
179
+ return _context.stop();
180
+ }
181
+ }
182
+ }, _callee);
183
+ })), [currentLine.voice, getVoice, currentMessage.text, playSound]);
113
184
  var getCurrentVisible = (0, _react.useCallback)(function (uid) {
114
185
  var object;
115
186
  scene.traverseVisible(function (node) {
@@ -201,11 +272,13 @@ var DialogComponent = function DialogComponent(_ref) {
201
272
  }, [zoomInActor]);
202
273
  var backLine = (0, _react.useCallback)(function () {
203
274
  if (lastLine && !lastLine.decision) {
275
+ setCurrentMessage({});
204
276
  changeLine(line - 1);
205
277
  }
206
278
  }, [lastLine, line]);
207
279
  var nextLine = (0, _react.useCallback)(function () {
208
280
  if (line < lines.length - 1) {
281
+ setCurrentMessage({});
209
282
  changeLine(line + 1);
210
283
  } else {
211
284
  resetAnims(true);
@@ -222,8 +295,6 @@ var DialogComponent = function DialogComponent(_ref) {
222
295
  }, [camera, currentLine.decision, emitEvent, line, lines.length, resetAnims, resetCameraView]); // Position bubble over talking character/and switch emotions
223
296
 
224
297
  (0, _react.useEffect)(function () {
225
- setCurrentMessage({});
226
-
227
298
  if (currentLine.decision) {
228
299
  resetCameraView();
229
300
  return;
@@ -275,38 +346,42 @@ var DialogComponent = function DialogComponent(_ref) {
275
346
  if (startAction !== action) {
276
347
  mixer.stopAllAction();
277
348
  (0, _helpers.executeCrossFade)(startAction, action, crossFadeDuration);
278
- }
279
-
280
- if (talkingCharacter.uid === element.uid) {
281
- var head = element.getObjectByName('Head_M');
282
- var box = new _three.Box3().setFromObject(element);
283
- var bodySize = box.getSize(new _three.Vector3());
284
- var headPos = head.getWorldPosition(new _three.Vector3());
285
- headPos.add((0, _helpers.vectorTranslateX)(bodySize.x / 8, camera));
286
- headPos.add((0, _helpers.vectorTranslateY)(bodySize.y / 6, camera));
287
- messagePosition.current = {
288
- pos: headPos.toArray(),
289
- child: head,
290
- parent: element
291
- };
292
-
293
- if (zoomInActor) {
294
- animationStart.current = true;
349
+ } // Ugly timeout to wait an animation transition
350
+
351
+
352
+ setTimeout(function () {
353
+ if (talkingCharacter.uid === element.uid) {
354
+ var head = element.getObjectByName('Head_M');
355
+ var target = new _three.Object3D();
356
+ head.getWorldPosition(target.position);
357
+ target.applyQuaternion(camera.quaternion);
358
+ target.translateX(1.25);
359
+ target.translateY(3);
360
+ messagePosition.current = {
361
+ pos: target.position.toArray(),
362
+ child: head,
363
+ parent: element
364
+ };
365
+
366
+ if (zoomInActor) {
367
+ animationStart.current = true;
368
+ } // Ugly timeout to wait for zoom in ( maybe use tween (?))
369
+
370
+
371
+ setTimeout(function () {
372
+ var _talkingCharacter$cha;
373
+
374
+ setCurrentMessage({
375
+ text: talkingCharacter.text,
376
+ name: talkingCharacter.alias || ((_talkingCharacter$cha = talkingCharacter.character) === null || _talkingCharacter$cha === void 0 ? void 0 : _talkingCharacter$cha.name),
377
+ left: true,
378
+ inScene: true,
379
+ thinkful: talkingCharacter.emotion === 'thinkful',
380
+ avatar: ''
381
+ });
382
+ }, zoomInActor ? 1200 : 0);
295
383
  }
296
-
297
- setTimeout(function () {
298
- var _talkingCharacter$cha;
299
-
300
- setCurrentMessage({
301
- text: talkingCharacter.text,
302
- name: talkingCharacter.alias || ((_talkingCharacter$cha = talkingCharacter.character) === null || _talkingCharacter$cha === void 0 ? void 0 : _talkingCharacter$cha.name),
303
- left: true,
304
- inScene: true,
305
- thinkful: talkingCharacter.emotion === 'thinkful',
306
- avatar: ''
307
- });
308
- }, zoomInActor ? 700 : 0);
309
- }
384
+ }, 1000 * crossFadeDuration);
310
385
  });
311
386
  } else {
312
387
  var _talkingCharacter$cha2;
@@ -323,7 +398,23 @@ var DialogComponent = function DialogComponent(_ref) {
323
398
  }
324
399
  }
325
400
  }
326
- }, [actors, camera, currentLine, zoomInActor, getCurrentVisible, translate, getAvatarURL, loadEmotion, resetAnims, resetCameraView]); // Autoplay logic
401
+ }, [actors, camera, currentLine, zoomInActor, getCurrentVisible, translate, getAvatarURL, loadEmotion, resetAnims, resetCameraView]); // Sound logic
402
+
403
+ (0, _react.useEffect)(function () {
404
+ var _currentLine$audio;
405
+
406
+ if ((currentLine === null || currentLine === void 0 ? void 0 : currentLine.audioType) === 'voice' && !lastSet.current) {
407
+ playSpeech();
408
+ } else if (currentLine !== null && currentLine !== void 0 && (_currentLine$audio = currentLine.audio) !== null && _currentLine$audio !== void 0 && _currentLine$audio.url) {
409
+ var _currentLine$audio2;
410
+
411
+ playSound(currentLine === null || currentLine === void 0 ? void 0 : (_currentLine$audio2 = currentLine.audio) === null || _currentLine$audio2 === void 0 ? void 0 : _currentLine$audio2.url);
412
+ }
413
+
414
+ return function () {
415
+ stop();
416
+ };
417
+ }, [currentLine === null || currentLine === void 0 ? void 0 : (_currentLine$audio3 = currentLine.audio) === null || _currentLine$audio3 === void 0 ? void 0 : _currentLine$audio3.url, currentLine === null || currentLine === void 0 ? void 0 : currentLine.audioType, currentMessage, lines, playSound, playSpeech, stop]); // Autoplay logic
327
418
 
328
419
  (0, _react.useEffect)(function () {
329
420
  var timeout;
@@ -332,35 +423,41 @@ var DialogComponent = function DialogComponent(_ref) {
332
423
  if (line >= lines.length - 1 && automatic) {
333
424
  setAutomatic(false);
334
425
  } else if (automatic && currentMessage.text && !currentLine.decision) {
426
+ var time = 70 * (currentMessage === null || currentMessage === void 0 ? void 0 : currentMessage.text.length);
335
427
  timeout = setTimeout(function () {
336
428
  nextLine();
337
- }, 70 * currentMessage.text.length);
429
+ }, time < 1400 ? 1400 : time);
338
430
  }
339
431
  }
340
432
 
341
433
  return function () {
342
434
  if (timeout) clearTimeout(timeout);
343
435
  };
344
- }, [currentMessage, nextLine, autoPlay, lines.length, automatic, line, currentLine.decision]); //Positioning logic
436
+ }, [currentMessage, nextLine, autoPlay, lines.length, automatic, line, currentLine.decision, currentLine === null || currentLine === void 0 ? void 0 : currentLine.audioType, currentLine === null || currentLine === void 0 ? void 0 : (_currentLine$audio4 = currentLine.audio) === null || _currentLine$audio4 === void 0 ? void 0 : _currentLine$audio4.url]); //Positioning logic
345
437
 
346
- (0, _react.useEffect)(function () {
438
+ var checkBubblePosition = (0, _react.useCallback)(function () {
347
439
  if (bubbleRef.current && currentLine.slots) {
348
440
  var talkingCharacter = currentLine.slots.find(function (slot) {
349
441
  return slot.talking;
350
442
  });
351
443
  var bubbleRect = bubbleRef.current.getBoundingClientRect();
352
- var x = bubbleRect.x + bubbleRect.width / 2;
353
- var y = bubbleRect.y - bubbleRect.height / 2;
444
+ var x = bubbleRect.x + bubbleRect.width - 75;
445
+ var y = bubbleRect.y - bubbleRect.height;
354
446
 
355
- if ((x < 0 || x > window.innerWidth || y < 0 || y > window.innerHeight) && currentMessage.inScene) {
447
+ if ((x < 0 || x > window.innerWidth) && currentMessage.inScene) {
356
448
  var child = messagePosition.current.child;
357
449
 
358
450
  if (child) {
359
- groupRef.current.position.fromArray(messagePosition.current.pos);
360
- var completeOutside = x > window.innerWidth + bubbleRect.width || y < -bubbleRect.height || y > window.innerHeight || x < -bubbleRect.width;
361
- var distanceX = bubbleRect.width + bubbleRect.width / 4;
362
- bubbleRef.current.parentNode.style.transform = "translate3d(-".concat(distanceX, "px, -").concat(bubbleRect.height / 2, "px, 0px)");
451
+ var target = new _three.Object3D();
452
+ child.getWorldPosition(target.position);
453
+ target.applyQuaternion(camera.quaternion);
454
+ groupRef.current.position.copy(target.position);
363
455
  groupRef.current.updateMatrixWorld();
456
+ var divider = isMobile ? 10 : 6;
457
+ var distanceX = bubbleRect.width + bubbleRect.width / divider;
458
+ bubbleRef.current.parentNode.style.transform = "translate3d(-".concat(distanceX, "px, -").concat(bubbleRect.height, "px, 0px)");
459
+ bubbleRect = bubbleRef.current.getBoundingClientRect();
460
+ var completeOutside = x > window.innerWidth + bubbleRect.width || y < -bubbleRect.height || y > window.innerHeight || x < -bubbleRect.width;
364
461
 
365
462
  if (completeOutside) {
366
463
  bubbleRef.current.parentNode.style.transform = '';
@@ -380,10 +477,13 @@ var DialogComponent = function DialogComponent(_ref) {
380
477
  bubbleRef.current.parentNode.style.transform = '';
381
478
  }
382
479
  }
383
- }, [currentMessage.text, camera, currentLine.slots, getAvatarURL, currentMessage.inScene]); // Animate Camera
480
+ }, [camera.quaternion, currentLine.slots, currentMessage.inScene, getAvatarURL]);
481
+ (0, _react.useEffect)(function () {
482
+ checkBubblePosition();
483
+ }, [checkBubblePosition]); // Animate Camera
384
484
 
385
- (0, _fiber.useFrame)(function (_ref2, delta) {
386
- var camera = _ref2.camera;
485
+ (0, _fiber.useFrame)(function (_ref4, delta) {
486
+ var camera = _ref4.camera;
387
487
 
388
488
  if (animationStart.current) {
389
489
  var targetPosition = messagePosition.current.child ? messagePosition.current.pos : defaultCamera.current.position.toArray();
@@ -394,7 +494,7 @@ var DialogComponent = function DialogComponent(_ref) {
394
494
  animationTarget.current.applyQuaternion(camera.quaternion);
395
495
  }
396
496
 
397
- animationTarget.current.translateX(12);
497
+ animationTarget.current.translateX(13);
398
498
  animationTarget.current.translateY(-5);
399
499
  animationTarget.current.translateZ(30);
400
500
  }
@@ -405,6 +505,8 @@ var DialogComponent = function DialogComponent(_ref) {
405
505
  if (lastSet.current || !messagePosition.current.child) {
406
506
  camera.copy(defaultCamera.current);
407
507
  }
508
+
509
+ checkBubblePosition();
408
510
  } else {
409
511
  camera.position.lerp(animationTarget.current.position, delta * 2.5);
410
512
  }
@@ -417,7 +519,7 @@ var DialogComponent = function DialogComponent(_ref) {
417
519
  disableBackground: true
418
520
  },
419
521
  onFinish: nextLine,
420
- emitEvent: emitEvent,
522
+ emitEvent: emitRef.current,
421
523
  soundActions: soundActions
422
524
  });
423
525
 
@@ -101,7 +101,6 @@ var VideoVisor = function VideoVisor(_ref) {
101
101
  backgroundRepeat: "no-repeat",
102
102
  backgroundPosition: "center center"
103
103
  };
104
- console.log('re-render control');
105
104
  return /*#__PURE__*/_react.default.createElement("div", {
106
105
  className: "video media-visor",
107
106
  style: isAudio ? configForAudio : null
@@ -76,8 +76,7 @@ var getRecursiveJSX = function getRecursiveJSX(jsx) {
76
76
  return /*#__PURE__*/_react.default.createElement(Type, _extends({
77
77
  key: "".concat(jsx.type, "_").concat(i)
78
78
  }, props), getRecursiveJSX(children, i, slide, onClick, onHover));
79
- } // console.log('🐮', jsx);
80
-
79
+ }
81
80
 
82
81
  if (((_jsx$props3 = jsx.props) === null || _jsx$props3 === void 0 ? void 0 : _jsx$props3.children) !== null) {
83
82
  return jsx.split(' ').filter(function (word) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gamelearn/arcade-components",
3
3
  "license": "Gamelearn",
4
- "version": "0.25.38",
4
+ "version": "0.26.1",
5
5
  "main": "dist/index.js",
6
6
  "files": [
7
7
  "dist",