Pinkishu

effectsAPI

Aug 11th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1. local effects = {}
  2.  
  3.  
  4.  
  5.  
  6.  
  7. local function getBasePath()
  8.     return "effectAPI/"
  9.  
  10. end
  11.  
  12. local function readEffectFromFile(effPath)
  13.     if not fs.exists(effPath) then return nil end
  14.     local file = fs.open(effPath,"r")
  15.  
  16.     if file then
  17.         local eff = {}
  18.         eff.name = file.readLine()
  19.         local c = ""
  20.         local line = file.readLine()
  21.         while line do
  22.             c = c.. " "..line
  23.             line = file.readLine()
  24.         end
  25.         file.close()
  26.         eff.play,e = loadstring(c)
  27.         if e then error("LOAD:"..e) end
  28.         return eff
  29.     else
  30.         return nil
  31.     end
  32. end
  33.  
  34. function reloadEffects()
  35.     local basePath = getBasePath()
  36.     local effectPath = fs.combine(basePath,"effects")
  37.     if fs.exists(effectPath) and fs.isDir(effectPath) then
  38.         local files = fs.list(effectPath)
  39.         for i,v in ipairs(files) do
  40.             local ePath = fs.combine(effectPath,v)
  41.             if not fs.isDir(ePath) then
  42.                 local newEff = readEffectFromFile(ePath)
  43.                 if newEff then effects[newEff.name] = newEff end
  44.             end
  45.         end
  46.     end
  47. end
  48. reloadEffects()
  49.  
  50.  
  51. local function _applyEffect(x,y,screen,effect)
  52.     if not effects[effect] then
  53.         reloadEffects()
  54.     end
  55.     if effects[effect] then
  56.         if effects[effect].play == nil then error("Effect "..effect.. " lacks play function.") end
  57.         effects[effect].play(x,y,screen)
  58.     end
  59. end
  60.  
  61.  
  62. local function _playEffect(x,y,preScreen,newScreen,effect)
  63.     if preScreen then
  64.         _applyEffect(x,y,preScreen,"clearEffect")
  65.     end
  66.  
  67.     if newScreen then
  68.         _applyEffect(x,y,newScreen,effect)
  69.     end
  70. end
  71.  
  72. function playEffect(x,y,preScreen,newScreen,effect)
  73.  
  74.     _playEffect(x,y,nil,preScreen,"clearEffect")
  75.     _playEffect(x,y,nil,newScreen,effect)
  76. end
Advertisement
Add Comment
Please, Sign In to add comment