aboutsummaryrefslogtreecommitdiffstats
path: root/Scripts/world.lua
blob: 3b56d9ae9ef86c1f8e1aa3c11d201188eeea8952 (plain)
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
world = {
    Seed = 5345345,
    Layers = 2,

    -- This is run when the world is registered and not after,
    -- although it is possible to register materials later
    Register = function(self)

        -- TODO make world have global textures to speed up rendering

        self:registerMaterial("grass", {
            -- TODO combine both of these into 1
            texture = {
                file = "Assets/world.png",
                offset = { x = 0, y = 0 },
                size = { x = 64, y = 64 }
            },
            normal = {
                file = "Assets/world_normal.png",
                offset = { x = 0, y = 0 },
                size = { x = 64, y = 64 }
            }
        });
        self:registerMaterial("dirt", {
            texture = {
                file = "Assets/world.png",
                offset = { x = 64, y = 0 },
                size = { x = 64, y = 64 }
            },
            normal = {
                file = "Assets/world_normal.png",
                offset = { x = 64, y = 0 },
                size = { x = 64, y = 64 }
            }
        });
        self:registerMaterial("stone", {
            texture = {
                file = "Assets/world.png",
                offset = { x = 128, y = 0 },
                size = { x = 64, y = 64 }
            },
            normal = {
                file = "Assets/world_normal.png",
                offset = { x = 128, y = 0 },
                size = { x = 64, y = 64 }
            }
        });
        self:registerMaterial("flower", {
            texture = {
                file = "Assets/world.png",
                offset = { x = 192, y = 0 },
                size = { x = 64, y = 64 }
            },
            normal = {
                file = "Assets/world_normal.png",
                offset = { x = 192, y = 0 },
                size = { x = 64, y = 64 }
            },
            passable = true
        });
        self:registerMaterial("trunk", {
            texture = {
                file = "Assets/world.png",
                offset = { x = 256, y = 0 },
                size = { x = 64, y = 64 }
            },
            normal = {
                file = "Assets/world_normal.png",
                offset = { x = 256, y = 0 },
                size = { x = 64, y = 64 }
            }
        });
    end,

    Generate = function(self)
        math.randomseed(self.Seed)
        xsize, ysize, zsize = self:setSize(250, 128, 3)
        for Z = 0,zsize-1 do
            for X = 0,xsize-1 do
                if Z == 0 then
                    YGen = math.floor(6*math.sin(X/20)) + 64
                elseif Z == 1 then
                    YGen = math.floor(9*math.sin(X/20)) + 64
                else
                    YGen = math.floor(15*math.sin(X/20)) + 64
                end
                YDepth = math.random(3,5)
                for Y = 0,ysize-1 do
                    if Y == YGen then
                        self:setData(X, Y, Z, "grass");
                    elseif Y < YGen and Y > (YGen - YDepth) then
                        self:setData(X, Y, Z, "dirt");
                    elseif Y < YGen then
                        self:setData(X, Y, Z, "stone");
                    elseif Y == YGen + 1 then
                        if math.random(0, 100) == 53 then
                            self:setData(X, Y, Z, "flower");
                        elseif math.random(0, 100) == 45 then
                            self:setData(X, Y, Z, "trunk");
                        end
                    end
                    --print(X..","..Y..","..Z);
                end
            end
        end
        self:setData(1000, 1345, 5, "grass"); -- Test error checking
        print("Done with world gen");
    end
}

--world:Generate()
game.worldRegister(world)