game.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. objects = {}
  2. max_objects = 70
  3. function init()
  4. -- Set the title of the main window
  5. Window.set_title("Hello world!")
  6. -- Create the resource package
  7. package = Device.create_resource_package("level")
  8. -- Load resource package
  9. ResourcePackage.load(package)
  10. -- Wait for completion
  11. ResourcePackage.flush(package)
  12. -- Create world
  13. world = Device.create_world()
  14. -- Spawn camera
  15. camera_unit = World.spawn_unit(world, "units/camera")
  16. camera = Unit.camera(camera_unit, "camera")
  17. -- Setup camera
  18. Camera.set_near_clip_distance(camera, 0.01)
  19. Camera.set_far_clip_distance(camera, 1000)
  20. Camera.set_projection_type(camera, Camera.ORTHOGRAPHIC)
  21. Camera.set_orthographic_metrics(camera, -6, 6, -6 / 1.6, 6 / 1.6)
  22. Camera.set_viewport_metrics(camera, 0, 0, 1000, 625)
  23. Unit.set_local_position(camera_unit, Vector3(0, 0, 1))
  24. local unit_names = { "units/star", "units/circle", "units/pentagon", "units/square" }
  25. -- Spawn units randomly
  26. math.randomseed(os.time())
  27. for i = 1, max_objects do
  28. objects[i] = {
  29. unit = World.spawn_unit(world, unit_names[math.random(#unit_names)],
  30. Vector3(math.random(-6, 6), math.random(-6 / 1.6, 6 / 1.6), 0)),
  31. rot_speed = math.random(1.5),
  32. cur_rot = math.random(3.14)
  33. }
  34. end
  35. end
  36. function frame(dt)
  37. -- Stop the engine when the 'ESC' key is released
  38. if Keyboard.button_released(Keyboard.ESCAPE) then
  39. Device.stop()
  40. end
  41. for i = 1, max_objects do
  42. local obj = objects[i]
  43. local r = obj.rot_speed
  44. obj.cur_rot = obj.cur_rot + r * dt
  45. Unit.set_local_rotation(obj.unit, Quaternion(Vector3(0, 0, 1), obj.cur_rot))
  46. end
  47. Device.render_world(world, camera, dt)
  48. end
  49. function shutdown()
  50. -- Destroy all units
  51. for i = 1, max_objects do
  52. World.destroy_unit(world, objects[i].unit)
  53. end
  54. World.destroy_unit(world, camera_unit)
  55. -- Destroy world
  56. Device.destroy_world(world)
  57. -- Unload package
  58. ResourcePackage.unload(package)
  59. Device.destroy_resource_package(package)
  60. end