about summary refs log tree commit diff stats
path: root/html/text-world/js/worldgen.js
blob: c8f5778d5131b628fa0ed015c5d0aa1038f1f77e (plain) (blame)
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
'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]
        );
    }
};