-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain.lua
109 lines (98 loc) · 3.23 KB
/
terrain.lua
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
local base = require("chunk")
local objects = require("objects")
local terrain = setmetatable({}, {__index = base})
terrain.x = 0
terrain.world = nil
terrain.lines = {}
terrain.perlin = setmetatable({}, {__index = require("lib/perlin")})
local flag = false
--[[ Callback ]]
function terrain:generateChunk(chunk, dt)
self:generateVerticesAt(chunk)
self:generateLinesAt(chunk)
end
function terrain:draw()
local chunk = self:getChunk(self.x)
local index, object
local chunkIndex
local firstChunk = math.max(chunk - self.halfDrawRange, 1)
local lastChunk = chunk + self.halfDrawRange
for chunkIndex = firstChunk, lastChunk do
if self.lines[chunkIndex] ~= nil then
for index, object in ipairs(self.lines[chunkIndex]) do
object:draw()
end
end
end
end
-- [[ Utility ]]
function terrain:generateVerticesAt(chunk)
print("Starting vertices", chunk)
if chunk < 1 then return end
local x = self:getX(chunk)
local index
for index = x, x+self.chunkSize, self.spacing do
self:setVertex(index, chunk)
end
end
function terrain:setVertex(x, chunk)
if chunk < 1 then return end
if self[chunk] == nil then self[chunk] = {} end
local index = self:getChunkIndex(x, chunk)
if self[chunk][index] ~= nil then return end
table.insert(self[chunk], index, self.perlin:fbm(x) * self.scale)
--[[print("Vertex", x,
"Chunk", chunk,
"Index", index,
"getn", table.getn(self[chunk]))]]
end
function terrain:generateLinesAt(chunk)
print("Starting lines", chunk)
if self.world == nil then return end
if chunk < 1 or self[chunk] == nil then return end
local index, object
local lastObject, lastIndex
local from, to
for index, object in ipairs(self[chunk]) do
if index > 1 then
if self.lines[chunk] == nil then self.lines[chunk] = {} end
if self.lines[chunk][lastIndex] == nil then
from = (lastIndex + (chunk - 1) * self.minimumVerticesPerChunk) * self.spacing - self.spacing -- Do not use minimumVerticesPerChunk
to = (index + (chunk - 1) * self.minimumVerticesPerChunk) * self.spacing - self.spacing -- Do not use minimumVerticesPerChunk
table.insert(self.lines[chunk], lastIndex,
self:terrainLine(self.world, from, lastObject, to, object)
)
--[[print("Line", from, to,
"Chunk", chunk,
"Index", lastIndex,
"getn", table.getn(self.lines[chunk]))]]
end
end
lastIndex = index
lastObject = object
end
end
--[[ Objects ]]
function terrain:terrainLine(world, x1, y1, x2, y2)
local o = setmetatable({}, {__index = objects.baseObject()})
local body = love.physics.newBody(world, x1, y1, "static")
x1, y1 = body:getLocalPoint(x1, y1)
x2, y2 = body:getLocalPoint(x2, y2)
o.shape = love.physics.newEdgeShape(x1, y1, x2, y2)
o.fixture = love.physics.newFixture(body, o.shape, 1)
o.fixture:setUserData(o)
o.color = {0.4, 1, 0.4, 1}
function o:draw()
local points = {}
for _, point in ipairs({self.fixture:getBody():getWorldPoints(self.shape:getPoints())}) do
table.insert(points, point)
end
if table.getn(points) >= 4 then
love.graphics.setColor(unpack(self.color))
love.graphics.line(unpack(points))
love.graphics.setColor(1,1,1,1)
end
end
return o
end
return terrain