arcanumcube 0.1.1 → 0.1.3

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.
@@ -264,6 +264,42 @@ function getRandomTwistList(steps = 0) {
264
264
  }
265
265
  return list;
266
266
  }
267
+ var SIDE_FACES = Object.freeze([
268
+ [1, 2, 4, 5],
269
+ [2, 0, 5, 3],
270
+ [0, 1, 3, 4],
271
+ [2, 1, 5, 4],
272
+ [0, 2, 3, 5],
273
+ [1, 0, 4, 3]
274
+ ]);
275
+ function getInitialState(up, front) {
276
+ const down = (up + 3) % 6;
277
+ const side = SIDE_FACES[up];
278
+ const state = new Array(CUBE_SIZE * CUBE_SIZE).fill(up);
279
+ for (let i = 0; i < 4; i++) {
280
+ if (i == 2) {
281
+ const downs = [...Array(CUBE_SIZE * CUBE_SIZE)].fill(down);
282
+ state.push(...downs);
283
+ }
284
+ const sides = [...Array(CUBE_SIZE * CUBE_SIZE)].fill(side[(front + i) % 4]);
285
+ state.push(...sides);
286
+ }
287
+ return state;
288
+ }
289
+ function isSameArrays(arr1, arr2) {
290
+ if (arr1.length !== arr2.length) return false;
291
+ return !arr1.some((v, i) => v !== arr2[i]);
292
+ }
293
+ var GOAL_STATE_LIST = [];
294
+ for (let up = 0; up < 6; up++) {
295
+ for (let front = 0; front < 4; front++) {
296
+ GOAL_STATE_LIST.push(getInitialState(up, front));
297
+ }
298
+ }
299
+ Object.freeze(GOAL_STATE_LIST);
300
+ function MatchGoalState(state) {
301
+ return GOAL_STATE_LIST.some((s) => isSameArrays(state, s));
302
+ }
267
303
  var Cube = class {
268
304
  type;
269
305
  position;
@@ -408,6 +444,9 @@ var ArcanumCube = class {
408
444
  const list = this.getUndoList(steps);
409
445
  this.twist(list, true);
410
446
  }
447
+ isSolved() {
448
+ return MatchGoalState(this.getStickerColors());
449
+ }
411
450
  getHistory() {
412
451
  return this._history;
413
452
  }
@@ -651,7 +690,7 @@ var standardSkin = {
651
690
  cube: {
652
691
  material: () => {
653
692
  return new THREE.MeshStandardMaterial({
654
- color: 1118481,
693
+ color: 3092271,
655
694
  metalness: 0.8,
656
695
  roughness: 0.4
657
696
  });
@@ -1135,7 +1174,8 @@ var WebGLCube = class extends Cube {
1135
1174
  gap: 0.01,
1136
1175
  enableShadow: false,
1137
1176
  skin: DefaultSkin,
1138
- wireframe: false
1177
+ wireframe: false,
1178
+ wireframeColor: 32768
1139
1179
  };
1140
1180
  if (opts) {
1141
1181
  Object.assign(this._config, opts);
@@ -1179,7 +1219,7 @@ var WebGLCube = class extends Cube {
1179
1219
  }
1180
1220
  if (config.wireframe) {
1181
1221
  mat.wireframe = config.wireframe;
1182
- mat.color = new THREE3.Color("#080");
1222
+ mat.color = new THREE3.Color(config.wireframeColor);
1183
1223
  }
1184
1224
  const m = new THREE3.Mesh(geo, mat);
1185
1225
  m.name = this.type;
@@ -1220,7 +1260,7 @@ var WebGLCube = class extends Cube {
1220
1260
  }
1221
1261
  if (config.wireframe) {
1222
1262
  pmat.wireframe = config.wireframe;
1223
- pmat.color = new THREE3.Color("#080");
1263
+ pmat.color = new THREE3.Color(config.wireframeColor);
1224
1264
  }
1225
1265
  const pm = new THREE3.Mesh(pgeo, pmat);
1226
1266
  pm.name = CUBE.STICKER;
@@ -1310,7 +1350,9 @@ var WebGLArcanumCube = class extends ArcanumCube {
1310
1350
  /** tween group */
1311
1351
  _tweens;
1312
1352
  /** light at the center of cube */
1313
- _pointLights;
1353
+ _coreLights;
1354
+ /** status of locking the twist */
1355
+ _lockTwist;
1314
1356
  constructor(options) {
1315
1357
  super(options);
1316
1358
  this._config = {
@@ -1322,8 +1364,12 @@ var WebGLArcanumCube = class extends ArcanumCube {
1322
1364
  gap: 0.01,
1323
1365
  enableShadow: false,
1324
1366
  skin: DefaultSkin,
1325
- enableLight: false,
1326
- wireframe: false
1367
+ enableCoreLight: false,
1368
+ coreLightColor: 33023,
1369
+ coreLightIntensity: 30,
1370
+ wireframe: false,
1371
+ wireframeColor: 32768,
1372
+ twistOptions: {}
1327
1373
  };
1328
1374
  this._matrix = [];
1329
1375
  this._group = new THREE3.Group();
@@ -1331,7 +1377,8 @@ var WebGLArcanumCube = class extends ArcanumCube {
1331
1377
  this._cubeMap = {};
1332
1378
  this._cancelDragDeg = 15;
1333
1379
  this._tweens = new TWEEN.Group();
1334
- this._pointLights = [];
1380
+ this._coreLights = [];
1381
+ this._lockTwist = false;
1335
1382
  if (options) {
1336
1383
  Object.assign(this._config, options);
1337
1384
  }
@@ -1357,7 +1404,7 @@ var WebGLArcanumCube = class extends ArcanumCube {
1357
1404
  this._cubeMap = {};
1358
1405
  this._matrix = [];
1359
1406
  this._history = [];
1360
- this._pointLights = [];
1407
+ this._coreLights = [];
1361
1408
  const fixedGroups = new THREE3.Group();
1362
1409
  const config = this._config;
1363
1410
  const yarray = [];
@@ -1373,7 +1420,8 @@ var WebGLArcanumCube = class extends ArcanumCube {
1373
1420
  enableShadow: config.enableShadow,
1374
1421
  skin: config.skin,
1375
1422
  envMap: config.envMap,
1376
- wireframe: config.wireframe
1423
+ wireframe: config.wireframe,
1424
+ wireframeColor: config.wireframeColor
1377
1425
  });
1378
1426
  cube.init();
1379
1427
  xarray.push(cube);
@@ -1381,18 +1429,18 @@ var WebGLArcanumCube = class extends ArcanumCube {
1381
1429
  this._cubeObjectList.push(entityGroup);
1382
1430
  this._cubeMap[entityGroup.id] = cube;
1383
1431
  fixedGroups.add(cube.getGroup());
1384
- if (config.enableLight) {
1432
+ if (config.enableCoreLight) {
1385
1433
  if (x !== SIDE_MIN && y !== SIDE_MIN && z !== SIDE_MIN) {
1386
1434
  const light = new THREE3.PointLight(
1387
- config.wireframe ? 32768 : 33023,
1388
- 30,
1435
+ config.wireframe ? config.wireframeColor : config.coreLightColor,
1436
+ config.coreLightIntensity,
1389
1437
  20,
1390
1438
  0.1
1391
1439
  );
1392
1440
  light.position.x = (x - 1 / 2) * CUBE_SIDE_LEN * (1 + config.gap) * config.scale;
1393
1441
  light.position.y = (y - 1 / 2) * CUBE_SIDE_LEN * (1 + config.gap) * config.scale;
1394
1442
  light.position.z += (z - 1 / 2) * CUBE_SIDE_LEN * (1 + config.gap) * config.scale;
1395
- this._pointLights.push(light);
1443
+ this._coreLights.push(light);
1396
1444
  }
1397
1445
  }
1398
1446
  }
@@ -1402,8 +1450,8 @@ var WebGLArcanumCube = class extends ArcanumCube {
1402
1450
  }
1403
1451
  this._matrix = yarray;
1404
1452
  this._group.clear();
1405
- if (config.enableLight) {
1406
- this._pointLights.forEach((light) => {
1453
+ if (config.enableCoreLight) {
1454
+ this._coreLights.forEach((light) => {
1407
1455
  this._group.add(light);
1408
1456
  });
1409
1457
  }
@@ -1427,8 +1475,14 @@ var WebGLArcanumCube = class extends ArcanumCube {
1427
1475
  }
1428
1476
  await this.init();
1429
1477
  }
1478
+ lockTwist(flag) {
1479
+ this._lockTwist = flag;
1480
+ }
1481
+ isTwisting() {
1482
+ return this._tweens.getAll().length > 0;
1483
+ }
1430
1484
  reset(duration = 1800) {
1431
- if (this._tweens.getAll().length > 0) return;
1485
+ if (this._lockTwist || this.isTwisting()) return;
1432
1486
  if (this._selectedCube) this.deselectCube();
1433
1487
  if (this._selectedSticker) this.deselectSticker();
1434
1488
  this._twistGroup = void 0;
@@ -1605,8 +1659,8 @@ var WebGLArcanumCube = class extends ArcanumCube {
1605
1659
  }
1606
1660
  }
1607
1661
  this._group.clear();
1608
- if (this._config.enableLight) {
1609
- this._pointLights.forEach((light) => {
1662
+ if (this._config.enableCoreLight) {
1663
+ this._coreLights.forEach((light) => {
1610
1664
  this._group.add(light);
1611
1665
  });
1612
1666
  }
@@ -1615,7 +1669,7 @@ var WebGLArcanumCube = class extends ArcanumCube {
1615
1669
  return twistGroup;
1616
1670
  }
1617
1671
  dragTwist(twist, rad) {
1618
- if (this._tweens.getAll().length > 0) return;
1672
+ if (this._lockTwist || this.isTwisting()) return;
1619
1673
  if (!this._draggingTwist || this._draggingTwist.twist != twist) {
1620
1674
  this._draggingTwist = {
1621
1675
  twist,
@@ -1630,7 +1684,7 @@ var WebGLArcanumCube = class extends ArcanumCube {
1630
1684
  this._draggingTwist.group.quaternion.copy(q);
1631
1685
  }
1632
1686
  dragTwistEnd() {
1633
- if (this._tweens.getAll().length > 0) return;
1687
+ if (this._lockTwist || this.isTwisting()) return;
1634
1688
  if (this._draggingTwist) {
1635
1689
  const deg = this._draggingTwist.rad * 180 / Math.PI;
1636
1690
  if (deg > this._cancelDragDeg) {
@@ -1643,7 +1697,7 @@ var WebGLArcanumCube = class extends ArcanumCube {
1643
1697
  // twist randomly several steps
1644
1698
  scramble(steps = 0, duration = 3e3) {
1645
1699
  const list = getRandomTwistList(steps);
1646
- this.tweenTwist(list, false, duration);
1700
+ this.tweenTwist(list, false, duration, false);
1647
1701
  }
1648
1702
  undo(steps = 1, duration = 300) {
1649
1703
  const list = this.getUndoList(steps);
@@ -1651,17 +1705,25 @@ var WebGLArcanumCube = class extends ArcanumCube {
1651
1705
  }
1652
1706
  // twisting(複数回対応)
1653
1707
  // durationを0にするとTweenなしとなる
1654
- tweenTwist(twist, reverse = false, duration = 500, cancel = false) {
1655
- if (this._tweens.getAll().length > 0) return;
1708
+ tweenTwist(twist, reverse = false, duration = 500, cancel = false, options) {
1709
+ if (this._lockTwist || this.isTwisting()) return;
1710
+ options = { ...this._config.twistOptions, ...options };
1656
1711
  if (duration === 0) {
1657
1712
  if (Array.isArray(twist)) {
1658
1713
  if (twist.length == 0) return;
1659
- for (const c of twist) {
1714
+ options?.onStart && options.onStart(this);
1715
+ const len = twist.length;
1716
+ for (let i = 0; i < len; i++) {
1717
+ const c = twist[i];
1660
1718
  this._immediatelyTwist(c, reverse);
1719
+ options?.onTwisted && options.onTwisted(this, c, i + 1, len);
1661
1720
  }
1662
1721
  } else {
1722
+ options?.onStart && options.onStart(this);
1663
1723
  this._immediatelyTwist(twist, reverse);
1724
+ options?.onTwisted && options.onTwisted(this, twist, 1, 1);
1664
1725
  }
1726
+ options?.onComplete && options.onComplete(this);
1665
1727
  return;
1666
1728
  }
1667
1729
  let firstTween = void 0;
@@ -1669,8 +1731,15 @@ var WebGLArcanumCube = class extends ArcanumCube {
1669
1731
  if (Array.isArray(twist)) {
1670
1732
  if (twist.length == 0) return;
1671
1733
  const lap = duration / twist.length;
1672
- for (const c of twist) {
1673
- const t = this._tweenTwist(c, reverse, lap, cancel);
1734
+ const len = twist.length;
1735
+ for (let i = 0; i < len; i++) {
1736
+ const c = twist[i];
1737
+ const opts = {};
1738
+ const ontwisted = options?.onTwisted;
1739
+ if (ontwisted)
1740
+ opts.onTwisted = (self, twist2, n1, n2) => ontwisted(this, twist2, i + 1, len);
1741
+ if (i === len - 1 && options?.onComplete) opts.onComplete = options.onComplete;
1742
+ const t = this._tweenTwist(c, reverse, lap, cancel, opts);
1674
1743
  this._tweens.add(t);
1675
1744
  if (!tween) {
1676
1745
  firstTween = tween = t;
@@ -1680,10 +1749,11 @@ var WebGLArcanumCube = class extends ArcanumCube {
1680
1749
  }
1681
1750
  }
1682
1751
  } else {
1683
- firstTween = this._tweenTwist(twist, reverse, duration, cancel);
1752
+ firstTween = this._tweenTwist(twist, reverse, duration, cancel, options);
1684
1753
  this._tweens.add(firstTween);
1685
1754
  }
1686
1755
  if (firstTween) {
1756
+ options?.onStart && options.onStart(this);
1687
1757
  firstTween.start();
1688
1758
  }
1689
1759
  }
@@ -1705,7 +1775,7 @@ var WebGLArcanumCube = class extends ArcanumCube {
1705
1775
  super.twist(twist, reverse);
1706
1776
  }
1707
1777
  // twist with tween
1708
- _tweenTwist(twist, reverse, duration, cancel) {
1778
+ _tweenTwist(twist, reverse, duration, cancel, options = {}) {
1709
1779
  let qa;
1710
1780
  if (this._draggingTwist) {
1711
1781
  this._twistGroup = this._draggingTwist.group;
@@ -1742,12 +1812,30 @@ var WebGLArcanumCube = class extends ArcanumCube {
1742
1812
  super.twist(twist, reverse);
1743
1813
  }
1744
1814
  this._tweens.remove(tween);
1815
+ options.onTwisted && options.onTwisted(this, twist, 1, 1);
1816
+ options.onComplete && options.onComplete(this);
1817
+ if (options.onSolved && this.isSolved()) {
1818
+ options.onSolved(this);
1819
+ }
1745
1820
  });
1746
1821
  return tween;
1747
1822
  }
1748
1823
  updateTweens() {
1749
1824
  this._tweens.update();
1750
1825
  }
1826
+ // set color of core lights
1827
+ setCoreLightColor(color) {
1828
+ const c = new THREE3.Color(color);
1829
+ this._coreLights.forEach((l) => {
1830
+ l.color = c;
1831
+ });
1832
+ }
1833
+ // change intensity of core lights
1834
+ setCoreLightIntensity(intensity) {
1835
+ this._coreLights.forEach((l) => {
1836
+ l.intensity = intensity;
1837
+ });
1838
+ }
1751
1839
  };
1752
1840
  export {
1753
1841
  ArcanumCube,
@@ -1763,6 +1851,7 @@ export {
1763
1851
  FACE,
1764
1852
  GetSkinByName,
1765
1853
  GetSkinNameList,
1854
+ MatchGoalState,
1766
1855
  SIDE_MAX,
1767
1856
  SIDE_MIDDLE,
1768
1857
  SIDE_MIN,