game.lua 2.7 KB

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