about summary refs log tree commit diff stats
path: root/html/text-world/js/worldgen.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/text-world/js/worldgen.js')
-rw-r--r--html/text-world/js/worldgen.js130
1 files changed, 130 insertions, 0 deletions
diff --git a/html/text-world/js/worldgen.js b/html/text-world/js/worldgen.js
new file mode 100644
index 0000000..c8f5778
--- /dev/null
+++ b/html/text-world/js/worldgen.js
@@ -0,0 +1,130 @@
+'use strict';
+
+const WorldGenerator = {
+    generate() {
+        let world = World.create();
+        
+        // Create player
+        let result = WorldGenerator.createPlayer(world);
+        world = result.world;
+        const playerId = result.entityId;
+
+        // Generate environments
+        world = WorldGenerator.generateEnvironments(world);
+
+        return { world, playerId };
+    },
+
+    createPlayer(world) {
+        const newWorld = World.createEntity(world);
+        const entityId = newWorld.nextEntityId - 1;
+        
+        return {
+            world: World.addComponent(
+                World.addComponent(
+                    World.addComponent(
+                        newWorld,
+                        entityId,
+                        new Position(0, 0)
+                    ),
+                    entityId,
+                    new Inventory([])
+                ),
+                entityId,
+                new Messages([])
+            ),
+            entityId
+        };
+    },
+
+    generateEnvironments(world) {
+        let newWorld = world;
+        
+        for (let x = 0; x < 10; x++) {
+            for (let y = 0; y < 10; y++) {
+                newWorld = WorldGenerator.createEnvironment(newWorld, x, y);
+            }
+        }
+        
+        return newWorld;
+    },
+
+    createEnvironment(world, x, y) {
+        const newWorld = World.createEntity(world);
+        const entityId = newWorld.nextEntityId - 1;
+        
+        // Add basic components
+        let updatedWorld = World.addComponent(
+            World.addComponent(
+                newWorld,
+                entityId,
+                new Position(x, y)
+            ),
+            entityId,
+            this.generateEnvironmentDescription(x, y)
+        );
+
+        // Add items based on environment type
+        const items = this.generateItems(x, y);
+        items.forEach(item => {
+            const itemEntity = World.createEntity(updatedWorld);
+            updatedWorld = World.addComponent(
+                World.addComponent(
+                    updatedWorld,
+                    itemEntity,
+                    new Position(x, y)
+                ),
+                itemEntity,
+                item
+            );
+        });
+
+        return updatedWorld;
+    },
+
+    generateEnvironmentDescription(x, y) {
+        const environments = [
+            {
+                short: "Forest Clearing",
+                long: "A peaceful clearing surrounded by tall trees. Sunlight filters through the canopy above.",
+                explorable: {
+                    "trees": "The trees here are ancient oaks, their bark rough and weathered.",
+                    "ground": "The forest floor is covered in soft moss and fallen leaves."
+                }
+            },
+            {
+                short: "Rocky Outcrop",
+                long: "A rocky area with large boulders scattered about. Some seem to hide small caves.",
+                explorable: {
+                    "boulders": "The boulders are covered in colorful lichens.",
+                    "caves": "Small openings that could shelter wildlife."
+                }
+            }
+        ];
+
+        const index = (x * 7 + y * 13) % environments.length;
+        return new Description(
+            environments[index].short,
+            environments[index].long,
+            environments[index].explorable
+        );
+    },
+
+    generateItems(x, y) {
+        const items = [
+            new Item("mushroom", "A small, colorful mushroom that might be edible."),
+            new Item("stone", "A smooth, flat stone that catches your eye."),
+            new Item("stick", "A sturdy wooden stick."),
+            new Item("flower", "A beautiful wildflower with vibrant petals."),
+            new Item("feather", "A delicate feather from some unknown bird.")
+        ];
+
+        // Use coordinates to deterministically generate items
+        const numItems = ((x * 3 + y * 5) % 3) + 1; // 1-3 items per location
+        const startIndex = (x * 7 + y * 11) % items.length;
+        
+        return Array.from({ length: numItems }, (_, i) => 
+            items[(startIndex + i) % items.length]
+        );
+    }
+};