@houstonp/rubiks-cube 1.0.3 → 1.1.0

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.
package/README.md CHANGED
@@ -76,6 +76,17 @@ cube.addEventListener("state", (e) => {
76
76
 
77
77
  The Rubiks cube web component listens for custom events to perform twists, rotations and camera changes. As per convention, the starting rotation has green facing forward, white facing up and red facing to the right.
78
78
 
79
+ ### Reset event
80
+
81
+ The rubiks-cube element listens for the `reset` custom event and resets the cube to its initial state.
82
+
83
+ #### Example
84
+
85
+ ```js
86
+ const cube = document.querySelector("rubiks-cube");
87
+ cube.dispatchEvent(new CustomEvent("reset"));
88
+ ```
89
+
79
90
  ### Camera events
80
91
 
81
92
  The rubiks-cube element listens for the `camera` custom event and moves the camera to the specified position.
package/index.js CHANGED
@@ -108,12 +108,21 @@ class RubiksCube extends HTMLElement {
108
108
  cameraAnimationGroup.update();
109
109
  controls.update();
110
110
  animationQueue.update();
111
- const animationGroup = animationQueue.getAnimationGroup();
112
- if (animationGroup !== undefined) scene.add(animationGroup);
113
- if (animationQueue.finished()) sendState();
111
+ if (animationQueue.currentAnimation) {
112
+ scene.add(animationQueue.getAnimationGroup());
113
+ }
114
+ if (animationQueue.finished()) {
115
+ sendState();
116
+ scene.remove(animationQueue.getAnimationGroup());
117
+ }
114
118
  renderer.render(scene, camera);
115
119
  }
116
120
 
121
+ this.addEventListener("reset", () => {
122
+ animationQueue.clear();
123
+ cube.reset();
124
+ });
125
+
117
126
  // add event listeners for rotation and camera controls
118
127
  this.addEventListener("rotate", (e) => {
119
128
  const action = getRotationDetails(e.detail.action);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@houstonp/rubiks-cube",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "Rubiks Cube Web Component built with threejs",
5
5
  "main": "index.js",
6
6
  "author": "Houston Pearse",
package/src/animation.js CHANGED
@@ -12,6 +12,15 @@ export class AnimationQueue {
12
12
  this.factor = factor;
13
13
  }
14
14
 
15
+ clear() {
16
+ this.queue = [];
17
+ if (this.currentAnimation) {
18
+ this.currentAnimation.fastforward = true;
19
+ this.currentAnimation.update();
20
+ this.currentAnimation = undefined;
21
+ }
22
+ }
23
+
15
24
  /**
16
25
  * @param {Animation} animation
17
26
  */
@@ -66,8 +75,7 @@ export class AnimationQueue {
66
75
  * @returns {THREE.Group | undefined}
67
76
  */
68
77
  getAnimationGroup() {
69
- if (this.currentAnimation === undefined) return undefined;
70
- return this.currentAnimation.getGroup();
78
+ return this.currentAnimation?.getGroup();
71
79
  }
72
80
  }
73
81
 
@@ -106,7 +114,6 @@ export class Animation {
106
114
  this._lastUpdate = Date.now();
107
115
  const layerObjects = this._cube.getRotationLayer(this._axis, this._layers);
108
116
  this._layerGroup.add(...layerObjects);
109
- this._cube.group.remove(...layerObjects);
110
117
  }
111
118
 
112
119
  teardown() {
@@ -122,8 +129,6 @@ export class Animation {
122
129
  piece.userData.rotation.z = piece.rotation.z;
123
130
  });
124
131
  this._cube.group.add(...this._layerGroup.children);
125
- this._layerGroup.clear();
126
- this._cube.stickerState = this._cube.getStickerState();
127
132
  }
128
133
 
129
134
  update() {
package/src/cube.js CHANGED
@@ -28,15 +28,29 @@ export default class Cube {
28
28
  );
29
29
  piece.rotation.set(state.rotation.x, state.rotation.y, state.rotation.z);
30
30
  piece.userData = {
31
- position: state.position,
32
- rotation: state.rotation,
33
- initialPosition: state.position,
34
- initialRotation: state.rotation,
31
+ position: Object.assign({}, state.position),
32
+ rotation: Object.assign({}, state.rotation),
33
+ initialPosition: Object.assign({}, state.position),
34
+ initialRotation: Object.assign({}, state.rotation),
35
35
  type: state.type,
36
36
  };
37
37
  this.group.add(piece);
38
38
  }
39
- this.stickerState = this.getStickerState();
39
+ }
40
+
41
+ reset() {
42
+ this.group.children.forEach((piece) => {
43
+ const { x, y, z } = piece.userData.initialPosition;
44
+ const { x: u, y: v, z: w } = piece.userData.initialRotation;
45
+ piece.position.set(x * 1.04, y * 1.04, z * 1.04);
46
+ piece.rotation.set(u, v, w);
47
+ piece.userData.position.x = x
48
+ piece.userData.position.y = y
49
+ piece.userData.position.z = z
50
+ piece.userData.rotation.x = u
51
+ piece.userData.rotation.y = v
52
+ piece.userData.rotation.z = w
53
+ });
40
54
  }
41
55
  /**
42
56
  * @param {"x"|"y"|"z"} axis