Spaces:
Running
Running
Create index.html
Browse files- index.html +133 -0
index.html
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Setup Babylon.js
|
2 |
+
const canvas = document.getElementById("renderCanvas");
|
3 |
+
const engine = new BABYLON.Engine(canvas, true);
|
4 |
+
|
5 |
+
const createScene = function() {
|
6 |
+
const scene = new BABYLON.Scene(engine);
|
7 |
+
|
8 |
+
// Skybox setup
|
9 |
+
scene.environmentTexture = new BABYLON.CubeTexture("sky.jpg", scene);
|
10 |
+
|
11 |
+
// Camera
|
12 |
+
const camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 100, BABYLON.Vector3.Zero(), scene);
|
13 |
+
camera.attachControl(canvas, true);
|
14 |
+
|
15 |
+
// Light
|
16 |
+
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
|
17 |
+
|
18 |
+
// Load maps
|
19 |
+
const mapMatrix = generateRandomMap();
|
20 |
+
const mapWidth = mapMatrix[0].length;
|
21 |
+
const mapHeight = mapMatrix.length;
|
22 |
+
|
23 |
+
// Ground setup with the same size as the map
|
24 |
+
const ground = BABYLON.MeshBuilder.CreateGround("ground", { width: mapWidth, height: mapHeight }, scene);
|
25 |
+
ground.material = new BABYLON.StandardMaterial("groundMat", scene);
|
26 |
+
ground.material.diffuseTexture = new BABYLON.Texture("sky.jpg", scene);
|
27 |
+
|
28 |
+
// Position ground underneath the map
|
29 |
+
ground.position.x = mapWidth / 2 - 0.5;
|
30 |
+
ground.position.z = mapHeight / 2 - 0.5;
|
31 |
+
|
32 |
+
// Create a video texture
|
33 |
+
const videoTexture = new BABYLON.VideoTexture("video", ["wall1.mp4"], scene, true);
|
34 |
+
|
35 |
+
// Create a material and assign the video texture to it
|
36 |
+
const videoMaterial = new BABYLON.StandardMaterial("videoMat", scene);
|
37 |
+
videoMaterial.diffuseTexture = videoTexture;
|
38 |
+
|
39 |
+
// Create map
|
40 |
+
createMapFromMatrix(mapMatrix, scene, videoMaterial);
|
41 |
+
|
42 |
+
// Player
|
43 |
+
const startPosition = getStartPosition(mapMatrix);
|
44 |
+
const player = BABYLON.MeshBuilder.CreateBox("player", { size: 1 }, scene);
|
45 |
+
player.position = new BABYLON.Vector3(startPosition.x, 0.5, startPosition.z);
|
46 |
+
player.material = new BABYLON.StandardMaterial("playerMat", scene);
|
47 |
+
player.material.diffuseTexture = new BABYLON.Texture("sky.jpg", scene);
|
48 |
+
|
49 |
+
// NPC - Red Sphere
|
50 |
+
const npcPosition = getRandomFreePosition(mapMatrix);
|
51 |
+
const npc = BABYLON.MeshBuilder.CreateSphere("npc", { diameter: 1 }, scene);
|
52 |
+
npc.position = new BABYLON.Vector3(npcPosition.x, 0.5, npcPosition.z);
|
53 |
+
npc.material = new BABYLON.StandardMaterial("npcMat", scene);
|
54 |
+
npc.material.diffuseColor = new BABYLON.Color3(1, 0, 0); // Red color
|
55 |
+
|
56 |
+
// Basic movement
|
57 |
+
window.addEventListener("keydown", (event) => {
|
58 |
+
switch (event.key) {
|
59 |
+
case "ArrowUp":
|
60 |
+
player.position.z -= 0.1;
|
61 |
+
break;
|
62 |
+
case "ArrowDown":
|
63 |
+
player.position.z += 0.1;
|
64 |
+
break;
|
65 |
+
case "ArrowLeft":
|
66 |
+
player.position.x -= 0.1;
|
67 |
+
break;
|
68 |
+
case "ArrowRight":
|
69 |
+
player.position.x += 0.1;
|
70 |
+
break;
|
71 |
+
}
|
72 |
+
});
|
73 |
+
|
74 |
+
return scene;
|
75 |
+
};
|
76 |
+
|
77 |
+
function generateRandomMap() {
|
78 |
+
const size = 100;
|
79 |
+
const map = [];
|
80 |
+
for (let y = 0; y < size; y++) {
|
81 |
+
const row = [];
|
82 |
+
for (let x = 0; x < size; x++) {
|
83 |
+
row.push(Math.random() > 0.8 ? 1 : 0); // 1 for wall, 0 for empty
|
84 |
+
}
|
85 |
+
map.push(row);
|
86 |
+
}
|
87 |
+
return map;
|
88 |
+
}
|
89 |
+
|
90 |
+
function createMapFromMatrix(matrix, scene, wallMaterial) {
|
91 |
+
matrix.forEach((row, y) => {
|
92 |
+
row.forEach((value, x) => {
|
93 |
+
if (value === 1) {
|
94 |
+
const wall = BABYLON.MeshBuilder.CreateBox("wall_" + x + "_" + y, {
|
95 |
+
width: 1,
|
96 |
+
height: 2,
|
97 |
+
depth: 1
|
98 |
+
}, scene);
|
99 |
+
wall.position = new BABYLON.Vector3(x, 1, y);
|
100 |
+
wall.material = wallMaterial; // Assign the same material to all walls
|
101 |
+
}
|
102 |
+
});
|
103 |
+
});
|
104 |
+
}
|
105 |
+
|
106 |
+
function getStartPosition(matrix) {
|
107 |
+
for (let y = 0; y < matrix.length; y++) {
|
108 |
+
for (let x = 0; x < matrix[y].length; x++) {
|
109 |
+
if (matrix[y][x] === 0) {
|
110 |
+
return { x, z: y }; // Starting position
|
111 |
+
}
|
112 |
+
}
|
113 |
+
}
|
114 |
+
return { x: 0, z: 0 }; // Default position if none found
|
115 |
+
}
|
116 |
+
|
117 |
+
function getRandomFreePosition(matrix) {
|
118 |
+
let x, z;
|
119 |
+
do {
|
120 |
+
x = Math.floor(Math.random() * matrix[0].length);
|
121 |
+
z = Math.floor(Math.random() * matrix.length);
|
122 |
+
} while (matrix[z][x] !== 0); // Ensure the position is free (no wall)
|
123 |
+
return { x, z };
|
124 |
+
}
|
125 |
+
|
126 |
+
const scene = createScene();
|
127 |
+
engine.runRenderLoop(() => {
|
128 |
+
scene.render();
|
129 |
+
});
|
130 |
+
|
131 |
+
window.addEventListener("resize", () => {
|
132 |
+
engine.resize();
|
133 |
+
});
|