-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.lua
More file actions
363 lines (327 loc) · 10.9 KB
/
map.lua
File metadata and controls
363 lines (327 loc) · 10.9 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
-- map.lua
local entity = require "entity"
local state = require "state"
local event = require "event"
local time = require "time"
local M = {}
local loadEntities = function (map, objectgroup)
for _,object in ipairs(map.layers[objectgroup].objects) do
local position = { x = object.x, y = object.y }
object.position = position
entity.new(object)
end
end
M.new = function(arg)
local map = nil
_,map = pcall(arg.data)
local tilelayer = nil
local objectgroup = nil
local object = {}
-- find layers
for i,layer in ipairs(map.layers) do
if layer.type == "tilelayer" then
tilelayer = i
elseif layer.type == "objectgroup" then
objectgroup = i
end
end
-- map properties
--[[for k,v in pairs(map.properties) do
local property = assert(loadstring(v))
_,map.properties[k] = pcall("return " .. property)
print(map.properties[k])
end]]
object.properties = function ()
return map.properties
end
-- tileset properties
for _,tileset in ipairs(map.tilesets) do
local tiles = {}
for _,tile in ipairs(tileset.tiles) do
tiles[tile.id + 1] = tile.properties
end
tileset.tiles = tiles
end
-- tile data
local tiledata = {}
local loadTiledata = function ()
for x=1,map.layers[tilelayer].width do
tiledata[x] = {}
for y=1,map.layers[tilelayer].height do
tiledata[x][y] = map.layers[tilelayer].data[x + ((y - 1) * map.layers[tilelayer].width)]
event.notify("tileCreation", {x=x, y=y})
end
end
end
loadEntities(map, objectgroup)
-- render info
local tileset = {}
tileset.image = love.graphics.newImage("game/"..map.tilesets[1].image)
tileset.image:setFilter("linear", "linear")
tileset.quads = {}
local max_tiles = map.tilesets[tilelayer].tilewidth * map.tilesets[1].tileheight
local tiles_x = map.tilesets[tilelayer].imagewidth / map.tilesets[1].tilewidth
for i=1,max_tiles do
tileset.quads[i] = love.graphics.newQuad(
((i - 1) % tiles_x) * map.tilesets[tilelayer].tilewidth,
math.floor((i - 1) / tiles_x) * map.tilesets[tilelayer].tileheight,
map.tilesets[tilelayer].tilewidth, map.tilesets[tilelayer].tileheight,
map.tilesets[tilelayer].imagewidth,
map.tilesets[tilelayer].imageheight
)
end
tileset.image:setFilter("nearest","linear")
local tile_batch = love.graphics.newSpriteBatch(tileset.image,
math.ceil(love.graphics.getWidth()/(map.tilesets[tilelayer].tilewidth/5)) *
math.ceil(love.graphics.getHeight()/(map.tilesets[tilelayer].tileheight/5)))
--if map.properties["wrap"] == "true" then
-- setWrap('repeat','repeat')
--else
-- setWrap('clamp','clamp')
--end
-- parallax
local parallax ={}
for i=1,3 do
if map.properties["parallax" .. i] then
parallax[i] = love.graphics.newImage("game/"..map.properties["parallax" .. i])
else
break
end
end
local status, canvas = pcall(love.graphics.newCanvas)
if not status or not (type(canvas) == 'userdata') then
canvas = nil
end
object.width = function ()
return map.width * map.tilewidth
end
object.height = function ()
return map.height * map.tileheight
end
object.ntilesx = function()
return map.width
end
object.ntilesy = function()
return map.height
end
-- getTileIndices(arg)
object.getTileIndices = function (arg)
return {x = math.ceil(arg.x / (map.tilesets[tilelayer].tilewidth * entity.scale)),
y = math.ceil(arg.y / (map.tilesets[tilelayer].tileheight * entity.scale))}
end
-- getTileProperties(arg)
object.getTileProperties = function (arg)
if arg.x < 1 or arg.y < 1 or
arg.x > map.layers[tilelayer].width or
arg.y > map.layers[tilelayer].height then
return {}
end
local tile_id = tiledata[arg.x][arg.y]
local properties = {}
if map.tilesets[tilelayer].tiles[tile_id] then
for k,v in pairs(map.tilesets[tilelayer].tiles[tile_id]) do
local f = assert(loadstring("return " .. v))
_,properties[k] = pcall(f)
end
end
return properties
end
-- getTileBounds(arg)
object.getTileBounds = function (arg)
return {
top = (arg.y - 1) * (map.tilesets[tilelayer].tileheight * entity.scale),
left = (arg.x - 1) * (map.tilesets[tilelayer].tilewidth * entity.scale),
bottom = arg.y * (map.tilesets[tilelayer].tileheight * entity.scale),
right = arg.x * (map.tilesets[tilelayer].tilewidth * entity.scale)
}
end
local lastParallaxUpdate = 0
--prerender()
object.prerender = function ()
if canvas then
canvas:clear()
canvas:renderTo(function()
local camera = state.get().camera.position
camera.x = math.floor(camera.x)
camera.y = math.floor(camera.y)
lastParallaxUpdate = camera.x
love.graphics.setColor({255,255,255})
local screen_width = love.graphics.getWidth() --native_mode.width
local screen_height = love.graphics.getHeight() --native_mode.height
for i=1,#parallax do
parallax[i]:setWrap('repeat','repeat')
local image = parallax[i]
local width = screen_width
local height = screen_height
local x = (i / 20) * (camera.x - (screen_width / 2))
local y = 0
if x < screen_width and x + width > 0 then
love.graphics.drawq(parallax[i],
love.graphics.newQuad(x,y,screen_width,screen_height,width,height),
0, -- x
0, -- y
0, -- rotation
1, 1, -- scale_x, scale_y
0, 0, -- origin_x, origin_y
0, 0 -- shearing_x, shearing_y
)
end
end
end)
end
end
-- render()
object.render = function (lagging)
local camera = state.get().camera.position
camera.x = math.floor(camera.x)
camera.y = math.floor(camera.y)
love.graphics.setColor({255,255,255})
local screen_width = love.graphics.getWidth() --native_mode.width
local screen_height = love.graphics.getHeight() --native_mode.height
if canvas then
if not lagging and math.abs(lastParallaxUpdate - camera.x) >= 7 then
object.prerender()
end
love.graphics.draw(canvas,
0, 0, -- x, y
0, -- rotation
1, 1, -- scale_x, scale_y
0, 0, -- origin_x, origin_y
0, 0 -- shearing_x, shearing_y
)
else
for i=1,#parallax do
parallax[i]:setWrap('repeat','repeat')
local image = parallax[i]
-- local width = parallax[i]:getWidth()
-- local height = parallax[i]:getHeight()
-- local x = (i / 10) * (camera.x - (screen_width / 2))
-- local y = (height - (screen_height / 2))
local width = screen_width
local height = screen_height
local x = (i / 20) * (camera.x - (screen_width / 2))
local y = 0
if x < screen_width and x + width > 0 then
love.graphics.drawq(parallax[i],
-- love.graphics.newQuad(x,y,screen_width,screen_height,width*entity.scale,height*entity.scale),
love.graphics.newQuad(x,y,screen_width,screen_height,width,height),
0, -- x
0, -- y
0, -- rotation
1, 1, -- scale_x, scale_y
0, 0, -- origin_x, origin_y
0, 0 -- shearing_x, shearing_y
)
end
end
end
tile_batch:clear()
local l = math.floor((camera.x - (screen_width / 2))/(map.tilesets[tilelayer].tilewidth*entity.scale))
local r = math.ceil((camera.x + (screen_width / 2))/(map.tilesets[tilelayer].tilewidth*entity.scale))
local t = math.floor((camera.y - (screen_height / 2))/(map.tilesets[tilelayer].tileheight*entity.scale))
local b = math.ceil((camera.y + (screen_height / 2))/(map.tilesets[tilelayer].tileheight*entity.scale))
if not map.properties['wrap'] then
if l < 0 then l = 0 end
if r > map.layers[tilelayer].width-1 then r = map.layers[tilelayer].width-1 end
if t < 0 then t = 0 end
if b > map.layers[tilelayer].height-1 then b = map.layers[tilelayer].height-1 end
end
for x=l,r do
for y=t,b do
normX = (x % map.layers[tilelayer].width) + 1
normY = (y % map.layers[tilelayer].height) + 1
if tiledata[normX][normY] > 0 then
tile_batch:addq(tileset.quads[tiledata[normX][normY]],
x*map.tilesets[tilelayer].tilewidth,
y*map.tilesets[tilelayer].tileheight)
end
end
end
love.graphics.setColor({255,255,255})
love.graphics.draw(tile_batch,
(screen_width / 2) - camera.x, (screen_height / 2) - camera.y, -- x, y
0, -- rotation
entity.scale, entity.scale, -- scale_x, scale_y
0, 0, -- origin_x, origin_y
0, 0 -- shearing_x, shearing_y
)
end
-- canContain(arg)
object.canContain = function (arg)
local small = {
top = arg.top,
left = arg.left,
bottom = arg.bottom,
right = arg.right
}
local size = arg.size
local big = {
top = arg.top,
left = arg.left,
bottom = arg.top + size,
right = arg.left + size
}
local max_i = size - (small.right - small.left) + 1
local max_j = size - (small.bottom - small.top) + 1
for i=1,max_i do
for j=1,max_j do
local fit = true
for y=big.top,big.bottom do
for x=big.left,big.right do
if object.getTileProperties(x, y).solid then
fit = false
end
end
end
if fit == true then
return true
end
big.top = big.top + 1
big.bottom = big.bottom + 1
end
big.top = big.top - size + 1
big.bottom = big.bottom - size + 1
big.left = big.left + 1
big.right = big.right + 1
end
end
object.reset = function ()
time.clear()
entity.clearAll()
loadEntities(map, objectgroup)
loadTiledata()
end
object.delete = function()
event.unsubscribe("tileCollision", object.tileCollision)
event.unsubscribe("tileCreation", object.tileCreation)
event.unsubscribe("tileDecay", object.tileDecay)
end
object.tileCollision = function (arg)
local tile = object.getTileProperties(arg)
if tile.destructible then
if not tile.destroyWeight or arg.entity.weight() >= tile.destroyWeight then
tiledata[arg.x][arg.y] = tile.destroyTo or 0
event.notify("tileCreation", arg)
end
end
end
object.tileCreation = function (arg)
local tile = object.getTileProperties(arg)
if tile.decayTime then
time.delay("tileDecay",arg,tile.decayTime)
end
end
object.tileDecay = function (arg)
local tile = object.getTileProperties(arg)
tiledata[arg.x][arg.y] = tile.decayTo or 0
event.notify("tileCreation", arg)
end
event.subscribe("tileCollision", object.tileCollision)
event.subscribe("tileCreation", object.tileCreation)
event.subscribe("tileDecay", object.tileDecay)
time.clear()
loadTiledata()
object.prerender()
return object
end
return M