-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
288 lines (257 loc) · 6.84 KB
/
main.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
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
-- require('perlin')
require('catui')
local colors = require('colors')
local map = {}
map.sizeX = 1000
map.sizeY = 1000
map.tileSize = 1
map.levels = {
{
name = 'Deep Water',
height = 0.3,
color = {20, 27, 122}
},
{
name = 'Shallow Water',
height = 0.4,
color = {}
},
{
name = 'Sand',
height = 0.45,
color = {}
},
{
name = 'Grass',
height = 0.55,
color = {}
},
{
name = 'Forest',
height = 0.6,
color = {}
},
{
name = 'Rock',
height = 0.7,
color = {}
},
{
name = 'Steep Rock',
height = 0.9,
color = {}
},
{
name = 'Snow',
height = 1,
color = {}
}
}
function mapVal(x_in, in_min, in_max, out_min, out_max)
return ((x_in - in_min) * (out_max - out_min)) / ((in_max - in_min) + out_min)
end
function generateNoiseMap(width, height, seed, scale, octaves, persistance, lacunarity)
if scale <= 0 then
scale = 0.0001
end
local rng = love.math.newRandomGenerator(seed)
local ox = rng:random(100000)
local oy = rng:random(100000)
local lowest = 0
local highest = 0
-- generate noise
local noiseMap = {}
for x = 1, width do
noiseMap[x] = {}
for y = 1, height do
local amplitude = 1
local frequency = 1
local noiseHeight = 0
for o = 1, octaves do
local sampleX = ((ox + x + rng:random()) / scale) * frequency
local sampleY = ((oy + y + rng:random()) / scale) * frequency
local noiseVal = (love.math.noise(sampleX, sampleY) * 2) - 1
noiseHeight = noiseHeight + (noiseVal * amplitude)
amplitude = amplitude * persistance
frequency = frequency * lacunarity
end
if noiseHeight < lowest then
lowest = noiseHeight
elseif noiseHeight > highest then
highest = noiseHeight
end
noiseMap[x][y] = noiseHeight
end
end
for x = 1, width do
for y = 1, height do
noiseMap[x][y] = mapVal(noiseMap[x][y], lowest, highest, 0, 1)
end
end
print('Highest: ' .. highest)
print('Lowest: ' .. lowest)
return noiseMap
end
function lerp(start, ending, amt)
return (1 - amt) * start + amt * ending
end
function createButton(btnConfig)
local btn = UIButton:new()
btn:setPos(btnConfig.x, btnConfig.y)
btn:setSize(btnConfig.w, btnConfig.h)
btn:setText(btnConfig.text)
btn:setAnchor(0, 0)
btnConfig.parent:addChild(btn)
return btn
end
function createLabel(lblConfig)
local lbl = UILabel:new(lblConfig.font or 'font/visat.ttf', tostring(lblConfig.text), lblConfig.fontSize or 12)
lbl:setPos(lblConfig.x, lblConfig.y)
lbl:setSize(lblConfig.w, lblConfig.h)
lbl:setText(lblConfig.text)
lbl:setAnchor(lblConfig.ax or 0, lblConfig.ay or 0)
lbl:setAutoSize(lblConfig.autoSize or false)
lbl:setFontColor(lblConfig.fontColor or {1, 1, 1, 1})
lblConfig.parent:addChild(lbl)
return lbl
end
function createInput(inConfig)
local inp = UIEditText:new()
inp:setPos(inConfig.x, inConfig.y)
inp:setSize(inConfig.w, inConfig.h)
inp:setText(tostring(inConfig.text))
inConfig.parent:addChild(inp)
return inp
end
function love.load()
-- local noiseMap = {}
-- perlin:load()
noiseMap = generateNoiseMap(map.sizeX, map.sizeY, 11234, 140, 4, 0.4, 2)
mgr = UIManager:getInstance()
local content = UIContent:new()
content:setPos(love.graphics.getWidth() - 150, 0)
content:setSize(150, love.graphics.getHeight())
content:setContentSize(150, love.graphics.getHeight())
mgr.rootCtrl.coreContainer:addChild(content)
local theme = {
height = 20
}
local btnGenerate =
createButton(
{
x = 10,
y = 50,
w = 100,
h = theme.height,
text = 'Generate',
parent = content
}
)
local mapConfig =
createLabel(
{
x = 10,
y = 0,
w = 100,
h = theme.height,
text = 'Map Config',
parent = content
}
)
local mapConfigXLabel =
createLabel(
{
x = 10,
y = 25,
w = 10,
h = theme.height,
text = 'X:',
parent = content
}
)
local mapConfigX =
createInput(
{
x = 35,
y = 25,
w = 40,
h = theme.height,
text = map.sizeX,
parent = content
}
)
local mapConfigYLabel =
createLabel(
{
x = 75,
y = 25,
w = 10,
h = theme.height,
text = 'Y:',
parent = content
}
)
local mapConfigY =
createInput(
{
x = 100,
y = 25,
w = 40,
h = theme.height,
text = map.sizeY,
parent = content
}
)
-- local MapEditSizeX = UIEditText:new()
-- MapEditSizeX:setPos(100, 50)
-- MapEditSizeX:setSize(35, 25)
-- MapEditSizeX:setText(tostring(map.sizeX))
-- content:addChild(MapEditSizeX)
-- mgr.rootCtrl.coreContainer:addChild(MapEditSizeX)
-- local MapEditSizeY = UIEditText:new()
-- MapEditSizeY:setPos(60, 50)
-- MapEditSizeY:setSize(35, 25)
-- MapEditSizeY:setText(tostring(map.sizeY))
-- content:addChild(MapEditSizeY)
-- mgr.rootCtrl.coreContainer:addChild(MapEditSizeY)
-- x = 1
-- y = 1
-- noiseMap[x][y] = noise:noise(1,1,0)
-- noise:noise(139,254,0)
end
function love.update(dt)
mgr:update(dt)
end
function love.draw()
for x = 1, #noiseMap do
for y = 1, #noiseMap[x] do
-- local colorVal = mapVal(noiseMap[x][y], lowest, highest, 0, 1)
local colorVal = noiseMap[x][y]
love.graphics.setColor(colorVal, colorVal, colorVal)
-- print('NoiseMap - X:' .. x .. ' Y: ' .. y .. ' : ' .. noiseMap[x][y])
love.graphics.rectangle('fill', (x - 1) * map.tileSize, (y - 1) * map.tileSize, map.tileSize, map.tileSize)
end
end
mgr:draw()
end
function love.mousemoved(x, y, dx, dy)
mgr:mouseMove(x, y, dx, dy)
end
function love.mousepressed(x, y, button, isTouch)
mgr:mouseDown(x, y, button, isTouch)
end
function love.mousereleased(x, y, button, isTouch)
mgr:mouseUp(x, y, button, isTouch)
end
function love.keypressed(key, scancode, isrepeat)
mgr:keyDown(key, scancode, isrepeat)
end
function love.keyreleased(key)
mgr:keyUp(key)
end
function love.wheelmoved(x, y)
mgr:whellMove(x, y)
end
function love.textinput(text)
mgr:textInput(text)
end