main.lua 2.3 KB

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