package.path = package.path .. ";./assets/scripts/?.lua"
require "luascape.audio"
require "luascape.bullet"
require "luascape.core"
require "luascape.graphics"
require "luascape.input"
require "luascape.io"
require "luascape.logic"
require "luascape.render"
require "luascape.system"
require "luascape.math"
require "input"
require "materials"
require "particlesystem"
require "settings"
require "threads"
require "triggers"
require "dialogs"

-- Globals
renderer = nil
rootNode = nil
light = nil
player = {} 
finishLevel = false
crossSectionCreation = 0.0

entities = {}

function applyMaterial(entityType, node)
  --[[
  if (entityType == "crystal") then
    -- materials.applyCrystalMaterial(node, rootNode, renderer, light)
    materials.applyHatchingMaterial(node, light, "test.png")
  end
  if (entityType == "entity") then
    materials.applyHatchingMaterial(node, light, "stehle.jpg")
  end
  if (entityType == "lavalake") then
    materials.applyLavaMaterial(node)
  end
  
  ]]--
end

function initDotties() 
  local level = scape.tinyandbig.Act.currentAct()
  for i= 1,10 do
    local dottie = scape.tinyandbig.Dottie(Vector(math.random(-2,2),math.random(10,20),math.random(-2,2)))
    level.worldModel.entities:add(dottie) 
    rootNode.children:add( dottie.node)
  end
end

function setClipDepth(f)
  local level = scape.tinyandbig.Level.currentLevel()
  local levelRenderPass = level.levelRenderPass
  local lodRenderPass = levelRenderPass.renderer:findFirstRenderPass("LODRenderPass")
  if (lodRenderPass) then
    lodRenderPass.clipDepth = f
  end
  
end

function initLevel(levelRenderPass, _rootNode)
  local level = scape.tinyandbig.Act.currentAct()
  rootNode = _rootNode
  renderer = levelRenderPass
  initInputSystem()
  initScene(level)
  initDust(levelRenderPass)
  initDialogs()
end

function initDust(levelRenderPass)
  local particleRenderPass = levelRenderPass.renderer:findFirstRenderPass("particleRenderPass")
  local scene
  if (particleRenderPass) then
    scene = particleRenderPass.scene
  else
    scene = scape.tinyandbig.Act.currentAct().node  
  end
  local dust = scene.children:add(Infinitude(levelRenderPass.camera))
  for i = 1,5 do
    local node, system, crowd = particleSystem.create()
    dust.children:add(node)
    crowd.material = crowd.material:shader():clone()
    graphics.applyTexture(crowd, "dust0" .. i .. ".png")
    
    system.maxParticleCount = 1000
    system.output = 100
    system.emitter.radius = 30
    system.velocity = Vector(0,0,0)
    system.velocityRange = Vector(0.5,0.5,0.5)
    system.lifetime = 10
    system.alphaOutFading = true
    system.gravity = Vector(0,0,0)
    system.rotationSpeed = math.random() * 2 -1
    system:setScale(0.05,0.1)
    system:setRotation(0,6.315)
  end
  return dust
end


function initScene(level)
  light = rootNode.children:add(scape.objects.Light())
  light.diffuseColor = scape.graphics.Color(1.0, 1.0, 1.0, 1.0)
  light.ambientColor = scape.graphics.Color(1.0, 1.0, 1.0, 1.0)
  light.type = Light.DIRECTIONAL
  light.translation = Vector(-100, 100, -100)
  light.rotation = Quaternion(Vector.AXIS_X, math.pi / 2)
  local interpreter = MayaLevelInterpreter(level)
  interpreter:loadMayaLevel(settings.level)
end


function updateLevel()
  threads.dispatch()
end

close = function()
  finishLevel = true
end
