-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.lua
301 lines (268 loc) · 9.73 KB
/
world.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
289
290
291
292
293
294
295
296
297
298
299
300
301
world = {
server = {},
client = {}
}
local chunkSize = 8
-- max tiles visible (ceil(x)+1) + sides for blending (+2)
local tileIdW, tileIdH = math.ceil(gsx/15) + 1 + 2, math.ceil(gsy/15) + 1 + 2
shaders.mapRender:send('tileIdRes', {tileIdW, tileIdH})
for _, sc in ipairs{'server', 'client'} do
world[sc].newDefaults = function()
local t = {
chunks = {},
chunkSize = chunkSize
}
if sc == 'server' then
t.chunkCanvas = love.graphics.newCanvas(chunkSize, chunkSize)
elseif sc == 'client' then
t.chunkImages = {}
t.chunkIdImages = {}
t.tileColors = {
{41, 137, 214}, -- water
{251, 228, 125}, -- sand
{98, 195, 116}, -- grass
{98, 98, 98}, -- rock
{205, 140, 79}, -- path
{183, 163, 43}, -- floor
{104, 88, 0}, -- wall
{73, 73, 73}, -- platform
{73, 73, 73} -- platform
}
for _, color in ipairs(t.tileColors) do
for i=1, 3 do
color[i] = color[i]/255
end
end
t.tileIdCanvas = love.graphics.newCanvas(tileIdW, tileIdH)
end
return t
end
world[sc].new = function(self, o)
o = o or {}
for k, v in pairs(self.newDefaults()) do
if o[k] == nil then o[k] = v end
end
setmetatable(o, self)
self.__index = self
return o
end
end
function world.server:getChunk(x, y)
if self.chunks[x] and self.chunks[x][y] then
return self.chunks[x][y]
else
if self.chunks[x] == nil then self.chunks[x] = {} end
local _canvas = love.graphics.getCanvas()
local _shader = love.graphics.getShader()
love.graphics.setCanvas(self.chunkCanvas)
love.graphics.clear()
love.graphics.setShader(shaders.mapGen)
shaders.mapGen:send('camPos', {x*self.chunkSize, y*self.chunkSize})
love.graphics.push()
love.graphics.origin()
love.graphics.rectangle('fill', 0, 0, self.chunkSize, self.chunkSize)
love.graphics.pop()
love.graphics.setCanvas(_canvas)
love.graphics.setShader(_shader)
local imageData = self.chunkCanvas:newImageData()
self.chunks[x][y] = {}
local chunk = self.chunks[x][y]
for i=1, self.chunkSize do
if chunk[i] == nil then chunk[i] = {} end
for j=1, self.chunkSize do
local v = imageData:getPixel(i-1, j-1)
v = lume.round(v*255)
chunk[i][j] = v
end
end
return chunk
end
end
function world.server:getTile(x, y)
local tx = math.floor(x/15)
local ty = math.floor(y/15)
local cx = math.floor(tx/self.chunkSize)
local cy = math.floor(ty/self.chunkSize)
tx = tx - cx*self.chunkSize + 1
ty = ty - cy*self.chunkSize + 1
return self:getChunk(cx, cy)[tx][ty]
end
function world.server:destroy()
self.chunks = {}
end
function world.client:setChunk(x, y, chunk)
if self.chunks[x] == nil then self.chunks[x] = {} end
self.chunks[x][y] = chunk
local imageData = love.image.newImageData(self.chunkSize, self.chunkSize)
local idImageData = love.image.newImageData(self.chunkSize, self.chunkSize)
for i=1, self.chunkSize do
for j=1, self.chunkSize do
local v = chunk[i][j]
imageData:setPixel((i-1), (j-1), unpack(self.tileColors[v] or {0,0,0}))
idImageData:setPixel((i-1), (j-1), v/255, 0, 0)
end
end
if self.chunkImages[x] == nil then self.chunkImages[x] = {} end
self.chunkImages[x][y] = love.graphics.newImage(imageData)
if self.chunkIdImages[x] == nil then self.chunkIdImages[x] = {} end
self.chunkIdImages[x][y] = love.graphics.newImage(idImageData)
end
function world.client:getTile(x, y)
local tx = math.floor(x/15)
local ty = math.floor(y/15)
local cx = math.floor(tx/self.chunkSize)
local cy = math.floor(ty/self.chunkSize)
tx = tx - cx*self.chunkSize + 1
ty = ty - cy*self.chunkSize + 1
if self.chunks[cx] and self.chunks[cx][cy] then
return self.chunks[cx][cy][tx][ty]
end
-- have to wait for server to send
end
function world.client:destroy()
self.chunks = {}
end
function world.client:update(dt)
-- get visible chunks (+ pad) if nil
local p = playerController.player
local cx1 = math.floor((p.x - 465)/15/self.chunkSize) - 1
local cx2 = math.floor((p.x + 465)/15/self.chunkSize) + 1
local cy1 = math.floor((p.y - 465)/15/self.chunkSize) - 1
local cy2 = math.floor((p.y + 465)/15/self.chunkSize) + 1
for cx=cx1, cx2 do
for cy=cy1, cy2 do
if not self.chunks[cx] or not self.chunks[cx][cy] then
client.nutClient:sendRPC('getWorldChunk', bitser.dumps{x=cx, y=cy})
end
end
end
end
function world.client:draw()
local _canvas = love.graphics.getCanvas()
local _shader = love.graphics.getShader()
-- set id canvas
love.graphics.setCanvas(self.tileIdCanvas)
love.graphics.clear()
love.graphics.setShader()
love.graphics.setColor(1, 1, 1)
love.graphics.push()
love.graphics.origin()
-- visible tile (+ blend pad) chunks
local tx1 = math.floor((camera.x - camera.ssx/2)/15) - 1
local tx2 = math.floor((camera.x + camera.ssx/2)/15) + 1
local ty1 = math.floor((camera.y - camera.ssy/2)/15) - 1
local ty2 = math.floor((camera.y + camera.ssy/2)/15) + 1
local cx1 = math.floor(tx1/self.chunkSize)
local cx2 = math.floor(tx2/self.chunkSize)
local cy1 = math.floor(ty1/self.chunkSize)
local cy2 = math.floor(ty2/self.chunkSize)
local idImgs = self.chunkIdImages
for cx=cx1, cx2 do
for cy=cy1, cy2 do
if idImgs[cx] and idImgs[cx][cy] then
love.graphics.draw(idImgs[cx][cy], cx*self.chunkSize - tx1, cy*self.chunkSize - ty1)
end
end
end
love.graphics.pop()
love.graphics.setCanvas(_canvas)
-- draw world
love.graphics.setShader(shaders.mapRender)
shaders.mapRender:send('tileIds', self.tileIdCanvas)
shaders.mapRender:send('camPos', {
camera.x - camera.ssx/2,
camera.y - camera.ssy/2
})
shaders.mapRender:send('tilemapPos', {
math.floor((camera.x - camera.ssx/2)/15) - 2,
math.floor((camera.y - camera.ssy/2)/15) - 2,
})
shaders.mapRender:send('time', time)
love.graphics.setColor(1, 1, 1)
love.graphics.push()
love.graphics.origin()
love.graphics.rectangle('fill', 0, 0, gsx, gsy)
love.graphics.pop()
love.graphics.setShader(_shader)
-- test shader validity
if false then
love.graphics.setColor(1, 1, 1, 0.4)
for cx, chunkCol in pairs(self.chunkImages) do -- chunks[x]
for cy, img in pairs(chunkCol) do
love.graphics.draw(img, cx*self.chunkSize*15, cy*self.chunkSize*15, 0, 15, 15)
end
end
end
end
function world.client:drawMap(mapSize)
local _canvas = love.graphics.getCanvas()
local _shader = love.graphics.getShader()
-- todo: 2x
if mapSize == nil then mapSize = 'full' end
local x, y = 0, 0
local w, h = gsx, gsy
love.graphics.setCanvas{canvases.game, stencil=true}
if mapSize == 'mini' then
x = hud.mapPanel.x + 7
y = hud.mapPanel.y + 8
w, h = 60, 62
end
local p = playerController.player
local px = math.floor(lume.round(p.body:getX())/15)
local py = math.floor(lume.round(p.body:getY())/15)
-- bg
love.graphics.setColor(0.1, 0.1, 0.1)
love.graphics.rectangle('fill', x, y, w, h)
-- tiles
love.graphics.stencil(function()
love.graphics.rectangle('fill', x, y, w, h)
end, 'replace', 1)
love.graphics.setStencilTest('greater', 0)
love.graphics.setColor(1, 1, 1)
local cs = self.chunkSize
for cx=math.floor((px - w/2)/cs), math.floor((px + w/2)/cs) do
for cy=math.floor((py - h/2)/cs), math.floor((py + h/2)/cs) do
local img = safeIndex(self.chunkImages, cx, cy)
if img then
local dx = cx*cs - px
local dy = cy*cs - py
love.graphics.draw(img, x + math.floor(w/2) + dx, y + math.floor(h/2) + dy)
end
end
end
love.graphics.setStencilTest()
-- entities/players
love.graphics.setCanvas(canvases.tempGame)
love.graphics.clear()
for _, v in pairs(client.currentState.entities) do
if not v.destroyed and v.id ~= playerController.serverId and v.id ~= playerController.player.id
and (v.type == 'player' or v.enemy) then
if v.type == 'player' then
love.graphics.setColor(255/255, 216/255, 0/255)
else
love.graphics.setColor(1, 0, 0)
end
local ex = math.floor(lume.round(v.x)/15)
local ey = math.floor(lume.round(v.y)/15)
local dx = ex - px
local dy = ey - py
if math.abs(dx) <= math.floor(w/2) and math.abs(dy) <= math.floor(h/2) then
love.graphics.rectangle('fill', x + math.floor(w/2) + dx, y + math.floor(h/2) + dy, 1, 1)
end
end
end
-- local player
love.graphics.setColor(1, 1, 0)
love.graphics.rectangle('fill', x + math.floor(w/2), y + math.floor(h/2), 1, 1)
-- ent/player outline
love.graphics.setCanvas(_canvas)
love.graphics.setShader(shaders.outline)
shaders.outline:send('stepSize', {
1/canvases.tempGame:getWidth(),
1/canvases.tempGame:getHeight()
})
shaders.outline:send('outlineColor', {0, 0, 0, 1})
love.graphics.setColor(1, 1, 1)
love.graphics.draw(canvases.tempGame, 0, 0)
love.graphics.setShader(_shader)
end