main.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require "core/game/camera"
  2. Game = Game or {
  3. pw = nil,
  4. rw = nil,
  5. sg = nil,
  6. debug_graphics = false,
  7. debug_physics = false,
  8. camera = nil,
  9. }
  10. GameBase.game = Game
  11. GameBase.game_level = "levels/test"
  12. local cursor_modes = {"normal", "disabled"}
  13. local cursor_mode_nxt_idx = 2
  14. function Game.level_loaded()
  15. Game.pw = World.physics_world(GameBase.world)
  16. Game.rw = World.render_world(GameBase.world)
  17. Game.sg = World.scene_graph(GameBase.world)
  18. -- Spawn camera
  19. local camera_unit = World.spawn_unit(GameBase.world, "core/units/camera")
  20. SceneGraph.set_local_position(Game.sg, camera_unit, Vector3(0, 6.5, -30))
  21. GameBase.game_camera = camera_unit
  22. Game.camera = FPSCamera(GameBase.world, camera_unit)
  23. -- Debug
  24. PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
  25. RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
  26. end
  27. function Game.update(dt)
  28. -- Stop the engine when the 'ESC' key is released
  29. if Keyboard.released(Keyboard.button_id("escape")) then
  30. Device.quit()
  31. end
  32. if Keyboard.released(Keyboard.button_id("z")) then
  33. debug_physics = not debug_physics
  34. PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
  35. end
  36. if Keyboard.released(Keyboard.button_id("x")) then
  37. debug_graphics = not debug_graphics
  38. RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
  39. end
  40. -- Spawn a sphere when left mouse button is pressed
  41. if Mouse.pressed(Mouse.button_id("left")) then
  42. local pos = SceneGraph.local_position(Game.sg, Game.camera:unit())
  43. local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, Game.camera:unit()))
  44. local u1 = World.spawn_unit(GameBase.world, "units/sphere", pos)
  45. local a1 = PhysicsWorld.actor_instances(Game.pw, u1)
  46. Vector3.normalize(dir)
  47. PhysicsWorld.actor_add_impulse(Game.pw, a1, dir * 500.0)
  48. end
  49. -- Perform a raycast when middle mouse button is pressed
  50. if Mouse.pressed(Mouse.button_id("middle")) then
  51. local pos = SceneGraph.local_position(Game.sg, Game.camera:unit())
  52. local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, Game.camera:unit()))
  53. local hit, pos, normal, time, unit, actor = PhysicsWorld.cast_ray(Game.pw, pos, dir, 100)
  54. if hit then
  55. PhysicsWorld.actor_add_impulse(Game.pw, actor, dir * 400.0)
  56. end
  57. end
  58. -- Toggle mouse cursor modes
  59. if Keyboard.released(Keyboard.button_id("space")) then
  60. Window.set_cursor_mode(cursor_modes[cursor_mode_nxt_idx])
  61. cursor_mode_nxt_idx = 1 + cursor_mode_nxt_idx % 2
  62. end
  63. -- Update camera
  64. local delta = Mouse.axis(Mouse.axis_id("cursor_delta"))
  65. Game.camera:update(dt, delta.x, delta.y)
  66. end
  67. function Game.render(dt)
  68. end
  69. function Game.shutdown()
  70. end