main.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. -- Copyright (c) 2012-2023 Daniele Bartolini et al.
  2. -- SPDX-License-Identifier: MIT
  3. require "core/game/camera"
  4. Game = Game or {
  5. pw = nil,
  6. rw = nil,
  7. sg = nil,
  8. debug_graphics = false,
  9. debug_physics = false,
  10. camera = nil,
  11. }
  12. GameBase.game = Game
  13. GameBase.game_level = "levels/test"
  14. local cursor_modes = {"normal", "disabled"}
  15. local cursor_mode_nxt_idx = 2
  16. function Game.level_loaded()
  17. Game.pw = World.physics_world(GameBase.world)
  18. Game.rw = World.render_world(GameBase.world)
  19. Game.sg = World.scene_graph(GameBase.world)
  20. Game.camera = FPSCamera(GameBase.world, GameBase.camera_unit)
  21. -- Debug
  22. PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
  23. RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
  24. end
  25. function Game.update(dt)
  26. -- Stop the engine when the 'ESC' key is released
  27. if Keyboard.released(Keyboard.button_id("escape")) then
  28. Device.quit()
  29. end
  30. if Keyboard.released(Keyboard.button_id("z")) then
  31. debug_physics = not debug_physics
  32. PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
  33. end
  34. if Keyboard.released(Keyboard.button_id("x")) then
  35. debug_graphics = not debug_graphics
  36. RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
  37. end
  38. -- Spawn a sphere when left mouse button is pressed
  39. if Mouse.pressed(Mouse.button_id("left")) then
  40. local tr = SceneGraph.instance(Game.sg, Game.camera:unit())
  41. local pos = SceneGraph.local_position(Game.sg, tr)
  42. local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, tr))
  43. local u1 = World.spawn_unit(GameBase.world, "units/sphere", pos)
  44. local a1 = PhysicsWorld.actor_instance(Game.pw, u1)
  45. Vector3.normalize(dir)
  46. PhysicsWorld.actor_add_impulse(Game.pw, a1, dir * 500.0)
  47. end
  48. -- Perform a raycast when middle mouse button is pressed
  49. if Mouse.pressed(Mouse.button_id("middle")) then
  50. local tr = SceneGraph.instance(Game.sg, Game.camera:unit())
  51. local pos = SceneGraph.local_position(Game.sg, tr)
  52. local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, tr))
  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