From 95c0bcdd10ffbbaccf1008ddc89e074b7da5ff34 Mon Sep 17 00:00:00 2001
From: adrien <adrien@malingrey.fr>
Date: Mon, 5 Jun 2023 01:29:34 +0200
Subject: [PATCH] MazeMesh as module

---
 MazeMesh.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 index.html  |  3 ++-
 main.js     | 60 +------------------------------------------------
 3 files changed, 67 insertions(+), 60 deletions(-)
 create mode 100644 MazeMesh.js

diff --git a/MazeMesh.js b/MazeMesh.js
new file mode 100644
index 0000000..5a1bcdc
--- /dev/null
+++ b/MazeMesh.js
@@ -0,0 +1,64 @@
+import * as THREE from 'three';
+
+const mazeGeometry = new THREE.BoxGeometry( 1, 1, 1 );
+
+class MazeMesh extends THREE.InstancedMesh {
+    constructor( width, length, material ) {
+        super( mazeGeometry, material, width*length - 2 );
+        this.length = length
+        this.width = width
+        this.map = new Array(length).fill().map(() => new Array(width).fill(1))
+        this.start = new THREE.Vector3(width/2, .1, length/2)
+        this.exit = new THREE.Vector3(Math.floor(width/2), 0, 1)
+        this.count = length * width - 2
+
+        this.dig(this.exit)
+        this.dig(new THREE.Vector3(Math.floor(width/2), 0, 0))
+        this.build ( this.exit )
+        let matrix = new THREE.Matrix4()
+        let i=0
+        this.map.forEach((row, z) => {
+            row.forEach((isWall, x) => {
+                if (isWall) {
+                    matrix.setPosition( x + .5 - width/2, 0.5, z + .5 - length/2)
+                    this.setMatrixAt( i, matrix );
+                    i++
+                }
+            })
+        })
+    }
+    
+    dig(position) {
+        this.map[position.z][position.x] = 0
+    }
+
+    static DIRECTIONS = [
+        new THREE.Vector3( 0, 0, -1),
+        new THREE.Vector3( 0, 0,  1),
+        new THREE.Vector3(-1, 0,  0),
+        new THREE.Vector3( 1, 0,  0),
+    ]
+    build(position) {
+        for (var direction of Array.from(this.constructor.DIRECTIONS).sort(x => .5 - Math.random())) {
+            var step1 = position.clone().add(direction)
+            var step2 = step1.clone().add(direction)
+            if (this.isWall(step2) == 1) {
+                this.dig(step1)
+                this.dig(step2)
+                this.count -= 2
+                this.build(step2)
+            }
+        }
+    }
+
+    isWall(position) {
+        if (0 <= position.x && position.x < this.width &&
+            0 <= position.z && position.z < this.length) {
+            return this.map[Math.floor(position.z)][Math.floor(position.x)]
+        } else {
+            return -1
+        }
+    }
+}
+
+export { MazeMesh }
\ No newline at end of file
diff --git a/index.html b/index.html
index 029aa77..f1d96b3 100644
--- a/index.html
+++ b/index.html
@@ -19,7 +19,8 @@
 			{
 				"imports": {
 					"three": "https://unpkg.com/three@0.152.2/build/three.module.js",
-					"three/addons/": "https://unpkg.com/three@0.152.2/examples/jsm/"
+					"three/addons/": "https://unpkg.com/three@0.152.2/examples/jsm/",
+					"MazeMesh": "./MazeMesh.js"
 				}
 			}
 		</script>
diff --git a/main.js b/main.js
index 8da3c97..006e3aa 100644
--- a/main.js
+++ b/main.js
@@ -12,65 +12,7 @@ import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
 import { Water } from 'three/addons/objects/Water.js';
 import { Sky } from 'three/addons/objects/Sky.js';
 
-const mazeGeometry = new THREE.BoxGeometry( 1, 1, 1 );
-class MazeMesh extends THREE.InstancedMesh {
-    constructor( width, length, material ) {
-        super( mazeGeometry, material, width*length - 2 );
-        this.length = length
-        this.width = width
-        this.map = new Array(length).fill().map(() => new Array(width).fill(1))
-        this.start = new THREE.Vector3(width/2, .1, length/2)
-        this.exit = new THREE.Vector3(Math.floor(width/2), 0, 1)
-        this.count = length * width - 2
-
-        this.dig(this.exit)
-        this.dig(new THREE.Vector3(Math.floor(width/2), 0, 0))
-        this.build ( this.exit )
-        let matrix = new THREE.Matrix4()
-        let i=0
-        this.map.forEach((row, z) => {
-            row.forEach((isWall, x) => {
-                if (isWall) {
-                    matrix.setPosition( x + .5 - width/2, 0.5, z + .5 - length/2)
-                    this.setMatrixAt( i, matrix );
-                    i++
-                }
-            })
-        })
-    }
-    
-    dig(position) {
-        this.map[position.z][position.x] = 0
-    }
-
-    static DIRECTIONS = [
-        new THREE.Vector3( 0, 0, -1),
-        new THREE.Vector3( 0, 0,  1),
-        new THREE.Vector3(-1, 0,  0),
-        new THREE.Vector3( 1, 0,  0),
-    ]
-    build(position) {
-        for (var direction of Array.from(this.constructor.DIRECTIONS).sort(x => .5 - Math.random())) {
-            var step1 = position.clone().add(direction)
-            var step2 = step1.clone().add(direction)
-            if (this.isWall(step2) == 1) {
-                this.dig(step1)
-                this.dig(step2)
-                this.count -= 2
-                this.build(step2)
-            }
-        }
-    }
-
-    isWall(position) {
-        if (0 <= position.x && position.x < this.width &&
-            0 <= position.z && position.z < this.length) {
-            return this.map[Math.floor(position.z)][Math.floor(position.x)]
-        } else {
-            return -1
-        }
-    }
-}
+import { MazeMesh } from 'MazeMesh';
 
 const latitude  = THREE.MathUtils.degToRad(35)
 const longitude = THREE.MathUtils.degToRad(25)