game.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, 6.5, -30))
  24. -- Load test level
  25. World.load_level(wd, "test", Vector3(0, 0.1, 0))
  26. -- Debug stuff
  27. PhysicsWorld.enable_debug_drawing(pw, physics_debug)
  28. RenderWorld.enable_debug_drawing(rw, graphics_debug)
  29. -- Spawn FPS camera
  30. fpscamera = FPSCamera(wd, camera_unit)
  31. end
  32. function update(dt)
  33. -- Update wd
  34. World.update(wd, dt)
  35. -- Stop the engine when the 'ESC' key is released
  36. if Keyboard.released(Keyboard.button_id("escape")) then
  37. Device.quit()
  38. end
  39. if Keyboard.released(Keyboard.button_id("z")) then
  40. physics_debug = not physics_debug
  41. PhysicsWorld.enable_debug_drawing(pw, physics_debug)
  42. end
  43. if Keyboard.released(Keyboard.button_id("x")) then
  44. graphics_debug = not graphics_debug
  45. RenderWorld.enable_debug_drawing(rw, graphics_debug)
  46. end
  47. -- Spawn a sphere when left mouse button is pressed
  48. if Mouse.pressed(Mouse.button_id("left")) then
  49. local camera_transform = SceneGraph.transform_instances(sg, camera_unit)
  50. local pos = SceneGraph.local_position(sg, camera_transform)
  51. local dir = Matrix4x4.z(SceneGraph.local_pose(sg, camera_transform))
  52. local u1 = World.spawn_unit(wd, "sphere", pos)
  53. Vector3.normalize(dir)
  54. local a1 = PhysicsWorld.actor_instances(pw, u1)
  55. PhysicsWorld.actor_add_impulse(pw, a1, dir * 500.0)
  56. end
  57. -- Perform a raycast when middle mouse button is pressed
  58. if Mouse.pressed(Mouse.button_id("middle")) then
  59. local camera_transform = SceneGraph.transform_instances(sg, camera_unit)
  60. local pos = SceneGraph.local_position(sg, camera_transform)
  61. local dir = Matrix4x4.z(SceneGraph.local_pose(sg, camera_transform))
  62. local hits = PhysicsWorld.raycast(pw, pos, dir, 100, "closest")
  63. if #hits > 0 then
  64. PhysicsWorld.actor_add_impulse(pw, hits[1], dir * 400.0)
  65. end
  66. end
  67. -- Update camera
  68. local delta = Vector3.zero()
  69. if Mouse.pressed(Mouse.button_id("right")) then move = true end
  70. if Mouse.released(Mouse.button_id("right")) then move = false end
  71. if move then delta = Mouse.axis(Mouse.axis_id("cursor_delta")) end
  72. fpscamera:update(-delta.x, -delta.y)
  73. end
  74. function render(dt)
  75. Device.render(wd, fpscamera:camera())
  76. end
  77. function shutdown()
  78. Device.destroy_world(wd)
  79. end