main.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. Game.camera = FPSCamera(GameBase.world, GameBase.camera_unit)
  19. -- Debug
  20. PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
  21. RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
  22. end
  23. function Game.update(dt)
  24. -- Stop the engine when the 'ESC' key is released
  25. if Keyboard.released(Keyboard.button_id("escape")) then
  26. Device.quit()
  27. end
  28. if Keyboard.released(Keyboard.button_id("z")) then
  29. debug_physics = not debug_physics
  30. PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
  31. end
  32. if Keyboard.released(Keyboard.button_id("x")) then
  33. debug_graphics = not debug_graphics
  34. RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
  35. end
  36. -- Spawn a sphere when left mouse button is pressed
  37. if Mouse.pressed(Mouse.button_id("left")) then
  38. local tr = SceneGraph.instance(Game.sg, Game.camera:unit())
  39. local pos = SceneGraph.local_position(Game.sg, tr)
  40. local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, tr))
  41. local u1 = World.spawn_unit(GameBase.world, "units/sphere", pos)
  42. local a1 = PhysicsWorld.actor_instance(Game.pw, u1)
  43. Vector3.normalize(dir)
  44. PhysicsWorld.actor_add_impulse(Game.pw, a1, dir * 500.0)
  45. end
  46. -- Perform a raycast when middle mouse button is pressed
  47. if Mouse.pressed(Mouse.button_id("middle")) then
  48. local tr = SceneGraph.instance(Game.sg, Game.camera:unit())
  49. local pos = SceneGraph.local_position(Game.sg, tr)
  50. local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, tr))
  51. local hit, pos, normal, time, unit, actor = PhysicsWorld.cast_ray(Game.pw, pos, dir, 100)
  52. if hit then
  53. PhysicsWorld.actor_add_impulse(Game.pw, actor, dir * 400.0)
  54. end
  55. end
  56. -- Toggle mouse cursor modes
  57. if Keyboard.released(Keyboard.button_id("space")) then
  58. Window.set_cursor_mode(cursor_modes[cursor_mode_nxt_idx])
  59. cursor_mode_nxt_idx = 1 + cursor_mode_nxt_idx % 2
  60. end
  61. -- Update camera
  62. local delta = Mouse.axis(Mouse.axis_id("cursor_delta"))
  63. Game.camera:update(dt, delta.x, delta.y)
  64. end
  65. function Game.render(dt)
  66. end
  67. function Game.shutdown()
  68. end