2
0

loader.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. data = {}
  2. data.load = function()
  3. -- Media
  4. local function lookup(ext, fn)
  5. local function halp(s, k)
  6. local base = s._path .. '/' .. k
  7. local function extLoad(ext)
  8. if love.filesystem.exists(base .. ext) then
  9. s[k] = fn(base .. ext)
  10. elseif love.filesystem.isDirectory(base) then
  11. local t = {}
  12. t._path = base
  13. setmetatable(t, {__index = halp})
  14. s[k] = t
  15. else
  16. return false
  17. end
  18. return true
  19. end
  20. if type(ext) == 'table' then
  21. lume.each(ext, function(e) return extLoad(e) end)
  22. else
  23. extLoad(ext)
  24. end
  25. return rawget(s, k)
  26. end
  27. return halp
  28. end
  29. data.media = {}
  30. data.media.graphics = setmetatable({_path = 'media/graphics'}, {__index = lookup({'.png', '.dds'}, love.graphics and love.graphics.newImage or f.empty)})
  31. data.media.sounds = setmetatable({_path = 'media/sounds'}, {__index = lookup('.ogg', love.audio and love.audio.newSource or f.empty)})
  32. -- Data
  33. local function load(dir, type, fn)
  34. local id = 1
  35. local function halp(dir, dst)
  36. for _, file in ipairs(love.filesystem.getDirectoryItems(dir)) do
  37. path = dir .. '/' .. file
  38. if love.filesystem.isDirectory(path) then
  39. dst[file] = {}
  40. halp(path, dst[file])
  41. elseif file:match('%.lua$') and not file:match('^%.') then
  42. local obj = love.filesystem.load(path)()
  43. assert(obj, path .. ' did not return a value')
  44. obj.code = obj.code or file:gsub('%.lua', '')
  45. obj.id = id
  46. obj = lume.call(fn, obj) or obj
  47. data[type][id] = obj
  48. dst[obj.code] = obj
  49. id = id + 1
  50. end
  51. end
  52. end
  53. data[type] = {}
  54. halp(dir, data[type])
  55. end
  56. load('data/animation', 'animation', function(animation)
  57. -- Set up lazy loading for images
  58. local code = animation.code
  59. animation.graphics = setmetatable({_path = 'media/skeletons/' .. code}, {
  60. __index = lookup({'.png', '.dds'}, function(path)
  61. local img = love.graphics.newImage(path)
  62. if path:match('%.dds') then img:setMipmapFilter('nearest', 1) end
  63. return img
  64. end)
  65. })
  66. -- Set up static spine data structures
  67. local s = {}
  68. s.__index = s
  69. if love.filesystem.exists('media/skeletons/' .. code .. '/' .. code .. '.atlas') then
  70. s.atlas = spine.Atlas.new('media/skeletons/' .. code .. '/' .. code .. '.atlas')
  71. s.atlasAttachmentLoader = spine.AtlasAttachmentLoader.new(s.atlas)
  72. end
  73. s.json = spine.SkeletonJson.new(s.atlasAttachmentLoader)
  74. s.skeletonData = s.json:readSkeletonDataFile('media/skeletons/' .. code .. '/' .. code .. '.json')
  75. s.animationStateData = spine.AnimationStateData.new(s.skeletonData)
  76. -- Reverse-index keys (sorted for consistent order)
  77. local keys = lume.keys(animation.states)
  78. table.sort(keys)
  79. for i = 1, #keys do
  80. local state = animation.states[keys[i]]
  81. animation.states[i] = state
  82. state.index = i
  83. state.name = keys[i]
  84. end
  85. -- Set mixes
  86. for i = 1, #animation.states do
  87. table.each(animation.states, function(state)
  88. if state.index ~= i then
  89. s.animationStateData:setMix(animation.states[i].name, state.name, Animation.defaultMix)
  90. end
  91. end)
  92. table.each(animation.states[i].mix or {}, function(time, to)
  93. s.animationStateData:setMix(animation.states[i].name, to, time)
  94. end)
  95. end
  96. animation.spine = s
  97. return animation
  98. end)
  99. load('data/particle', 'particle')
  100. end