game.lua 3.1 KB

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