game.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. require "lua/camera"
  2. local wd = wd or nil
  3. local pw = pw or nil
  4. local rw = rw or nil
  5. local sg = sg or nil
  6. local camera = camera or nil
  7. local physics_debug = physics_debug or false
  8. local graphics_debug = graphics_debug or false
  9. local fpscamera = fpscamera or nil
  10. local move = false
  11. function init()
  12. -- Set the title of the main window
  13. Window.set_title("01-physics")
  14. Device.enable_resource_autoload(true)
  15. -- Create World
  16. wd = Device.create_world()
  17. pw = World.physics_world(wd)
  18. rw = World.render_world(wd)
  19. sg = World.scene_graph(wd)
  20. -- Spawn camera
  21. camera_unit = World.spawn_unit(wd, "core/units/camera")
  22. local camera_tr = SceneGraph.transform_instances(sg, camera_unit)
  23. SceneGraph.set_local_position(sg, camera_tr, Vector3(0, 1.5, -20))
  24. camera = World.camera(wd, camera_unit)
  25. World.set_camera_viewport_metrics(wd, camera, 0, 0, 1000, 625)
  26. -- Spawn light
  27. World.spawn_unit(wd, "core/units/light", Vector3(-25, 25, -25))
  28. -- Spawn skydome
  29. skydome = World.spawn_unit(wd, "skydome")
  30. local skydome_transform = SceneGraph.transform_instances(sg, skydome)
  31. SceneGraph.set_local_scale(sg, skydome_transform, Vector3(500, 500, 500))
  32. -- Spawn ground
  33. World.spawn_unit(wd, "plane", Vector3(0, 0, 0))
  34. -- Load test level
  35. World.load_level(wd, "test", Vector3(0, 0.1, 0))
  36. -- Spawn additional stuff
  37. World.spawn_unit(wd, "ramp", Vector3(0, 0, 25), Quaternion.look(Vector3(1, 0, 0)))
  38. -- Debug stuff
  39. PhysicsWorld.enable_debug_drawing(pw, physics_debug)
  40. RenderWorld.enable_debug_drawing(rw, graphics_debug)
  41. -- Spawn FPS camera
  42. fpscamera = FPSCamera(wd, camera_unit)
  43. end
  44. function update(dt)
  45. -- Update wd
  46. World.update(wd, dt)
  47. -- Stop the engine when the 'ESC' key is released
  48. if Keyboard.released(Keyboard.button_id("escape")) then
  49. Device.quit()
  50. end
  51. if Keyboard.released(Keyboard.button_id("z")) then
  52. physics_debug = not physics_debug
  53. PhysicsWorld.enable_debug_drawing(pw, physics_debug)
  54. end
  55. if Keyboard.released(Keyboard.button_id("x")) then
  56. graphics_debug = not graphics_debug
  57. RenderWorld.enable_debug_drawing(rw, graphics_debug)
  58. end
  59. -- Spawn a sphere when left mouse button is pressed
  60. if Mouse.pressed(Mouse.button_id("left")) then
  61. local camera_transform = SceneGraph.transform_instances(sg, camera_unit)
  62. local pos = SceneGraph.local_position(sg, camera_transform)
  63. local dir = Matrix4x4.z(SceneGraph.local_pose(sg, camera_transform))
  64. local u1 = World.spawn_unit(wd, "sphere", pos)
  65. Vector3.normalize(dir)
  66. local a1 = PhysicsWorld.actor_instances(pw, u1)
  67. PhysicsWorld.add_actor_impulse(pw, a1, dir * 500.0)
  68. end
  69. -- Perform a raycast when middle mouse button is pressed
  70. if Mouse.pressed(Mouse.button_id("middle")) then
  71. local camera_transform = SceneGraph.transform_instances(sg, camera_unit)
  72. local pos = SceneGraph.local_position(sg, camera_transform)
  73. local dir = Matrix4x4.z(SceneGraph.local_pose(sg, camera_transform))
  74. local hits = PhysicsWorld.raycast(pw, pos, dir, 100, "closest")
  75. if #hits > 0 then
  76. PhysicsWorld.add_actor_impulse(pw, hits[1], dir * 400.0)
  77. end
  78. end
  79. -- Update camera
  80. local delta = Vector3.zero()
  81. if Mouse.pressed(Mouse.button_id("right")) then move = true end
  82. if Mouse.released(Mouse.button_id("right")) then move = false end
  83. if move then delta = Mouse.axis(Mouse.axis_id("cursor_delta")) end
  84. fpscamera:update(-delta.x, -delta.y)
  85. end
  86. function render(dt)
  87. local win_w, win_h = Device.resolution()
  88. World.set_camera_aspect(wd, camera, win_w/win_h)
  89. World.set_camera_viewport_metrics(wd, camera, 0, 0, win_w, win_h)
  90. Device.render_world(wd, fpscamera:camera())
  91. end
  92. function shutdown()
  93. Device.destroy_world(wd)
  94. end