context.lua 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. local context = {}
  2. function context.load(scene, config)
  3. lib.tick.index = 1
  4. context.scene = app.scenes[scene]
  5. context.view = lib.view:new(nil, config)
  6. context.particles = lib.particles:new(nil, config)
  7. context.collision = lib.collision:new(nil, config)
  8. context.view.xmax = context.scene.width
  9. context.view.ymax = context.scene.height
  10. context.objects = {}
  11. for _, entry in ipairs(context.scene.objects) do
  12. local path = entry[1]
  13. local object = util.get(app, path .. '.object') or util.get(app, path)
  14. local instance = object:new(util.copy(entry), config)
  15. context.objects[instance] = instance
  16. if entry.key then
  17. instance._key = entry.key
  18. context.objects[entry.key] = instance
  19. context[entry.key] = instance
  20. end
  21. end
  22. end
  23. function context.unload()
  24. for object in pairs(context.objects) do
  25. f.try(object.unbind, object)
  26. end
  27. context.objects = nil
  28. context.view:unbind()
  29. context.particles:unbind()
  30. context.collision:unbind()
  31. end
  32. function context:addObject(class, props)
  33. local object = class:new(props)
  34. self.objects[object] = object
  35. return object
  36. end
  37. function context:removeObject(object)
  38. self.objects[object] = nil
  39. self.collision:remove(object)
  40. if object._key then
  41. self.objects[object._key] = nil
  42. end
  43. end
  44. function context:getObject(object)
  45. return self.objects[object]
  46. end
  47. return context