-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
init.lua
616 lines (509 loc) · 13.4 KB
/
init.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Map class
--
--==========================================================================--
require( "engine.shared.hook" )
class( "map" )
map._maps = map._maps or {}
if ( _CLIENT ) then
local r_draw_world = convar( "r_draw_world", "1", nil, nil, "Draws world" )
local function drawMap( map )
local worldIndex = camera.getWorldIndex()
if ( worldIndex ~= map:getWorldIndex() ) then
return
end
love.graphics.push()
local x, y = map:getX(), map:getY()
love.graphics.translate( x, y )
map:draw()
love.graphics.pop()
end
function map.drawWorld()
local r, g, b, a = love.graphics.getBackgroundColor()
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
love.graphics.setColor( r, g, b, a )
love.graphics.rectangle( "fill", 0, 0, width, height )
love.graphics.push()
-- Setup camera
local scale = camera.getZoom()
love.graphics.scale( scale )
local x, y = camera.getTranslation()
love.graphics.translate( x, y )
-- Draw maps
if ( r_draw_world:getBoolean() ) then
for _, map in ipairs( map._maps ) do
drawMap( map )
end
end
-- Draw entities
entity.drawAll()
love.graphics.pop()
end
end
function map.exists( name )
name = string.gsub( name, "%.", "/" )
return love.filesystem.getInfo( "maps/" .. name .. ".lua" ) ~= nil
end
function map.findNextWorldIndex()
local worldIndex = 1
for _, map in ipairs( map._maps ) do
if ( worldIndex == map:getWorldIndex() ) then
worldIndex = worldIndex + 1
end
end
return worldIndex
end
function map.getAll()
return table.shallowcopy( map._maps )
end
function map.getByName( name )
for _, map in ipairs( map._maps ) do
if ( name == map:getName() ) then
return map
end
end
end
function map.getAtPosition( position, worldIndex )
worldIndex = worldIndex or 1
for _, map in ipairs( map._maps ) do
local px, py = position.x, position.y
local x, y = map:getX(), map:getY()
local width = map:getPixelWidth()
local height = map:getPixelHeight()
if ( map.pointinrect( px, py, x, y, width, height ) ) then
return map
end
end
end
function map.getWorld()
return map._world
end
function map.getGroundBody()
return map._groundBody
end
function map.load( name, x, y, worldIndex )
x = x or 0
y = y or 0
if ( map.getByName( name ) ) then
return
end
local map = map( name, x, y, worldIndex )
table.insert( map._maps, map )
end
function map.reload( library )
if ( string.sub( library, 1, 8 ) ~= "maps." ) then
return
end
local name = string.gsub( library, "maps.", "" )
local r = map.getByName( name )
local x = r:getX()
local y = r:getY()
local worldIndex = r:getWorldIndex()
-- TODO: Move players to new map.
map.unload( name )
map.load( name, x, y, worldIndex )
end
hook.set( "shared", map.reload, "onReloadScript", "reloadMap" )
if ( _CLIENT ) then
local function initializeTiles( map )
for _, maplayer in ipairs( map:getLayers() ) do
if ( maplayer:getType() == "tilelayer" ) then
maplayer:initializeTiles()
end
end
end
function map.reloadTiles( filename )
if ( not string.find( filename, "images/tilesets/" ) ) then
return
end
for _, map in ipairs( map._maps ) do
initializeTiles( map )
end
end
hook.set( "client", reloadTiles, "onReloadImage", "reloadTiles" )
end
function map.unload( name )
unrequire( "maps." .. name )
for i, map in ipairs( map._maps ) do
if ( name == map:getName() ) then
map:remove()
table.remove( map._maps, i )
return
end
end
end
function map.unloadAll()
for i = #map._maps, 1, -1 do
local m = map._maps[ i ]
map.unload( m:getName() )
end
end
map.shutdown = map.unloadAll
if ( not _DEDICATED ) then
concommand( "map", "Loads the specified map",
function( _, _, _, _, argT )
local name = argT[ 1 ]
if ( name == nil ) then
print( "map <map name>" )
return
end
if ( not map.exists( name ) ) then
print( name .. " does not exist." )
return
end
engine.client.disconnect()
if ( not engine.client.initializeServer() ) then
return
end
game.initialMap = name
engine.client.connectToListenServer()
end,
nil,
function( argS )
local autocomplete = {}
local files = love.filesystem.getDirectoryItems( "maps" )
for _, v in ipairs( files ) do
if ( string.fileextension( v ) == "lua" ) then
local name = string.gsub( v, ".lua", "" )
local cmd = "map " .. name
if ( string.find( cmd, "map " .. argS, 1, true ) ) then
table.insert( autocomplete, cmd )
end
end
end
table.sort( autocomplete )
return autocomplete
end
)
end
function map.pointinrect( px, py, x, y, width, height )
return px >= x and
py > y and
px < x + width and
py <= y + height
end
function map.roundToGrid( x, y )
local map = map.getAtPosition( vector( x, y ) )
if ( map == nil ) then
return x, y
end
local w, h = map:getTileSize()
x = x - x % w + math.nearestmult( x % w, w )
y = y - y % h + math.nearestmult( y % h, h )
return x, y
end
function map.snapToGrid( x, y )
local map = map.getAtPosition( vector( x, y ) )
if ( map == nil ) then
return x, y
end
local w, h = map:getTileSize()
x = x - x % w
y = y - y % h
return x, y
end
function map:map( name, x, y, worldIndex )
self.name = name
self.data = require( "maps." .. name )
self.x = x or 0
self.y = y or 0
self.worldIndex = worldIndex or 1
self:parse()
if ( _CLIENT ) then
require( "engine.client.canvas" )
local width = self:getPixelWidth()
local height = self:getPixelHeight()
self.canvas = canvas( width, height, { dpiscale = 1 } )
self.canvas:setFilter( "nearest", "nearest" )
end
self.needsRedraw = true
end
if ( _CLIENT ) then
function map:draw()
local layers = self:getLayers()
if ( layers == nil ) then
return
end
local canvas = self:getCanvas()
if ( self.needsRedraw ) then
love.graphics.push()
canvas:renderTo( function()
love.graphics.origin()
love.graphics.clear()
for _, layer in ipairs( layers ) do
if ( layer:isVisible() ) then
layer:draw()
end
end
end )
love.graphics.pop()
self.needsRedraw = false
end
love.graphics.setColor( color.white )
canvas:draw()
end
accessor( map, "canvas" )
end
function map:cleanup()
local entities = self:getEntities()
if ( entities == nil ) then
return
end
for i = #entities, 1, -1 do
local entity = entities[ i ]
if ( not typeof( entity, "player" ) ) then
entity:remove()
end
end
end
accessor( map, "entities" )
function map:getFilename()
return string.gsub( self.name, "%.", "/" ) .. ".lua"
end
accessor( map, "formatVersion" )
local data = {}
local gid = 0
function map:getGidsAtPosition( position )
-- Convert to local positions
position = vector.copy( position )
position.x = position.x - self:getX()
position.y = position.y - self:getY()
local tileWidth = self:getTileWidth()
local tileHeight = self:getTileHeight()
position.y = position.y - tileHeight
local x = ( position.x / tileWidth ) + 1
local y = ( position.y / tileHeight ) * self:getWidth()
local xy = x + y
local gids = {}
for _, layer in ipairs( self:getLayers() ) do
data = layer:getData()
gid = data[ xy ]
table.insert( gids, gid )
end
return gids
end
accessor( map, "layers" )
accessor( map, "name" )
accessor( map, "orientation" )
function map:getPixelWidth()
return self:getTileWidth() * self:getWidth()
end
function map:getPixelHeight()
return self:getTileHeight() * self:getHeight()
end
accessor( map, "properties" )
function map:getTileset( layer )
local gid = layer:getHighestTileGid()
local tileset = nil
for _, t in ipairs( self:getTilesets() ) do
if ( t:getFirstGid() <= gid ) then
tileset = t
end
end
return tileset
end
accessor( map, "tilesets" )
accessor( map, "tileWidth" )
accessor( map, "tileHeight" )
accessor( map, "width" )
accessor( map, "height" )
accessor( map, "worldIndex" )
accessor( map, "x" )
accessor( map, "y" )
local function beginContact( a, b, contact )
a = a:getUserData()
b = b:getUserData()
if ( a ) then
a:startTouch( b, contact )
end
end
local function endContact( a, b, contact )
a = a:getUserData()
b = b:getUserData()
if ( a ) then
a:endTouch( b, contact )
end
end
local function preSolve( a, b, contact )
end
local function postSolve( a, b, contact, normalimpulse, tangentimpulse )
end
function map.initializeWorld()
if ( map._world ) then
return
end
map._world = love.physics.newWorld()
map._groundBody = love.physics.newBody( map.getWorld(), 0, 0 )
local world = map.getWorld()
world:setCallbacks( beginContact, endContact, preSolve, postSolve )
end
function map:initializePhysics()
-- If the map has trigger_transitions, let level designers set
-- up nodraws themselves.
require( "engine.shared.entities.entity" )
if ( entity.findByClassname( "trigger_transition", self ) ) then
return false
end
local x = self:getX()
local y = self:getY()
local width = self:getPixelWidth()
local height = self:getPixelHeight()
local ground = map.getGroundBody()
-- Top boundary
local shape = love.physics.newEdgeShape( x + 0, y + 0, x + width, y + 0 )
local fixture = love.physics.newFixture( ground, shape )
-- Right boundary
shape = love.physics.newEdgeShape( x + width, y + 0, x + width, y + height )
fixture = love.physics.newFixture( ground, shape )
-- Bottom boundary
shape = love.physics.newEdgeShape( x + width, y + height, x + 0, y + height )
fixture = love.physics.newFixture( ground, shape )
-- Left boundary
shape = love.physics.newEdgeShape( x + 0, y + height, x + 0, y + 0 )
fixture = love.physics.newFixture( ground, shape )
end
local px = 0
local py = 0
local x = 0
local y = 0
local width = 0
local height = 0
local pointinrect = map.pointinrect
local gids = {}
local firstGid = 0
local tiles = {}
local hasvalue = table.hasvalue
local hasProperties = false
local properties = {}
local walkable = nil
function map:isTileWalkableAtPosition( position )
-- Check world bounds
px = position.x
py = position.y
x = self:getX()
y = self:getY()
width = self:getPixelWidth()
height = self:getPixelHeight()
if ( not pointinrect( px, py, x, y, width, height ) ) then
-- No, but check nearby map
local tilePosition = vector( px, py )
local worldIndex = self:getWorldIndex()
local map = map.getAtPosition( tilePosition, worldIndex )
if ( map ) then
return map:isTileWalkableAtPosition( position )
else
return false
end
end
-- Check world collisions
gids = self:getGidsAtPosition( position )
for _, tileset in ipairs( self:getTilesets() ) do
firstGid = tileset:getFirstGid()
tiles = tileset:getTiles()
for _, tile in ipairs( tiles ) do
hasProperties = hasvalue( gids, tile.id + firstGid )
properties = tile.properties
walkable = hasProperties and properties and properties.walkable
if ( hasProperties and walkable == false ) then
return false
end
end
end
-- Check entity collisions
py = py - game.tileSize
local entities = self:getEntities()
if ( entities ) then
for _, entity in ipairs( entities ) do
local body = entity:getBody()
if ( entity:testPoint(
px + game.tileSize / 2,
py + game.tileSize / 2
) and ( body and body:getType() == "static" ) ) then
return false
end
end
end
return true
end
function map:loadTilesets( tilesets )
if ( self.tilesets ) then
return
end
self.tilesets = {}
require( "engine.shared.map.tileset" )
for _, tilesetData in ipairs( tilesets ) do
local tileset = map.tileset( self, tilesetData )
table.insert( self.tilesets, tileset )
end
end
function map:loadLayers( layers )
if ( self.layers ) then
return
end
self.layers = {}
require( "engine.shared.map.layer" )
for _, layerData in ipairs( layers ) do
local layer = map.layer( layerData )
layer:setMap( self )
layer:parse()
local tileset = self:getTileset( layer )
layer:setTileset( tileset )
table.insert( self.layers, layer )
end
end
function map:parse()
if ( self.data == nil ) then
return
end
local data = self.data
self:setFormatVersion( data[ "version" ] )
self:setOrientation( data[ "orientation" ] )
self:setWidth( data[ "width" ] )
self:setHeight( data[ "height" ] )
self:setTileWidth( data[ "tilewidth" ] )
self:setTileHeight( data[ "tileheight" ] )
self:setProperties( table.copy( data[ "properties" ] ) )
map.initializeWorld()
-- self:initializePhysics()
self:loadTilesets( data[ "tilesets" ] )
self:loadLayers( data[ "layers" ] )
-- NOTE: Do this here to detect trigger_transitions, instead.
self:initializePhysics()
self.data = nil
end
function map:remove()
self:cleanup()
local world = map.getWorld()
if ( world and #map._maps == 1 ) then
local ground = map.getGroundBody()
ground:destroy()
map._groundBody = nil
world:destroy()
map._world = nil
end
end
function map:removeEntity( entity )
local entities = self:getEntities()
if ( entities == nil ) then
return
end
for i, v in ipairs( entities ) do
if ( v == entity ) then
table.remove( entities, i )
end
end
end
function map:getTileSize()
return self:getTileWidth(), self:getTileHeight()
end
function map.tick( timestep )
local world = map.getWorld()
if ( world ) then
world:update( timestep )
end
end
function map:__tostring()
return "map: \"" .. self:getFilename() .. "\""
end