game.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, 0, 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. -- Update the world
  38. Device.update_world(world, dt)
  39. -- Stop the engine when the 'ESC' key is released
  40. if Keyboard.button_released(Keyboard.ESCAPE) then
  41. Device.stop()
  42. end
  43. for i = 1, max_objects do
  44. local obj = objects[i]
  45. local r = obj.rot_speed
  46. obj.cur_rot = obj.cur_rot + r * dt
  47. Unit.set_local_rotation(obj.unit, 0, Quaternion(Vector3(0, 0, 1), obj.cur_rot))
  48. end
  49. -- Render the world
  50. Device.render_world(world, camera)
  51. end
  52. function shutdown()
  53. -- Destroy all units
  54. for i = 1, max_objects do
  55. World.destroy_unit(world, objects[i].unit)
  56. end
  57. World.destroy_unit(world, camera_unit)
  58. -- Destroy world
  59. Device.destroy_world(world)
  60. -- Unload package
  61. ResourcePackage.unload(package)
  62. Device.destroy_resource_package(package)
  63. end