2
0

game.lua 3.0 KB

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