loader.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. table.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.shaders = setmetatable({_path = 'media/shaders'}, {__index = lookup('.shader', love.graphics and love.graphics.newShader or f.empty)})
  32. data.media.sounds = setmetatable({_path = 'media/sounds'}, {__index = lookup('.ogg', love.audio and love.audio.newSource or f.empty)})
  33. -- Data
  34. local function load(dir, type, fn)
  35. local id = 1
  36. local function halp(dir, dst)
  37. for _, file in ipairs(love.filesystem.getDirectoryItems(dir)) do
  38. path = dir .. '/' .. file
  39. if love.filesystem.isDirectory(path) then
  40. dst[file] = {}
  41. halp(path, dst[file])
  42. elseif file:match('%.lua$') and not file:match('^%.') then
  43. local obj = love.filesystem.load(path)()
  44. assert(obj, path .. ' did not return a value')
  45. obj.code = obj.code or file:gsub('%.lua', '')
  46. obj.id = id
  47. obj = f.exe(fn, obj) or obj
  48. data[type][id] = obj
  49. dst[obj.code] = obj
  50. id = id + 1
  51. end
  52. end
  53. end
  54. data[type] = {}
  55. halp(dir, data[type])
  56. end
  57. load('data/buff', 'buff')
  58. load('data/ability', 'ability', function(ability)
  59. if ability.upgrades then
  60. table.each(ability.upgrades, function(upgrade)
  61. if upgrade.code then
  62. ability.upgrades[upgrade.code] = upgrade
  63. end
  64. end)
  65. end
  66. end)
  67. load('data/unit', 'unit', function(unit)
  68. if data.media.sounds[unit.code] and data.media.sounds[unit.code]['attackHit1'] then
  69. local sounds = {}
  70. for i = 1, 100 do
  71. if data.media.sounds[unit.code]['attackHit' .. i] then
  72. table.insert(sounds, 'media/sounds/' .. unit.code .. '/attackHit' .. i .. '.ogg')
  73. else
  74. break
  75. end
  76. end
  77. data.media.sounds[unit.code].attackHit = love.audio.newSource(sounds)
  78. end
  79. table.each(table.keys(unit.upgrades), function(upgrade, i)
  80. unit.upgrades[i] = unit.upgrades[upgrade]
  81. unit.upgrades[i].code = upgrade
  82. end)
  83. unit.attributes = {}
  84. unit.attributeCosts = {}
  85. table.each(config.attributes.list, function(attribute)
  86. unit.attributes[attribute] = 0
  87. unit.attributeCosts[attribute] = config.attributes.baseCost
  88. end)
  89. end)
  90. load('data/spell', 'spell')
  91. load('data/animation', 'animation', function(animation)
  92. -- Set up lazy loading for images
  93. local code = animation.code
  94. animation.graphics = setmetatable({_path = 'media/skeletons/' .. code}, {
  95. __index = lookup({'.png', '.dds'}, function(path)
  96. local img = love.graphics.newImage(path)
  97. if path:match('%.dds') then img:setMipmapFilter('nearest', 1) end
  98. return img
  99. end)
  100. })
  101. -- Set up static spine data structures
  102. local s = {}
  103. s.__index = s
  104. if love.filesystem.exists('media/skeletons/' .. code .. '/' .. code .. '.atlas') then
  105. s.atlas = spine.Atlas.new('media/skeletons/' .. code .. '/' .. code .. '.atlas')
  106. s.atlasAttachmentLoader = spine.AtlasAttachmentLoader.new(s.atlas)
  107. end
  108. s.json = spine.SkeletonJson.new(s.atlasAttachmentLoader)
  109. s.skeletonData = s.json:readSkeletonDataFile('media/skeletons/' .. code .. '/' .. code .. '.json')
  110. s.animationStateData = spine.AnimationStateData.new(s.skeletonData)
  111. -- Reverse-index keys (sorted for consistent order)
  112. local keys = table.keys(animation.states)
  113. table.sort(keys)
  114. for i = 1, #keys do
  115. local state = animation.states[keys[i]]
  116. animation.states[i] = state
  117. state.index = i
  118. state.name = keys[i]
  119. end
  120. -- Set mixes
  121. for i = 1, #animation.states do
  122. table.each(animation.states, function(state)
  123. if state.index ~= i then
  124. s.animationStateData:setMix(animation.states[i].name, state.name, Animation.defaultMix)
  125. end
  126. end)
  127. table.each(animation.states[i].mix, function(time, to)
  128. s.animationStateData:setMix(animation.states[i].name, to, time)
  129. end)
  130. end
  131. animation.spine = s
  132. -- If it's an animation for a unit, make sure all required animations are supplied.
  133. --[[if data.unit[animation.code] then
  134. local instance = animation()
  135. local function check(name)
  136. local code = animation.code:capitalize()
  137. local article = 'a'
  138. local first = name:sub(1, 1)
  139. if table.has({'a', 'e', 'i', 'o', 'u'}, first) then article = 'an' end
  140. if not instance.spine.skeletonData:findAnimation(name) then print(code .. ' is missing ' .. article .. ' ' .. name .. ' animation') end
  141. end
  142. check('spawn')
  143. check('idle')
  144. check('walk')
  145. check('attack')
  146. check('death')
  147. end]]
  148. return animation
  149. end)
  150. load('data/particle', 'particle')
  151. load('data/effect', 'effect')
  152. load('data/gooey', 'gooey')
  153. load('data/shruju', 'shruju')
  154. load('data/ai', 'ai')
  155. load('data/atlas', 'atlas')
  156. end