Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import uuid
|
|
8 |
import math
|
9 |
import time
|
10 |
|
11 |
-
# Import our GameState class
|
12 |
from gamestate import GameState
|
13 |
|
14 |
# --- Constants ---
|
@@ -104,12 +104,15 @@ def save_plot_data(filename, objects_data_list, plot_x_offset, plot_z_offset):
|
|
104 |
continue
|
105 |
|
106 |
relative_obj = {
|
107 |
-
'obj_id': obj_id,
|
|
|
108 |
'pos_x': pos.get('x', 0.0) - plot_x_offset,
|
109 |
'pos_y': pos.get('y', 0.0),
|
110 |
'pos_z': pos.get('z', 0.0) - plot_z_offset,
|
111 |
-
'rot_x': rot.get('_x', 0.0),
|
112 |
-
'
|
|
|
|
|
113 |
}
|
114 |
relative_objects.append(relative_obj)
|
115 |
|
@@ -161,8 +164,8 @@ with st.sidebar:
|
|
161 |
target_x = plot['x_offset']
|
162 |
target_z = plot['z_offset']
|
163 |
try:
|
164 |
-
js_code = f"teleportPlayer({target_x + PLOT_WIDTH/2}, {target_z + PLOT_DEPTH/2});"
|
165 |
from streamlit_js_eval import streamlit_js_eval
|
|
|
166 |
streamlit_js_eval(js_code=js_code, key=f"teleport_{plot['id']}")
|
167 |
except Exception as e:
|
168 |
st.error(f"Failed to send teleport command: {e}")
|
@@ -236,7 +239,7 @@ if save_data_from_js is not None:
|
|
236 |
st.header("Infinite Shared 3D World")
|
237 |
st.caption("Move to empty areas to expand the world. Use the sidebar 'Save' to store your work.")
|
238 |
|
239 |
-
#
|
240 |
injected_state = {
|
241 |
"ALL_INITIAL_OBJECTS": all_initial_objects,
|
242 |
"PLOTS_METADATA": plots_metadata,
|
@@ -248,11 +251,13 @@ injected_state = {
|
|
248 |
|
249 |
html_file_path = 'index.html'
|
250 |
html_content_with_state = None
|
|
|
251 |
try:
|
252 |
with open(html_file_path, 'r', encoding='utf-8') as f:
|
253 |
html_template = f.read()
|
254 |
|
255 |
-
|
|
|
256 |
<script>
|
257 |
window.ALL_INITIAL_OBJECTS = {json.dumps(injected_state["ALL_INITIAL_OBJECTS"])};
|
258 |
window.PLOTS_METADATA = {json.dumps(injected_state["PLOTS_METADATA"])};
|
@@ -268,4 +273,11 @@ js_injection_script = f"""
|
|
268 |
}});
|
269 |
</script>
|
270 |
"""
|
271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import math
|
9 |
import time
|
10 |
|
11 |
+
# Import our GameState class from gamestate.py
|
12 |
from gamestate import GameState
|
13 |
|
14 |
# --- Constants ---
|
|
|
104 |
continue
|
105 |
|
106 |
relative_obj = {
|
107 |
+
'obj_id': obj_id,
|
108 |
+
'type': obj_type,
|
109 |
'pos_x': pos.get('x', 0.0) - plot_x_offset,
|
110 |
'pos_y': pos.get('y', 0.0),
|
111 |
'pos_z': pos.get('z', 0.0) - plot_z_offset,
|
112 |
+
'rot_x': rot.get('_x', 0.0),
|
113 |
+
'rot_y': rot.get('_y', 0.0),
|
114 |
+
'rot_z': rot.get('_z', 0.0),
|
115 |
+
'rot_order': rot.get('_order', 'XYZ')
|
116 |
}
|
117 |
relative_objects.append(relative_obj)
|
118 |
|
|
|
164 |
target_x = plot['x_offset']
|
165 |
target_z = plot['z_offset']
|
166 |
try:
|
|
|
167 |
from streamlit_js_eval import streamlit_js_eval
|
168 |
+
js_code = f"teleportPlayer({target_x + PLOT_WIDTH/2}, {target_z + PLOT_DEPTH/2});"
|
169 |
streamlit_js_eval(js_code=js_code, key=f"teleport_{plot['id']}")
|
170 |
except Exception as e:
|
171 |
st.error(f"Failed to send teleport command: {e}")
|
|
|
239 |
st.header("Infinite Shared 3D World")
|
240 |
st.caption("Move to empty areas to expand the world. Use the sidebar 'Save' to store your work.")
|
241 |
|
242 |
+
# Prepare state to inject into HTML/JS
|
243 |
injected_state = {
|
244 |
"ALL_INITIAL_OBJECTS": all_initial_objects,
|
245 |
"PLOTS_METADATA": plots_metadata,
|
|
|
251 |
|
252 |
html_file_path = 'index.html'
|
253 |
html_content_with_state = None
|
254 |
+
|
255 |
try:
|
256 |
with open(html_file_path, 'r', encoding='utf-8') as f:
|
257 |
html_template = f.read()
|
258 |
|
259 |
+
# Use doubled curly braces for literal braces in the injected JS code.
|
260 |
+
js_injection_script = f"""
|
261 |
<script>
|
262 |
window.ALL_INITIAL_OBJECTS = {json.dumps(injected_state["ALL_INITIAL_OBJECTS"])};
|
263 |
window.PLOTS_METADATA = {json.dumps(injected_state["PLOTS_METADATA"])};
|
|
|
273 |
}});
|
274 |
</script>
|
275 |
"""
|
276 |
+
html_content_with_state = html_template.replace('</head>', js_injection_script + '\n</head>', 1)
|
277 |
+
components.html(html_content_with_state, height=750, scrolling=False)
|
278 |
+
except FileNotFoundError:
|
279 |
+
st.error(f"CRITICAL ERROR: Could not find the file '{html_file_path}'.")
|
280 |
+
st.warning(f"Make sure `{html_file_path}` is in the same directory as `app.py` and `{SAVE_DIR}` exists.")
|
281 |
+
except Exception as e:
|
282 |
+
st.error(f"An critical error occurred during HTML preparation or component rendering: {e}")
|
283 |
+
st.exception(e)
|