main.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Device.enable_resource_autoload(true)
  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, "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. -- Update camera
  59. local delta = Vector3.zero()
  60. if Mouse.button(Mouse.button_id("right")) > 0 then
  61. delta = Mouse.axis(Mouse.axis_id("cursor_delta"))
  62. end
  63. Game.camera:update(dt, delta.x, delta.y)
  64. end
  65. function Game.render(dt)
  66. end
  67. function Game.shutdown()
  68. end