map.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. local Map = class()
  2. -- ...
  3. purple = 0
  4. orange = 1
  5. local function safeLoad(file)
  6. local ok, chunk, result
  7. ok, chunk = pcall(love.filesystem.load, file)
  8. if not ok then print(chunk) return nil end
  9. ok, result = pcall(chunk)
  10. if not ok then print(result) return nil end
  11. return result
  12. end
  13. function Map:init(name)
  14. name = 'industria'
  15. local function dir(x) return 'data/maps/' .. name .. '/' .. x end
  16. self.props = {}
  17. self.tiles = {}
  18. self.atlas = love.graphics.newImage(dir(name .. '.png'))
  19. table.merge(safeLoad(dir(name .. '.lua')), self)
  20. map = self
  21. local props = safeLoad(dir('props.lua'))
  22. local tiles = safeLoad(dir('tiles.lua'))
  23. map = nil
  24. table.merge(tiles, self.tiles)
  25. table.merge(props, self.props)
  26. self.textures = table.map(self.textures, function(tex)
  27. tex[5], tex[6] = self.atlas:getDimensions()
  28. return love.graphics.newQuad(unpack(tex))
  29. end)
  30. self.mods = {}
  31. table.each({{kind = 'scoring'}, {kind = 'deathmatch'}}, function(mod)
  32. setmetatable(mod, {__index = data.mods[mod.kind]})
  33. f.exe(mod.activate, mod, self)
  34. self.mods[mod.kind] = mod
  35. end)
  36. table.each(self.props, function(prop)
  37. setmetatable(prop, {
  38. __index = data.prop[prop.kind .. (ctx.tag or ''):capitalize()] or data.prop[prop.kind],
  39. __tostring = data.prop[prop.kind].__tostring
  40. })
  41. end)
  42. self.props = table.filter(self.props, function(prop) return not ctx.tag or not prop.mod or self.mods[prop.mod] end)
  43. table.each(self.props, function(prop, id)
  44. prop.id = id
  45. f.exe(prop.activate, prop, self)
  46. end)
  47. self.batch = love.graphics.newSpriteBatch(self.atlas, #self.tiles + #self.props)
  48. self.batch:bind()
  49. self.batch:setColor(255, 255, 255)
  50. table.each(self.tiles, function(tile)
  51. tile[1] = self.textures[tile[1]]
  52. self.batch:add(unpack(tile))
  53. end)
  54. self.batch:unbind()
  55. f.exe(self.activate, self)
  56. self.depth = 5
  57. if ctx.view then
  58. ctx.view:register(self)
  59. ctx.view.xmax = self.width
  60. ctx.view.ymax = self.height
  61. end
  62. if self.weather then
  63. self.weather = new(data.weather[self.weather])
  64. if ctx.view then
  65. ctx.view:register(self.weather, 'gui')
  66. end
  67. end
  68. if self.soundscape then
  69. ctx.event:emit('sound.loop', {sound = self.soundscape, gui = true})
  70. end
  71. if ctx.view and love.graphics.isSupported('shader') then
  72. local downsample = 4
  73. local working = love.graphics.newCanvas(self.width / downsample, self.height / downsample)
  74. self.shadows = love.graphics.newCanvas(self.width / downsample, self.height / downsample)
  75. self.shadows:renderTo(function()
  76. love.graphics.setColor(0, 0, 0)
  77. love.graphics.push()
  78. love.graphics.scale(1 / downsample)
  79. table.each(self.props, function(p)
  80. if p.code == 'wall' or p.code == 'crate' then
  81. p.shape:draw('fill')
  82. end
  83. end)
  84. love.graphics.pop()
  85. end)
  86. data.media.shaders.horizontalBlur:send('amount', .0008)
  87. data.media.shaders.verticalBlur:send('amount', .0008)
  88. love.graphics.setColor(255, 255, 255)
  89. for i = 1, 3 do
  90. love.graphics.setShader(data.media.shaders.horizontalBlur)
  91. working:renderTo(function()
  92. love.graphics.draw(self.shadows)
  93. end)
  94. love.graphics.setShader(data.media.shaders.verticalBlur)
  95. self.shadows:renderTo(function()
  96. love.graphics.draw(working)
  97. end)
  98. end
  99. end
  100. ctx.event:on(app.net.events.prop, function(data)
  101. local prop = self.props[data.id]
  102. prop.x, prop.y = data.x / 10, data.y / 10
  103. end)
  104. end
  105. function Map:update()
  106. table.each(self.props, function(p) f.exe(p.update, p) end)
  107. if self.weather then self.weather:update() end
  108. table.each(self.mods, function(mod) f.exe(mod.update, mod) end)
  109. end
  110. function Map:draw()
  111. love.graphics.draw(self.batch)
  112. love.graphics.draw(self.shadows, 0, 0, 0, 4, 4)
  113. end
  114. function Map:modExec(mod, fn, ...)
  115. local mod = self.mods[mod]
  116. if mod and mod[fn] then
  117. mod[fn](mod, ...)
  118. end
  119. end
  120. return Map