package.path = package.path .. ";./assets/scripts/?.lua"
require "tinyandbig"
require "materials"
require "sound"
require "typo"

local preloader = Preloader.instance()

function loadAsset(filename, key)
  if (string.find(filename, "ma$") or string.find(filename, "cfg$")) then
    print("preload scene '" .. filename .. "' with key '" .. key .. "'")
    preloader:setScene(key, Scene.loadFromFile(filename), Preloaded.GAME_DEPENDENT)
    return;    
  end
  if (string.find(filename, "ogg$")) then
    print("preload sound '" .. filename .. "' with key '" .. key .. "'")
    preloader:setSound(key, sound.createSound(filename), Preloaded.GAME_DEPENDENT)
    return;    
  end
end

function preload(preloaded, parentKey) 
  for k,v in pairs(preloaded) do
    local key;
    if (parentKey ~= nil) then
      key = parentKey .. "." .. k
    else
      key = k
    end  
      
    if (type(v) == "string") then
      loadAsset(v, key)
    end
    if (type(v) == "table") then
      preload(v, key)
    end
    if (type(v) == "userdata") then
      preloader:set(key, Value(v), Preloaded.GAME_DEPENDENT)
    end
  end
end


local preloadedList = {
  typo = {
    zot = "zot.ma",
    smash = "smash.ma",
    crack = "crack.ma",
    grab = "grab.ma",
    gameover = "gameover.ma",
    pop = "pop.ma",
    saved = "saved.ma"
  },
  
  claw = "claw.ma",
  big = "big.cfg",
  
  materials = {
    typo = {
      cyan = materials.createTypoMaterial("letters_cyan.jpg"),
      black = materials.createTypoMaterial("letters_black.jpg"),
      pink = materials.createTypoMaterial("letters_pinkl.jpg")
    },
    skydome = Shader(),
    crystal = {
      edge = materials.createCrystalEdgeMaterial()
    },
    ropeHUD = materials.createRopeHUDMaterial(),
    particles = {
      impactdust = Texture.loadFromFile("impactdust.png"),
      smallstone = Texture.loadFromFile("smallstone.png")
    },
    cutline = materials.createCutLineMaterial(),
    cutHUD = materials.createCutHUDMaterial(),
  },

  sounds = {
    tiny = {
      walking = "tinywalking.ogg",
      land = "tinyland.ogg",
      firerope = "tinyfirerope.ogg",
      tinydie = "tinydie.ogg",
      arm = "tinyarmgun.ogg",
      unarm = "tinyunarmgun.ogg",
      laser = "tinylaser.ogg",
      roperelease = "tinyroperelease.ogg",
      savemonkey = "tinyreachsavemonkey.ogg",
      hardwork = "tinydoesahardjob.ogg",
      rebirth = "tinyrebirth.ogg",
      jump = "tinyjump.ogg"
    },
    big = {
      beam = "bigbeam.ogg",
    },
    ambient = {
      radiosound1 = "tinybigradiosound1.ogg",
      radiosound2 = "tinybigradiosound2.ogg",
      radiosound3 = "tinybigradiosound3.ogg",
      radiosound4 = "tinybigradiosound4.ogg",                  
      musictrack1 = "tinybigtrack1.ogg",
      musictrack2 = "tinybigtrack2.ogg",
      musictrack3 = "tinybigtrack3.ogg",
      atmotrack1 = "tinybigatmo1.ogg",
      atmotrack2 = "tinybigatmo2.ogg",
      atmotrack3 = "tinybigatmo3.ogg",
      atmogroll = "tinybigatmogroll.ogg",
      movingstone01 = "ambientmovingstone01.ogg",
      impactstone01 = "tinybig1stoneimpact1.ogg",
      impactstone02 = "tinybig1stoneimpact2.ogg",
      impactstone03 = "tinybig1stoneimpact3.ogg",
      impactstone04 = "tinybig1stoneimpact4.ogg",
      impactstone05 = "tinybig1stoneimpact5.ogg",
      impactstone11 = "tinybig2stoneimpact1.ogg",
      impactstone12 = "tinybig2stoneimpact2.ogg",
      impactstone13 = "tinybig2stoneimpact3.ogg",
      impactstone14 = "tinybig2stoneimpact4.ogg",
      impactstone15 = "tinybig2stoneimpact5.ogg",
      impactstone21 = "tinybig3stoneimpact1.ogg",
      impactstone22 = "tinybig3stoneimpact2.ogg",
      impactstone23 = "tinybig3stoneimpact3.ogg",
      impactstone24 = "tinybig3stoneimpact4.ogg",
      impactstone25 = "tinybig3stoneimpact5.ogg"
    }
  }
}

preload(preloadedList)