File size: 33,790 Bytes
e172c2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Wacky Shapes Adventure!</title>
    <style>
        /* --- Base Styles (Similar to previous, slightly tweaked) --- */
        body { font-family: 'Verdana', sans-serif; background-color: #2a3a4a; color: #f0f0f0; margin: 0; padding: 0; overflow: hidden; display: flex; flex-direction: column; height: 100vh; }
        #game-container { display: flex; flex-grow: 1; overflow: hidden; }
        #scene-container { flex-grow: 3; position: relative; border-right: 3px solid #506070; min-width: 250px; background-color: #101820; height: 100%; box-sizing: border-box; overflow: hidden; }
        #ui-container { flex-grow: 2; padding: 25px; overflow-y: auto; background-color: #3a4a5a; min-width: 320px; height: 100%; box-sizing: border-box; display: flex; flex-direction: column; }
        #scene-container canvas { display: block; }

        /* --- UI Elements --- */
        #story-title { color: #ffd700; /* Gold */ margin: 0 0 15px 0; padding-bottom: 10px; border-bottom: 2px solid #506070; font-size: 1.8em; font-weight: bold; text-shadow: 1px 1px 2px #111; }
        #story-content { margin-bottom: 25px; line-height: 1.7; flex-grow: 1; font-size: 1.1em; color: #e8e8e8;}
        #stats-inventory-container { margin-bottom: 25px; padding: 15px; border: 1px solid #506070; border-radius: 5px; background-color: #4a5a6a; font-size: 0.95em; }
        #stats-display, #inventory-display { margin-bottom: 10px; line-height: 1.8; }
        #stats-display span, #inventory-display .item-tag { display: inline-block; background-color: #5a6a7a; padding: 4px 10px; border-radius: 15px; margin: 0 8px 5px 0; border: 1px solid #7a8a9a; white-space: nowrap; box-shadow: inset 0 1px 2px rgba(0,0,0,0.2); }
        #stats-display strong, #inventory-display strong { color: #e0e0e0; margin-right: 6px; }
        #inventory-display em { color: #aabbcc; font-style: normal; }
        .item-tool { background-color: #a0522d; border-color: #cd853f; color: #fff;} /* Sienna/Peru */
        .item-key { background-color: #ffd700; border-color: #f0e68c; color: #333;} /* Gold/Khaki */
        .item-treasure { background-color: #20b2aa; border-color: #48d1cc; color: #fff;} /* LightSeaGreen/MediumTurquoise */
        .item-food { background-color: #ff6347; border-color: #fa8072; color: #fff;} /* Tomato/Salmon */
        .item-unknown { background-color: #778899; border-color: #b0c4de;} /* LightSlateGray/LightSteelBlue */

        /* --- Choices & Messages --- */
        #choices-container { margin-top: auto; padding-top: 20px; border-top: 2px solid #506070; }
        #choices-container h3 { margin-top: 0; margin-bottom: 12px; color: #e0e0e0; font-size: 1.2em; font-weight: bold;}
        #choices { display: flex; flex-direction: column; gap: 12px; }
        .choice-button { display: block; width: 100%; padding: 13px 16px; margin-bottom: 0; background-color: #607080; color: #fff; border: 1px solid #8090a0; border-radius: 5px; cursor: pointer; text-align: left; font-family: 'Verdana', sans-serif; font-size: 1.05em; font-weight: bold; transition: background-color 0.2s, transform 0.1s; box-sizing: border-box; letter-spacing: 0.5px; }
        .choice-button:hover:not(:disabled) { background-color: #ffc107; color: #222; border-color: #ffca2c; transform: translateY(-1px); }
        .choice-button:disabled { background-color: #4e5a66; color: #8a9aab; cursor: not-allowed; border-color: #607080; opacity: 0.7; }
        .message { padding: 8px 12px; margin: 10px 0; border-left-width: 4px; border-left-style: solid; font-size: 1em; background-color: rgba(0, 0, 0, 0.1); border-radius: 3px; }
        .message-info { color: #ccc; border-left-color: #666; }
        .message-item { color: #9cf; border-left-color: #48a; }
        .message-xp { color: #af8; border-left-color: #6a4; }
        .message-warning { color: #f99; border-left-color: #c66; }

    </style>
</head>
<body>
    <div id="game-container">
        <div id="scene-container"></div>
        <div id="ui-container">
            <h2 id="story-title">Adventure Awaits!</h2>
            <div id="story-content"><p>Getting ready...</p></div>
            <div id="stats-inventory-container">
                <div id="stats-display"></div>
                <div id="inventory-display"></div>
            </div>
            <div id="choices-container">
                <h3>What adventure will you choose?</h3>
                <div id="choices"></div>
            </div>
        </div>
    </div>

    <script type="importmap">
        { "imports": {
                "three": "https://unpkg.com/[email protected]/build/three.module.js",
                "three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
        }}
    </script>

    <script type="module">
        import * as THREE from 'three';

        console.log("Script module execution started.");

        const sceneContainer = document.getElementById('scene-container');
        const storyTitleElement = document.getElementById('story-title');
        const storyContentElement = document.getElementById('story-content');
        const choicesElement = document.getElementById('choices');
        const statsElement = document.getElementById('stats-display');
        const inventoryElement = document.getElementById('inventory-display');

        let scene, camera, renderer, clock;
        let currentSceneGroup = null; // Holds the group for the current scene's objects
        let currentLights = [];
        let currentMessage = ""; // Accumulates messages for UI update

        const MAT = { // Material library
            stone_grey: new THREE.MeshStandardMaterial({ color: 0x8a8a8a, roughness: 0.8 }),
            stone_brown: new THREE.MeshStandardMaterial({ color: 0x9d8468, roughness: 0.85 }),
            wood_light: new THREE.MeshStandardMaterial({ color: 0xcdaa7d, roughness: 0.7 }),
            wood_dark: new THREE.MeshStandardMaterial({ color: 0x6f4e2d, roughness: 0.75 }),
            leaf_green: new THREE.MeshStandardMaterial({ color: 0x4caf50, roughness: 0.6, side: THREE.DoubleSide }),
            leaf_autumn: new THREE.MeshStandardMaterial({ color: 0xffa040, roughness: 0.65, side: THREE.DoubleSide }),
            ground_dirt: new THREE.MeshStandardMaterial({ color: 0x8b5e3c, roughness: 0.9 }),
            ground_grass: new THREE.MeshStandardMaterial({ color: 0x558b2f, roughness: 0.85 }),
            metal_shiny: new THREE.MeshStandardMaterial({ color: 0xc0c0c0, roughness: 0.2, metalness: 0.8 }),
            metal_rusty: new THREE.MeshStandardMaterial({ color: 0xb7410e, roughness: 0.9, metalness: 0.2 }),
            crystal_blue: new THREE.MeshStandardMaterial({ color: 0x87cefa, roughness: 0.1, metalness: 0.1, transparent: true, opacity: 0.8, emissive:0x205080, emissiveIntensity: 0.4 }),
            crystal_red: new THREE.MeshStandardMaterial({ color: 0xff7f7f, roughness: 0.1, metalness: 0.1, transparent: true, opacity: 0.8, emissive:0x802020, emissiveIntensity: 0.4 }),
            bright_yellow: new THREE.MeshStandardMaterial({ color: 0xffeb3b, roughness: 0.6 }),
            deep_purple: new THREE.MeshStandardMaterial({ color: 0x9c27b0, roughness: 0.7 }),
            sky_blue: new THREE.MeshStandardMaterial({ color: 0x03a9f4, roughness: 0.5 }),
        };

        // --- Game State ---
        let gameState = {}; // Initialized in startGame

        // --- Item Data ---
        const itemsData = {
            "Wobbly Key": {type:"key", description:"Looks like it fits a jiggly lock."},
            "Shiny Sprocket": {type:"treasure", description:"A gear that gleams."},
            "Bouncy Mushroom": {type:"food", description:"Seems edible... maybe?"},
            "Sturdy Stick": {type:"tool", description:"Good for poking things."},
        };

        // --- Procedural Assembly Shapes ---
        const SHAPE_GENERATORS = {
            'pointy_cone': (size) => new THREE.ConeGeometry(size * 0.5, size * 1.2, 6 + Math.floor(Math.random() * 5)),
            'round_blob': (size) => new THREE.SphereGeometry(size * 0.6, 12, 8),
            'spinny_torus': (size) => new THREE.TorusGeometry(size * 0.5, size * 0.15, 8, 16),
            'boxy_chunk': (size) => new THREE.BoxGeometry(size, size * (0.8 + Math.random() * 0.4), size * (0.8 + Math.random() * 0.4)),
            'spiky_ball': (size) => new THREE.IcosahedronGeometry(size * 0.7, 0),
            'flat_plate': (size) => new THREE.BoxGeometry(size * 1.5, size * 0.1, size * 1.5),
            'tall_cylinder': (size) => new THREE.CylinderGeometry(size * 0.3, size * 0.3, size * 2.0, 8),
            'knotty_thing': (size) => new THREE.TorusKnotGeometry(size * 0.4, size * 0.1, 50, 8),
            'gem_shape': (size) => new THREE.OctahedronGeometry(size * 0.6, 0),
            'basic_tetra': (size) => new THREE.TetrahedronGeometry(size * 0.7, 0),
            'squashed_ball': (size) => new THREE.SphereGeometry(size * 0.6, 12, 8).scale(1, 0.6, 1),
            'holed_box': (size) => { // Example of more complex primitive combo
                 const shape = new THREE.Shape();
                 shape.moveTo(-size/2, -size/2); shape.lineTo(-size/2, size/2); shape.lineTo(size/2, size/2); shape.lineTo(size/2, -size/2); shape.closePath();
                 const hole = new THREE.Path();
                 hole.absellipse(0, 0, size*0.2, size*0.2, 0, Math.PI*2, false);
                 shape.holes.push(hole);
                 return new THREE.ExtrudeGeometry(shape, {depth: size*0.3, bevelEnabled: false});
            }
        };
        const shapeKeys = Object.keys(SHAPE_GENERATORS);

        // --- Game Data (Page-Based) ---
        const gameData = {
            1: {
                title: "The Giggling Grove",
                content: "<p>You stand at the edge of a forest filled with wobbly, colorful trees. A path leads deeper in. What strange shapes will you find?</p>",
                options: [ { text: "Follow the Wobbly Path", next: 2 }, { text: "Look for Shiny Things", next: 3 } ],
                assemblyParams: {
                    baseShape: 'ground_grass', baseSize: 30,
                    mainShapes: ['tall_cylinder', 'squashed_ball'],
                    accents: ['pointy_cone'],
                    count: 25, scaleRange: [0.8, 2.5],
                    colorTheme: [MAT.wood_light, MAT.leaf_green, MAT.bright_yellow],
                    arrangement: 'scatter'
                }
            },
            2: {
                 title: "Deeper in the Grove",
                 content: "<p>The trees here have funny faces carved into them! One seems to be hiding something behind it.</p>",
                 options: [ { text: "Peek behind the tree", next: 4 }, { text: "Keep going wobble-ward", next: 5 }, { text: "Head back", next: 1 } ],
                 assemblyParams: {
                     baseShape: 'ground_dirt', baseSize: 25,
                     mainShapes: ['tall_cylinder', 'basic_tetra', 'round_blob'],
                     accents: ['spiky_ball'],
                     count: 20, scaleRange: [1.0, 3.0],
                     colorTheme: [MAT.wood_dark, MAT.leaf_autumn, MAT.deep_purple],
                     arrangement: 'cluster'
                 }
             },
            3: {
                title: "Shiny Spot",
                content: "<p>Ooh, sparkly! You found a patch where little gem-like shapes are growing out of the ground.</p>",
                options: [ { text: "Try to pick one", next: 6 }, { text: "Go back to the path", next: 1 } ],
                assemblyParams: {
                     baseShape: 'ground_grass', baseSize: 15,
                     mainShapes: ['gem_shape', 'basic_tetra'],
                     accents: ['pointy_cone'],
                     count: 30, scaleRange: [0.2, 0.7],
                     colorTheme: [MAT.crystal_blue, MAT.crystal_red, MAT.stone_grey],
                     arrangement: 'patch', patchPos: {x:0, y:0.1, z:0}, patchRadius: 5
                 }
            },
             4: {
                title: "Tree's Secret",
                content: "<p>Aha! Tucked behind the tree trunk, you found a Sturdy Stick!</p>",
                options: [ { text: "Awesome! Go back.", next: 2 } ],
                reward: { addItem: "Sturdy Stick" },
                 assemblyParams: { // Same as page 2, maybe slightly zoomed?
                     baseShape: 'ground_dirt', baseSize: 25,
                     mainShapes: ['tall_cylinder', 'basic_tetra', 'round_blob'],
                     accents: ['spiky_ball'],
                     count: 20, scaleRange: [1.0, 3.0],
                     colorTheme: [MAT.wood_dark, MAT.leaf_autumn, MAT.deep_purple],
                     arrangement: 'cluster'
                 }
             },
              5: {
                title: "The Wobble End",
                content: "<p>The path ends at a tall tower made of wobbly, stacked blocks! It looks climbable, but tricky.</p>",
                options: [ { text: "Try to climb (TBC)", next: 99 }, { text: "Go back", next: 2 } ],
                 assemblyParams: {
                     baseShape: 'ground_dirt', baseSize: 20,
                     mainShapes: ['boxy_chunk', 'holed_box'],
                     accents: ['spinny_torus'],
                     count: 15, scaleRange: [1.5, 2.5],
                     colorTheme: [MAT.stone_brown, MAT.stone_grey, MAT.metal_rusty],
                     arrangement: 'stack', stackHeight: 8
                 }
             },
             6: {
                 title: "Picked a Gem!",
                 content: "<p>Success! You plucked a pretty crystal from the ground. It feels warm.</p>",
                 options: [ { text: "Cool! Go back.", next: 1 } ],
                 reward: { addItem: "Cave Crystal" }, // Re-use item name
                 assemblyParams: { // Same as page 3
                      baseShape: 'ground_grass', baseSize: 15,
                      mainShapes: ['gem_shape', 'basic_tetra'],
                      accents: ['pointy_cone'],
                      count: 30, scaleRange: [0.2, 0.7],
                      colorTheme: [MAT.crystal_blue, MAT.crystal_red, MAT.stone_grey],
                      arrangement: 'patch', patchPos: {x:0, y:0.1, z:0}, patchRadius: 5
                  }
             },
            99: {
                title: "Adventure Paused!",
                content: "<p>Wow, what an adventure! That's all for now, but maybe more fun awaits another day?</p>",
                options: [ { text: "Start Over?", next: 1 } ],
                assemblyParams: { // Simple ending scene
                    baseShape: 'flat_plate', baseSize: 10,
                    mainShapes: ['basic_tetra'],
                    accents: [],
                    count: 5, scaleRange: [1, 2],
                    colorTheme: [MAT.sky_blue, MAT.bright_yellow],
                    arrangement: 'center_stack', stackHeight: 3
                }
            }
        };

        // --- Core Functions ---

        function initThreeJS() {
            console.log("initThreeJS started.");
            scene = new THREE.Scene();
            scene.background = new THREE.Color(0x2a3a4a); // Match body background
            clock = new THREE.Clock();

            const width = sceneContainer.clientWidth || 1;
            const height = sceneContainer.clientHeight || 1;
            camera = new THREE.PerspectiveCamera(60, width / height, 0.1, 1000);
            camera.position.set(0, 5, 10);
            camera.lookAt(0, 1, 0);

            renderer = new THREE.WebGLRenderer({ antialias: true });
            renderer.setSize(width, height);
            renderer.shadowMap.enabled = true;
            renderer.shadowMap.type = THREE.PCFSoftShadowMap;
            renderer.toneMapping = THREE.ACESFilmicToneMapping;
            renderer.outputColorSpace = THREE.SRGBColorSpace;
            sceneContainer.appendChild(renderer.domElement);

            window.addEventListener('resize', onWindowResize, false);
            setTimeout(onWindowResize, 100);
            animate();
            console.log("initThreeJS finished.");
        }

        function onWindowResize() {
             if (!renderer || !camera || !sceneContainer) return;
             const width = sceneContainer.clientWidth || 1;
             const height = sceneContainer.clientHeight || 1;
             if (width > 0 && height > 0) {
                camera.aspect = width / height;
                camera.updateProjectionMatrix();
                renderer.setSize(width, height);
             }
        }

        function animate() {
            requestAnimationFrame(animate);
            const delta = clock.getDelta();
            const time = clock.getElapsedTime();

            if (currentSceneGroup) {
                currentSceneGroup.traverse(obj => {
                     if (obj.userData.update) obj.userData.update(time, delta);
                 });
            }

            if (renderer && scene && camera) renderer.render(scene, camera);
        }

        function createMesh(geometry, material, pos = {x:0,y:0,z:0}, rot = {x:0,y:0,z:0}, scale = {x:1,y:1,z:1}) {
             const mesh = new THREE.Mesh(geometry, material);
             mesh.position.set(pos.x, pos.y, pos.z);
             mesh.rotation.set(rot.x, rot.y, rot.z);
             mesh.scale.set(scale.x, scale.y, scale.z);
             mesh.castShadow = true; mesh.receiveShadow = true;
             return mesh;
         }

        function setupLighting(type = 'default') {
            currentLights.forEach(light => { if (light.parent) light.parent.remove(light); if(scene.children.includes(light)) scene.remove(light); });
            currentLights = [];

            const ambientLight = new THREE.AmbientLight(0xffffff, 0.6); // Slightly brighter ambient
            scene.add(ambientLight);
            currentLights.push(ambientLight);

            const directionalLight = new THREE.DirectionalLight(0xffffff, 1.0); // Stronger directional
            directionalLight.position.set(8, 15, 10);
            directionalLight.castShadow = true;
            directionalLight.shadow.mapSize.set(1024, 1024);
            directionalLight.shadow.camera.near = 0.5; directionalLight.shadow.camera.far = 50;
            const sb = 20;
            directionalLight.shadow.camera.left = -sb; directionalLight.shadow.camera.right = sb;
            directionalLight.shadow.camera.top = sb; directionalLight.shadow.camera.bottom = -sb;
            directionalLight.shadow.bias = -0.0005;
            scene.add(directionalLight);
            currentLights.push(directionalLight);
        }

        // --- Procedural Assembly Function ---
        function createProceduralAssembly(params) {
            console.log("Creating procedural assembly with params:", params);
            const group = new THREE.Group();
            const {
                baseShape = 'ground_grass', baseSize = 20,
                mainShapes = ['boxy_chunk'], accents = ['pointy_cone'],
                count = 10, scaleRange = [0.5, 1.5],
                colorTheme = [MAT.stone_grey, MAT.wood_light, MAT.leaf_green],
                arrangement = 'scatter', // scatter, cluster, stack, patch, center_stack
                stackHeight = 5, // for stack/center_stack
                clusterRadius = 5, // for cluster
                patchPos = {x:0, y:0, z:0}, patchRadius = 3 // for patch
            } = params;

            // 1. Create Base
            let baseMesh;
            if (baseShape.startsWith('ground_')) {
                baseMesh = createGround(MAT[baseShape] || MAT.ground_grass, baseSize);
                group.add(baseMesh);
            } else {
                const baseGeo = SHAPE_GENERATORS[baseShape] ? SHAPE_GENERATORS[baseShape](baseSize) : new THREE.BoxGeometry(baseSize, 0.2, baseSize);
                baseMesh = createMesh(baseGeo, colorTheme[0] || MAT.stone_grey, {y:0.1});
                baseMesh.receiveShadow = true; baseMesh.castShadow = false;
                group.add(baseMesh);
            }

            // 2. Generate Objects
            const allShapes = [...mainShapes, ...accents];
            let lastY = 0; // For stacking

            for (let i = 0; i < count; i++) {
                const shapeKey = allShapes[Math.floor(Math.random() * allShapes.length)];
                const geoFunc = SHAPE_GENERATORS[shapeKey];
                if (!geoFunc) { console.warn(`Shape generator not found for key: ${shapeKey}`); continue; }

                const scale = scaleRange[0] + Math.random() * (scaleRange[1] - scaleRange[0]);
                const geometry = geoFunc(scale);
                const material = colorTheme[Math.floor(Math.random() * colorTheme.length)];
                const mesh = createMesh(geometry, material);

                // Calculate position based on arrangement
                let position = { x:0, y:0, z:0 };
                geometry.computeBoundingBox();
                const height = (geometry.boundingBox.max.y - geometry.boundingBox.min.y) * mesh.scale.y;

                switch(arrangement) {
                    case 'stack':
                        position.y = lastY + height / 2;
                        position.x = (Math.random() - 0.5) * 0.5; // Slight offset
                        position.z = (Math.random() - 0.5) * 0.5;
                        lastY += height * 0.9; // Stack slightly overlapping
                        if (j > stackHeight) break; // Limit height
                        break;
                    case 'center_stack':
                         position.y = lastY + height / 2;
                         lastY += height * 0.95;
                         if (i >= stackHeight) { // Only stack up to height, then stop adding
                             continue; // Skip adding more meshes if stack is full
                         }
                         break;
                    case 'cluster':
                        const angle = Math.random() * Math.PI * 2;
                        const radius = Math.random() * clusterRadius;
                        position.x = Math.cos(angle) * radius;
                        position.z = Math.sin(angle) * radius;
                        position.y = height / 2; // Place on ground
                        break;
                     case 'patch':
                         const pAngle = Math.random() * Math.PI * 2;
                         const pRadius = Math.random() * patchRadius;
                         position.x = patchPos.x + Math.cos(pAngle) * pRadius;
                         position.z = patchPos.z + Math.sin(pAngle) * pRadius;
                         position.y = (patchPos.y || 0) + height / 2; // Place relative to patch center Y
                         break;
                    case 'scatter':
                    default:
                        position.x = (Math.random() - 0.5) * baseSize * 0.8;
                        position.z = (Math.random() - 0.5) * baseSize * 0.8;
                        position.y = height / 2; // Place on ground
                        break;
                }
                 mesh.position.set(position.x, position.y, position.z);

                // Random rotation
                mesh.rotation.set(
                    Math.random() * (arrangement === 'stack' || arrangement === 'center_stack' ? 0.1 : Math.PI), // Less random rotation if stacked
                    Math.random() * Math.PI * 2,
                    Math.random() * (arrangement === 'stack' || arrangement === 'center_stack' ? 0.1 : Math.PI)
                );

                // Add simple animation (optional)
                if (Math.random() < 0.3) { // 30% chance of animation
                     mesh.userData.rotSpeed = (Math.random() - 0.5) * 0.5; // Slow rotation
                     mesh.userData.update = (time) => {
                         mesh.rotation.y += mesh.userData.rotSpeed * clock.getDelta();
                     };
                }

                group.add(mesh);
            }

            return group;
        }


        // --- Scene Management (Back to Page-Based) ---
        function updateScene(assemblyParams) {
             console.log("updateScene called");
             // Clear previous scene objects
             if (currentSceneGroup) {
                currentSceneGroup.traverse(child => {
                    if (child.isMesh) {
                        if(child.geometry) child.geometry.dispose();
                        // Assume materials in MAT are shared, don't dispose them unless cloned per object
                    }
                });
                scene.remove(currentSceneGroup); // Remove the group from the main scene
                currentSceneGroup = null;
             }
             // Clear previous lights (except maybe a default ambient?)
             setupLighting('default'); // Reset to default lighting for each scene load

             if (!assemblyParams) {
                 console.warn("No assemblyParams provided, creating default scene.");
                 assemblyParams = { baseShape: 'ground_dirt', count: 5 }; // Basic default
             }

             try {
                 currentSceneGroup = createProceduralAssembly(assemblyParams);
                 scene.add(currentSceneGroup); // Add the new group
                 console.log("New scene group added.");
             } catch (error) {
                 console.error("Error creating procedural assembly:", error);
                 // Optionally add an error indicator scene
             }
         }


        // --- Game Logic ---
        function startGame() {
            console.log("startGame called.");
            const defaultChar = {
                name: "Player",
                stats: { hp: 20, maxHp: 20, xp: 0, strength: 10, dexterity: 10, constitution: 10, intelligence: 10, wisdom: 10, charisma: 10 },
                inventory: ["Sturdy Stick"] // Start with an item
            };
            gameState = {
                currentPageId: 1, // Start at page 1
                character: JSON.parse(JSON.stringify(defaultChar))
            };
            renderPage(gameState.currentPageId);
            console.log("startGame finished.");
        }

        function handleChoiceClick(choiceData) {
             console.log("Choice clicked:", choiceData);
             currentMessage = ""; // Clear previous messages

             let nextPageId = parseInt(choiceData.next);
             const currentPageData = gameData[gameState.currentPageId];
             const targetPageData = gameData[nextPageId];

             if (!targetPageData) {
                 console.error(`Invalid next page ID: ${nextPageId}`);
                 currentMessage = `<p class="message message-warning">Oops! That path seems to lead nowhere yet.</p>`;
                 nextPageId = gameState.currentPageId; // Stay on current page on error
             }

              // Apply rewards from *current* page choice before transitioning
              if (currentPageData && currentPageData.options) {
                 const chosenOption = currentPageData.options.find(opt => opt.next === choiceData.next); // Find the specific option clicked
                 if (chosenOption && chosenOption.reward) {
                    console.log("Applying reward:", chosenOption.reward);
                      if(chosenOption.reward.addItem && itemsData[chosenOption.reward.addItem]) {
                          if (!gameState.character.inventory.includes(chosenOption.reward.addItem)) {
                              gameState.character.inventory.push(chosenOption.reward.addItem);
                              currentMessage += `<p class="message message-item">You found a ${chosenOption.reward.addItem}!</p>`;
                          } else {
                               currentMessage += `<p class="message message-info">You found another ${chosenOption.reward.addItem}, but can't carry more.</p>`;
                          }
                      }
                      if(chosenOption.reward.xp) {
                          gameState.character.stats.xp += chosenOption.reward.xp;
                          currentMessage += `<p class="message message-xp">You gained ${chosenOption.reward.xp} XP!</p>`;
                      }
                      // Add stat gain logic here if needed for specific rewards
                 }
             }


             gameState.currentPageId = nextPageId;
             renderPage(nextPageId);
        }

        function renderPage(pageId) {
             console.log(`Rendering page ${pageId}`);
             const pageData = gameData[pageId];

             if (!pageData) {
                 console.error(`No page data for ID: ${pageId}`);
                 storyTitleElement.textContent = "Lost!";
                 storyContentElement.innerHTML = currentMessage + `<p>Somehow you've wandered off the map!</p>`;
                 choicesElement.innerHTML = `<button class="choice-button" onclick="handleChoiceClick({ next: 1 })">Go Back to Start</button>`;
                 updateStatsDisplay(); updateInventoryDisplay(); updateActionInfo();
                 updateScene({ baseShape: 'ground_dirt', count: 1, mainShapes: ['basic_tetra'], colorTheme: [MAT.metal_rusty] }); // Simple error scene
                 return;
             }

             storyTitleElement.textContent = pageData.title || "An Unnamed Place";
             storyContentElement.innerHTML = currentMessage + (pageData.content || "<p>...</p>");
             choicesElement.innerHTML = ''; // Clear previous choices

             if (pageData.options && pageData.options.length > 0) {
                 pageData.options.forEach(option => {
                     const button = document.createElement('button');
                     button.classList.add('choice-button');
                     button.textContent = option.text;
                     button.onclick = () => handleChoiceClick(option); // Pass the whole option
                     choicesElement.appendChild(button);
                 });
             } else {
                 // Maybe always add a restart button if no other options?
                 const button = document.createElement('button');
                 button.classList.add('choice-button');
                 button.textContent = "The End (Restart?)";
                 button.onclick = () => handleChoiceClick({ next: 1 });
                 choicesElement.appendChild(button);
             }

             updateStatsDisplay();
             updateInventoryDisplay();
             updateActionInfo();
             updateScene(pageData.assemblyParams); // Update 3D scene based on page params
        }
        // Make handleChoiceClick globally accessible for inline handlers
        window.handleChoiceClick = handleChoiceClick;

        function updateStatsDisplay() {
            if (!gameState.character || !statsElement) return;
            const stats = gameState.character.stats;
            const hpColor = stats.hp / stats.maxHp < 0.3 ? '#f88' : (stats.hp / stats.maxHp < 0.6 ? '#fd5' : '#8f8');

            statsElement.innerHTML = `<strong>Stats:</strong>
                <span style="color:${hpColor}">HP: ${stats.hp}/${stats.maxHp}</span> <span>XP: ${stats.xp}</span><br>
                <span>Str: ${stats.strength}</span> <span>Dex: ${stats.dexterity}</span> <span>Con: ${stats.constitution}</span>
                <span>Int: ${stats.intelligence}</span> <span>Wis: ${stats.wisdom}</span> <span>Cha: ${stats.charisma}</span>`;
        }

        function updateInventoryDisplay() {
             if (!gameState.character || !inventoryElement) return;
             let invHtml = '<strong>Inventory:</strong> ';
             if (gameState.character.inventory.length === 0) {
                 invHtml += '<em>Empty</em>';
             } else {
                 gameState.character.inventory.forEach(item => {
                     const itemDef = itemsData[item] || { type: 'unknown', description: '???' };
                     const itemClass = `item-${itemDef.type || 'unknown'}`;
                     invHtml += `<span class="item-tag ${itemClass}" title="${itemDef.description}">${item}</span>`;
                 });
             }
             inventoryElement.innerHTML = invHtml;
        }

        function updateActionInfo() {
             if (!actionInfoElement || !gameState ) return;
             actionInfoElement.textContent = `Location: ${gameData[gameState.currentPageId]?.title || 'Unknown'}`;
        }


        // --- Initialization ---
        document.addEventListener('DOMContentLoaded', () => {
             console.log("DOM Ready - Initializing Wacky Shapes Adventure.");
             try {
                 initThreeJS();
                 if (!scene || !camera || !renderer) throw new Error("Three.js failed to initialize.");
                 startGame(); // Start game directly
                 console.log("Game world initialized and started.");
             } catch (error) {
                 console.error("Initialization failed:", error);
                 storyTitleElement.textContent = "Initialization Error";
                 storyContentElement.innerHTML = `<p style="color:red;">Failed to start game:</p><pre style="color:red; white-space: pre-wrap;">${error.stack || error}</pre><p style="color:yellow;">Check console (F12) for details.</p>`;
                 if(sceneContainer) sceneContainer.innerHTML = '<p style="color:red; padding: 20px;">3D Scene Failed</p>';
                  document.getElementById('stats-inventory-container').style.display = 'none';
                  document.getElementById('choices-container').style.display = 'none';
             }
         });

    </script>
</body>
</html>