浏览代码

Start clean up the code.

Cixo Develop 9 月之前
父节点
当前提交
9bf62f69ea
共有 4 个文件被更改,包括 79 次插入6 次删除
  1. 9 2
      assets/actor.js
  2. 2 2
      assets/coordinates.js
  3. 56 0
      assets/factor.js
  4. 12 2
      assets/render-engine.js

+ 9 - 2
assets/actor.js

@@ -28,7 +28,7 @@ class actor extends coordinates {
         return this.#speed;
     }
 
-    rotate_cursor(cursor, size) {
+    rotate_cursor(cursor, size, scale = 2) {
         if (!(cursor instanceof position)) {
             throw new TypeError("Cursor compare must be an position object.");
         }
@@ -37,7 +37,14 @@ class actor extends coordinates {
             throw new TypeError("Size must be an position object.");
         }
 
-        const rotate = cursor.x / size.x * 360;        
+        if (typeof(scale) !== "number") {
+            throw new TypeError("Scale must be an number.");
+        }
+
+        const rotate = cursor.x / size.x * 360 * scale;
+        const bottom_rotate = cursor.y / size.y * 180 * scale;
+        
+        this.rotate_bottom(bottom_rotate);
         this.rotate_clockwise(rotate);
     }
 

+ 2 - 2
assets/coordinates.js

@@ -151,7 +151,7 @@ class coordinates {
             throw new TypeError("Degrees must be number.");
         }
 
-        this.#rotate + target;
+        this.#head_rotate += target;
     }
 
     rotate_bottom(target = 1) {
@@ -159,7 +159,7 @@ class coordinates {
             throw new TypeError("Degrees must be number.");
         }
 
-        this.#rotate -= target;
+        this.#head_rotate -= target;
     }
 
     get as_string() {

+ 56 - 0
assets/factor.js

@@ -0,0 +1,56 @@
+import * as three from "three-js";
+
+class factor {
+    #mesh;
+
+    constructor(mesh) {
+        if (!(mesh instanceof three.Mesh)) {
+            throw new TypeError("Must initialize with Mesh.");
+        }
+
+        this.#mesh = mesh;
+    }
+
+    get mesh() {
+        if (!(this.#mesh instanceof three.Mesh)) {
+            throw new TypeError("Mesh is not initialized.");
+        }
+
+        return this.#mesh;
+    }
+
+    loop() {
+        return null;
+    }
+}
+
+class functional_factor extends factor {
+    #animation
+    
+    constructor (init, animation = null) {
+        if (typeof(init) !== "function") {
+            throw new TypeError("Init must be an function.");
+        }   
+
+        if (animation !== null && typeof(animation) !== "function") {
+            throw new TypeError("Animation could only be null or function.");
+        }
+
+        const mesh = init(this);
+        
+        if (!(mesh instanceof three.Mesh)) {
+            throw new TypeError("Factor initializer must return mesh.");  
+        }
+
+        super(mesh);
+        this.#animation = animation;
+    }   
+
+    loop() {
+        if (this.#animation !== null) {
+            this.#animation(this);   
+        }
+    }
+} 
+
+export { factor, functional_factor }

+ 12 - 2
assets/render-engine.js

@@ -8,14 +8,15 @@ class render_engine {
     #canvas;
     #actors;
     #player;
+    #looped;
 
     constructor(canvas, context, player) {
         if (!(canvas instanceof HTMLCanvasElement)) {
             throw new TypeError("Canvas must be an HTMLCanvasElement.");
         }
 
-        if (!(context instanceof WebGL2RenderingContext)) {
-            throw new TypeError("Context must be WebGL2 RenderingContext.");
+        if (!(context instanceof WebGLRenderingContext)) {
+            throw new TypeError("Context must be WebGL RenderingContext.");
         }
 
         if (!(player instanceof actor)) {
@@ -59,11 +60,20 @@ class render_engine {
     }
 
     run() {
+        this.#looped = true;
         this.#init();
         this.#loop();
     }
 
+    stop() {
+        this.#looped = false;
+    }
+
     #loop() {
+        if (!this.#looped) {
+            return;
+        }
+
         const start = performance.now();
         this.#render();
         const stop = performance.now();