Spaces:
Sleeping
Sleeping
Update index.html
Browse files- index.html +61 -18
index.html
CHANGED
@@ -34,7 +34,7 @@
|
|
34 |
let selectedObjectType = window.SELECTED_OBJECT_TYPE || "None";
|
35 |
const plotWidth = window.PLOT_WIDTH || 50.0;
|
36 |
const plotDepth = window.PLOT_DEPTH || 50.0;
|
37 |
-
const worldState = window.WORLD_STATE || { objects: {}, players: {} };
|
38 |
|
39 |
// Materials
|
40 |
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x55aa55, roughness: 0.9, metalness: 0.1, side: THREE.DoubleSide });
|
@@ -51,6 +51,12 @@
|
|
51 |
'light': new THREE.MeshBasicMaterial({ color: 0xFFFF88 })
|
52 |
};
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
function init() {
|
55 |
scene = new THREE.Scene();
|
56 |
scene.background = new THREE.Color(0xabcdef);
|
@@ -129,14 +135,30 @@
|
|
129 |
function loadWorldState() {
|
130 |
clearWorldObjects();
|
131 |
const objects = worldState.objects || {};
|
|
|
132 |
for (const obj_id in objects) {
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
}
|
135 |
const players = worldState.players || {};
|
|
|
136 |
for (const username in players) {
|
137 |
const pos = players[username].position;
|
138 |
-
|
|
|
|
|
|
|
|
|
139 |
}
|
|
|
|
|
140 |
}
|
141 |
|
142 |
function clearWorldObjects() {
|
@@ -160,10 +182,11 @@
|
|
160 |
marker.userData.isPlayerMarker = true;
|
161 |
marker.userData.username = username;
|
162 |
scene.add(marker);
|
|
|
163 |
}
|
164 |
|
165 |
function createAndPlaceObject(objData, isNewlyPlacedLocally) {
|
166 |
-
if (!objData || !objData.obj_id || !objData.type) {
|
167 |
console.warn("Invalid object data:", objData);
|
168 |
return null;
|
169 |
}
|
@@ -176,10 +199,15 @@
|
|
176 |
mesh.position.set(objData.position.x, objData.position.y, objData.position.z);
|
177 |
}
|
178 |
if (objData.rotation && (
|
179 |
-
Math.abs(mesh.rotation.x - objData.rotation._x) > 0.01 ||
|
180 |
-
Math.abs(mesh.rotation.y - objData.rotation._y) > 0.01 ||
|
181 |
-
Math.abs(mesh.rotation.z - objData.rotation._z) > 0.01 )) {
|
182 |
-
mesh.rotation.set(
|
|
|
|
|
|
|
|
|
|
|
183 |
}
|
184 |
} else {
|
185 |
mesh = createPrimitiveMesh(objData.type);
|
@@ -189,16 +217,26 @@
|
|
189 |
mesh.userData.type = objData.type;
|
190 |
mesh.position.set(objData.position.x, objData.position.y, objData.position.z);
|
191 |
if (objData.rotation) {
|
192 |
-
mesh.rotation.set(
|
|
|
|
|
|
|
|
|
|
|
193 |
}
|
194 |
scene.add(mesh);
|
195 |
worldObjects.set(objData.obj_id, mesh);
|
196 |
-
console.log(`Created new object ${objData.obj_id} (${objData.type})
|
197 |
}
|
198 |
return mesh;
|
199 |
}
|
200 |
|
201 |
function createPrimitiveMesh(type) {
|
|
|
|
|
|
|
|
|
|
|
202 |
let mesh = null;
|
203 |
let geometry, material, material2;
|
204 |
|
@@ -462,7 +500,7 @@
|
|
462 |
mesh.receiveShadow = true;
|
463 |
break;
|
464 |
default:
|
465 |
-
console.warn("
|
466 |
return null;
|
467 |
}
|
468 |
} catch (e) {
|
@@ -515,12 +553,13 @@
|
|
515 |
|
516 |
console.log(`Placing ${selectedObjectType} (${newObjData.obj_id}) at`, newObjData.position);
|
517 |
|
518 |
-
createAndPlaceObject(newObjData, true);
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
|
|
524 |
} else {
|
525 |
// Move player
|
526 |
const newPosition = {
|
@@ -585,7 +624,10 @@
|
|
585 |
window.addEventListener('message', (event) => {
|
586 |
const data = event.data;
|
587 |
if (data.type === 'place_object') {
|
588 |
-
createAndPlaceObject(data.payload.object_data, false);
|
|
|
|
|
|
|
589 |
} else if (data.type === 'delete_object') {
|
590 |
removeObjectById(data.payload.obj_id);
|
591 |
} else if (data.type === 'move_player') {
|
@@ -594,6 +636,7 @@
|
|
594 |
scene.children.forEach(child => {
|
595 |
if (child.userData.isPlayerMarker && child.userData.username === username) {
|
596 |
child.position.set(position.x, position.y + 0.5, position.z);
|
|
|
597 |
}
|
598 |
});
|
599 |
}
|
|
|
34 |
let selectedObjectType = window.SELECTED_OBJECT_TYPE || "None";
|
35 |
const plotWidth = window.PLOT_WIDTH || 50.0;
|
36 |
const plotDepth = window.PLOT_DEPTH || 50.0;
|
37 |
+
const worldState = window.WORLD_STATE || { objects: {}, players: {}, action_history: [] };
|
38 |
|
39 |
// Materials
|
40 |
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x55aa55, roughness: 0.9, metalness: 0.1, side: THREE.DoubleSide });
|
|
|
51 |
'light': new THREE.MeshBasicMaterial({ color: 0xFFFF88 })
|
52 |
};
|
53 |
|
54 |
+
const VALID_OBJECT_TYPES = [
|
55 |
+
"Tree", "Rock", "Simple House", "Pine Tree", "Brick Wall", "Sphere", "Cube",
|
56 |
+
"Cylinder", "Cone", "Torus", "Mushroom", "Cactus", "Campfire", "Star", "Gem",
|
57 |
+
"Tower", "Barrier", "Fountain", "Lantern", "Sign Post"
|
58 |
+
];
|
59 |
+
|
60 |
function init() {
|
61 |
scene = new THREE.Scene();
|
62 |
scene.background = new THREE.Color(0xabcdef);
|
|
|
135 |
function loadWorldState() {
|
136 |
clearWorldObjects();
|
137 |
const objects = worldState.objects || {};
|
138 |
+
console.log(`Loading ${Object.keys(objects).length} objects from WORLD_STATE`);
|
139 |
for (const obj_id in objects) {
|
140 |
+
const objData = objects[obj_id];
|
141 |
+
if (!objData || !objData.obj_id || !objData.type || !VALID_OBJECT_TYPES.includes(objData.type) || !objData.position) {
|
142 |
+
console.warn(`Skipping invalid object: ${obj_id}`, objData);
|
143 |
+
continue;
|
144 |
+
}
|
145 |
+
const mesh = createAndPlaceObject(objData, false);
|
146 |
+
if (!mesh) {
|
147 |
+
console.warn(`Failed to create mesh for object: ${obj_id}`, objData);
|
148 |
+
}
|
149 |
}
|
150 |
const players = worldState.players || {};
|
151 |
+
console.log(`Loading ${Object.keys(players).length} players from WORLD_STATE`);
|
152 |
for (const username in players) {
|
153 |
const pos = players[username].position;
|
154 |
+
if (pos && typeof pos.x === 'number' && typeof pos.y === 'number' && typeof pos.z === 'number') {
|
155 |
+
createPlayerMarker(username, pos);
|
156 |
+
} else {
|
157 |
+
console.warn(`Invalid player position for ${username}:`, pos);
|
158 |
+
}
|
159 |
}
|
160 |
+
const actionHistory = worldState.action_history || [];
|
161 |
+
console.log(`Loaded ${actionHistory.length} action history entries`);
|
162 |
}
|
163 |
|
164 |
function clearWorldObjects() {
|
|
|
182 |
marker.userData.isPlayerMarker = true;
|
183 |
marker.userData.username = username;
|
184 |
scene.add(marker);
|
185 |
+
console.log(`Created player marker for ${username} at`, position);
|
186 |
}
|
187 |
|
188 |
function createAndPlaceObject(objData, isNewlyPlacedLocally) {
|
189 |
+
if (!objData || !objData.obj_id || !objData.type || !objData.position) {
|
190 |
console.warn("Invalid object data:", objData);
|
191 |
return null;
|
192 |
}
|
|
|
199 |
mesh.position.set(objData.position.x, objData.position.y, objData.position.z);
|
200 |
}
|
201 |
if (objData.rotation && (
|
202 |
+
Math.abs(mesh.rotation.x - (objData.rotation._x || 0)) > 0.01 ||
|
203 |
+
Math.abs(mesh.rotation.y - (objData.rotation._y || 0)) > 0.01 ||
|
204 |
+
Math.abs(mesh.rotation.z - (objData.rotation._z || 0)) > 0.01 )) {
|
205 |
+
mesh.rotation.set(
|
206 |
+
objData.rotation._x || 0,
|
207 |
+
objData.rotation._y || 0,
|
208 |
+
objData.rotation._z || 0,
|
209 |
+
objData.rotation._order || 'XYZ'
|
210 |
+
);
|
211 |
}
|
212 |
} else {
|
213 |
mesh = createPrimitiveMesh(objData.type);
|
|
|
217 |
mesh.userData.type = objData.type;
|
218 |
mesh.position.set(objData.position.x, objData.position.y, objData.position.z);
|
219 |
if (objData.rotation) {
|
220 |
+
mesh.rotation.set(
|
221 |
+
objData.rotation._x || 0,
|
222 |
+
objData.rotation._y || 0,
|
223 |
+
objData.rotation._z || 0,
|
224 |
+
objData.rotation._order || 'XYZ'
|
225 |
+
);
|
226 |
}
|
227 |
scene.add(mesh);
|
228 |
worldObjects.set(objData.obj_id, mesh);
|
229 |
+
console.log(`Created new object ${objData.obj_id} (${objData.type}) at`, objData.position);
|
230 |
}
|
231 |
return mesh;
|
232 |
}
|
233 |
|
234 |
function createPrimitiveMesh(type) {
|
235 |
+
if (!VALID_OBJECT_TYPES.includes(type)) {
|
236 |
+
console.warn(`Invalid object type for mesh creation: ${type}`);
|
237 |
+
return null;
|
238 |
+
}
|
239 |
+
|
240 |
let mesh = null;
|
241 |
let geometry, material, material2;
|
242 |
|
|
|
500 |
mesh.receiveShadow = true;
|
501 |
break;
|
502 |
default:
|
503 |
+
console.warn("Unexpected object type:", type);
|
504 |
return null;
|
505 |
}
|
506 |
} catch (e) {
|
|
|
553 |
|
554 |
console.log(`Placing ${selectedObjectType} (${newObjData.obj_id}) at`, newObjData.position);
|
555 |
|
556 |
+
const mesh = createAndPlaceObject(newObjData, true);
|
557 |
+
if (mesh) {
|
558 |
+
window.parent.postMessage({
|
559 |
+
type: 'place_object',
|
560 |
+
payload: { username: myUsername, object_data: newObjData }
|
561 |
+
}, '*');
|
562 |
+
}
|
563 |
} else {
|
564 |
// Move player
|
565 |
const newPosition = {
|
|
|
624 |
window.addEventListener('message', (event) => {
|
625 |
const data = event.data;
|
626 |
if (data.type === 'place_object') {
|
627 |
+
const mesh = createAndPlaceObject(data.payload.object_data, false);
|
628 |
+
if (!mesh) {
|
629 |
+
console.warn(`Failed to place object from message:`, data.payload.object_data);
|
630 |
+
}
|
631 |
} else if (data.type === 'delete_object') {
|
632 |
removeObjectById(data.payload.obj_id);
|
633 |
} else if (data.type === 'move_player') {
|
|
|
636 |
scene.children.forEach(child => {
|
637 |
if (child.userData.isPlayerMarker && child.userData.username === username) {
|
638 |
child.position.set(position.x, position.y + 0.5, position.z);
|
639 |
+
console.log(`Updated player marker for ${username} to`, position);
|
640 |
}
|
641 |
});
|
642 |
}
|