Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,133 +1,117 @@
|
|
1 |
import streamlit as st
|
2 |
import streamlit.components.v1 as components
|
3 |
import random
|
4 |
-
import
|
5 |
import os
|
6 |
|
7 |
-
#
|
8 |
-
if not
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
<!DOCTYPE html>
|
12 |
<html>
|
13 |
<head>
|
14 |
<meta charset="utf-8">
|
15 |
-
<title>Chofko's
|
16 |
-
<script src="https://
|
17 |
-
<style>
|
18 |
-
.controls {
|
19 |
-
position: absolute;
|
20 |
-
bottom: 20px;
|
21 |
-
left: 50%;
|
22 |
-
transform: translateX(-50%);
|
23 |
-
display: flex;
|
24 |
-
gap: 10px;
|
25 |
-
}
|
26 |
-
.controls button {
|
27 |
-
padding: 10px 20px;
|
28 |
-
font-size: 16px;
|
29 |
-
cursor: pointer;
|
30 |
-
}
|
31 |
-
#score {
|
32 |
-
position: absolute;
|
33 |
-
top: 20px;
|
34 |
-
left: 20px;
|
35 |
-
font-size: 24px;
|
36 |
-
color: white;
|
37 |
-
background-color: rgba(0,0,0,0.5);
|
38 |
-
padding: 10px;
|
39 |
-
border-radius: 5px;
|
40 |
-
}
|
41 |
-
</style>
|
42 |
</head>
|
43 |
<body>
|
44 |
<a-scene>
|
45 |
<a-assets>
|
46 |
-
<
|
|
|
|
|
47 |
</a-assets>
|
48 |
|
49 |
-
<a-sky
|
50 |
-
|
51 |
-
<a-plane position="0 0 -4" rotation="-90 0 0" width="100" height="100" color="#7BC8A4"></a-plane>
|
52 |
|
53 |
-
|
54 |
|
55 |
-
<a-entity
|
56 |
</a-scene>
|
57 |
-
|
58 |
-
<div id="score">Score: 0</div>
|
59 |
-
|
60 |
-
<div class="controls">
|
61 |
-
<button onmousedown="startMove('left')" onmouseup="stopMove('left')" ontouchstart="startMove('left')" ontouchend="stopMove('left')">Left</button>
|
62 |
-
<button onmousedown="startMove('right')" onmouseup="stopMove('right')" ontouchstart="startMove('right')" ontouchend="stopMove('right')">Right</button>
|
63 |
-
<button onmousedown="startMove('up')" onmouseup="stopMove('up')" ontouchstart="startMove('up')" ontouchend="stopMove('up')">Up</button>
|
64 |
-
<button onmousedown="startMove('down')" onmouseup="stopMove('down')" ontouchstart="startMove('down')" ontouchend="stopMove('down')">Down</button>
|
65 |
-
<button onclick="toggleSpeed()">Toggle Speed</button>
|
66 |
-
</div>
|
67 |
-
|
68 |
-
<script>
|
69 |
-
// ... (Include the entire JavaScript code from the original HTML file here)
|
70 |
-
</script>
|
71 |
</body>
|
72 |
</html>
|
73 |
-
'''
|
74 |
-
|
75 |
-
with open("chofko_ecosystem.html", "r") as f:
|
76 |
-
initial_html = f.read()
|
77 |
-
|
78 |
-
# Initialize session state
|
79 |
-
if "html_content" not in st.session_state:
|
80 |
-
st.session_state.html_content = initial_html
|
81 |
-
if "player_names" not in st.session_state:
|
82 |
-
st.session_state.player_names = []
|
83 |
-
|
84 |
-
# Function to generate a random name for a new player
|
85 |
-
def generate_player_name():
|
86 |
-
adjectives = ["Adventurous", "Brave", "Curious", "Daring", "Fearless", "Intrepid", "Valiant"]
|
87 |
-
nouns = ["Explorer", "Wanderer", "Pathfinder", "Trailblazer", "Voyager", "Wayfarer", "Traveler"]
|
88 |
-
return random.choice(adjectives) + " " + random.choice(nouns)
|
89 |
-
|
90 |
-
# Function to add a new player to the scene
|
91 |
-
def add_player():
|
92 |
-
player_name = generate_player_name()
|
93 |
-
while player_name in st.session_state.player_names:
|
94 |
-
player_name = generate_player_name()
|
95 |
-
st.session_state.player_names.append(player_name)
|
96 |
-
player_html = f'''
|
97 |
-
new Entity(
|
98 |
-
{random.uniform(-50, 50)},
|
99 |
-
{random.uniform(-50, 50)},
|
100 |
-
false,
|
101 |
-
'a-sphere',
|
102 |
-
'{random.choice(['male', 'female'])}',
|
103 |
-
1
|
104 |
-
);
|
105 |
-
'''
|
106 |
-
st.session_state.html_content = st.session_state.html_content.replace("createEntities();", f"createEntities();\n {player_html}")
|
107 |
|
108 |
# Streamlit app
|
109 |
def main():
|
110 |
-
st.set_page_config(page_title="Chofko's
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
st.
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import streamlit.components.v1 as components
|
3 |
import random
|
4 |
+
import json
|
5 |
import os
|
6 |
|
7 |
+
# Initialize session state
|
8 |
+
if "entities" not in st.session_state:
|
9 |
+
st.session_state.entities = []
|
10 |
+
|
11 |
+
# Function to get random 3D model file
|
12 |
+
def get_random_3d_model(model_type):
|
13 |
+
model_dir = f"models/{model_type}"
|
14 |
+
model_files = [f for f in os.listdir(model_dir) if f.endswith(('.glb', '.obj'))]
|
15 |
+
return os.path.join(model_dir, random.choice(model_files)) if model_files else None
|
16 |
+
|
17 |
+
# Function to add a new entity to the scene
|
18 |
+
def add_entity(entity_type):
|
19 |
+
model_path = get_random_3d_model(entity_type)
|
20 |
+
if model_path:
|
21 |
+
new_entity = {
|
22 |
+
'type': entity_type,
|
23 |
+
'model': model_path,
|
24 |
+
'position': {
|
25 |
+
'x': random.uniform(-5, 5),
|
26 |
+
'y': 0,
|
27 |
+
'z': random.uniform(-5, 5)
|
28 |
+
},
|
29 |
+
'rotation': {
|
30 |
+
'x': 0,
|
31 |
+
'y': random.uniform(0, 360),
|
32 |
+
'z': 0
|
33 |
+
}
|
34 |
+
}
|
35 |
+
st.session_state.entities.append(new_entity)
|
36 |
+
|
37 |
+
# Function to generate A-Frame entities
|
38 |
+
def generate_aframe_entities():
|
39 |
+
entities_html = ""
|
40 |
+
for entity in st.session_state.entities:
|
41 |
+
entities_html += f'''
|
42 |
+
<a-entity
|
43 |
+
gltf-model="#{entity['type']}"
|
44 |
+
position="{entity['position']['x']} {entity['position']['y']} {entity['position']['z']}"
|
45 |
+
rotation="{entity['rotation']['x']} {entity['rotation']['y']} {entity['rotation']['z']}">
|
46 |
+
</a-entity>
|
47 |
+
'''
|
48 |
+
return entities_html
|
49 |
+
|
50 |
+
# HTML template
|
51 |
+
html_template = '''
|
52 |
<!DOCTYPE html>
|
53 |
<html>
|
54 |
<head>
|
55 |
<meta charset="utf-8">
|
56 |
+
<title>Chofko's Ecosystem Simulator</title>
|
57 |
+
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
</head>
|
59 |
<body>
|
60 |
<a-scene>
|
61 |
<a-assets>
|
62 |
+
<a-asset-item id="room" src="{room_model}"></a-asset-item>
|
63 |
+
<a-asset-item id="player" src="{player_model}"></a-asset-item>
|
64 |
+
<a-asset-item id="creature" src="{creature_model}"></a-asset-item>
|
65 |
</a-assets>
|
66 |
|
67 |
+
<a-sky color="#ECECEC"></a-sky>
|
68 |
+
<a-plane position="0 0 0" rotation="-90 0 0" width="10" height="10" color="#7BC8A4"></a-plane>
|
|
|
69 |
|
70 |
+
{entities}
|
71 |
|
72 |
+
<a-entity camera look-controls position="0 1.6 0"></a-entity>
|
73 |
</a-scene>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
</body>
|
75 |
</html>
|
76 |
+
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# Streamlit app
|
79 |
def main():
|
80 |
+
st.set_page_config(page_title="Chofko's Ecosystem Simulator", layout="wide")
|
81 |
+
|
82 |
+
# Sidebar
|
83 |
+
st.sidebar.title("Chofko's Ecosystem Controls")
|
84 |
+
|
85 |
+
if st.sidebar.button("🏠 Add Room"):
|
86 |
+
add_entity("room")
|
87 |
+
|
88 |
+
if st.sidebar.button("🧍 Add Player"):
|
89 |
+
add_entity("player")
|
90 |
+
|
91 |
+
if st.sidebar.button("🐾 Add Creature"):
|
92 |
+
add_entity("creature")
|
93 |
+
|
94 |
+
if st.sidebar.button("🗑️ Clear All Entities"):
|
95 |
+
st.session_state.entities = []
|
96 |
+
|
97 |
+
st.sidebar.subheader("Current Entities")
|
98 |
+
st.sidebar.json(st.session_state.entities)
|
99 |
+
|
100 |
+
# Main area - HTML5 canvas
|
101 |
+
room_model = get_random_3d_model("room") or ""
|
102 |
+
player_model = get_random_3d_model("player") or ""
|
103 |
+
creature_model = get_random_3d_model("creature") or ""
|
104 |
+
|
105 |
+
entities_html = generate_aframe_entities()
|
106 |
+
|
107 |
+
html_content = html_template.format(
|
108 |
+
room_model=room_model,
|
109 |
+
player_model=player_model,
|
110 |
+
creature_model=creature_model,
|
111 |
+
entities=entities_html
|
112 |
+
)
|
113 |
+
|
114 |
+
components.html(html_content, height=600)
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
main()
|