game.lua 2.5 KB

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