main.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. -- Copyright (c) 2012-2025 Daniele Bartolini et al.
  2. -- SPDX-License-Identifier: MIT
  3. require "core/game/camera"
  4. Game = Game or {
  5. dl = nil
  6. }
  7. GameBase.game = Game
  8. GameBase.game_level = nil
  9. function Game.init()
  10. -- Override default camera's settings
  11. local camera = World.camera_instance(GameBase.world, GameBase.camera_unit)
  12. World.camera_set_projection_type(GameBase.world, camera, "orthographic")
  13. World.camera_set_orthographic_size(GameBase.world, camera, 7)
  14. local scene_graph = World.scene_graph(GameBase.world)
  15. local camera_transform = SceneGraph.instance(scene_graph, GameBase.camera_unit)
  16. SceneGraph.set_local_position(scene_graph, camera_transform, Vector3(0, -1, 0))
  17. -- Create debug line to draw joypad's buttons
  18. Game.dl = World.create_debug_line(GameBase.world, true)
  19. end
  20. function Game.update(dt)
  21. -- Stop the engine when the 'ESC' key is released
  22. if Keyboard.released(Keyboard.button_id("escape")) then
  23. Device.quit()
  24. end
  25. local function axes(center, pad_axis)
  26. local c = center
  27. local a = pad_axis
  28. local r = c + a
  29. r.z = 0.0
  30. local k = c + a
  31. k.x = c.x - 1.5
  32. k.y = c.y + k.z
  33. k.z = 0.0
  34. return r, k
  35. end
  36. local c = Vector3(-2, 0, 0)
  37. local d = Vector3( 2, 0, 0)
  38. local a = Pad1.axis(Pad1.axis_id("left")) + Pad1.axis(Pad1.axis_id("trigger_left"))
  39. local b = Pad1.axis(Pad1.axis_id("right")) + Pad1.axis(Pad1.axis_id("trigger_right"))
  40. local r, k = axes(c, a)
  41. local t, l = axes(d, b)
  42. -- Left thumb
  43. DebugLine.add_circle(Game.dl, c, 1.0, Vector3.forward(), Color4.red())
  44. DebugLine.add_circle(Game.dl, r, 0.1, Vector3.forward(), Pad1.button(Pad1.button_id("thumb_left")) == 1 and Color4.green() or Color4.red())
  45. DebugLine.add_circle(Game.dl, k, 0.1, Vector3.forward(), Color4.red())
  46. -- Right thumb
  47. DebugLine.add_circle(Game.dl, d, 1.0, Vector3.forward(), Color4.red())
  48. DebugLine.add_circle(Game.dl, t, 0.1, Vector3.forward(), Pad1.button(Pad1.button_id("thumb_right")) == 1 and Color4.green() or Color4.red())
  49. DebugLine.add_circle(Game.dl, l, 0.1, Vector3.forward(), Color4.red())
  50. -- Left/right shoulder
  51. DebugLine.add_circle(Game.dl, Vector3(0, 0, 1.5) + c, 0.1, Vector3.forward(), Pad1.button(Pad1.button_id("shoulder_left")) == 1 and Color4.green() or Color4.red())
  52. DebugLine.add_circle(Game.dl, Vector3(0, 0, 1.5) + d, 0.1, Vector3.forward(), Pad1.button(Pad1.button_id("shoulder_right")) == 1 and Color4.green() or Color4.red())
  53. -- Dpad
  54. local upos = Vector3( 0, 0, 1)
  55. local dpos = Vector3( 0, 0, -1)
  56. local lpos = Vector3(-1, 0, 0)
  57. local rpos = Vector3( 1, 0, 0)
  58. local orig = Vector3(-4, 0, 3)
  59. DebugLine.add_circle(Game.dl, orig + upos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("up")) == 1 and Color4.green() or Color4.red())
  60. DebugLine.add_circle(Game.dl, orig + dpos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("down")) == 1 and Color4.green() or Color4.red())
  61. DebugLine.add_circle(Game.dl, orig + lpos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("left")) == 1 and Color4.green() or Color4.red())
  62. DebugLine.add_circle(Game.dl, orig + rpos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("right")) == 1 and Color4.green() or Color4.red())
  63. -- Buttons
  64. local xpos = Vector3(-1, 0, 0)
  65. local ypos = Vector3( 0, 0, 1)
  66. local bpos = Vector3( 1, 0, 0)
  67. local apos = Vector3( 0, 0, -1)
  68. local o = Vector3(4, 0, 3)
  69. DebugLine.add_circle(Game.dl, o + apos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("a")) == 1 and Color4.green() or Color4.red())
  70. DebugLine.add_circle(Game.dl, o + bpos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("b")) == 1 and Color4.green() or Color4.red())
  71. DebugLine.add_circle(Game.dl, o + xpos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("x")) == 1 and Color4.green() or Color4.red())
  72. DebugLine.add_circle(Game.dl, o + ypos, 0.5, Vector3.forward(), Pad1.button(Pad1.button_id("y")) == 1 and Color4.green() or Color4.red())
  73. -- Extra
  74. local start_pos = Vector3( 1, 0, 0)
  75. local back_pos = Vector3(-1, 0, 0)
  76. local guide_pos = Vector3( 0, 0, 0)
  77. local hh = Vector3(0, 0, 2)
  78. DebugLine.add_circle(Game.dl, hh + start_pos, 0.3, Vector3.forward(), Pad1.button(Pad1.button_id("start")) == 1 and Color4.green() or Color4.red())
  79. DebugLine.add_circle(Game.dl, hh + back_pos, 0.3, Vector3.forward(), Pad1.button(Pad1.button_id("back")) == 1 and Color4.green() or Color4.red())
  80. DebugLine.add_circle(Game.dl, hh + guide_pos, 0.4, Vector3.forward(), Pad1.button(Pad1.button_id("guide")) == 1 and Color4.green() or Color4.red())
  81. DebugLine.submit(Game.dl)
  82. DebugLine.reset(Game.dl)
  83. end
  84. function Game.render(dt)
  85. end
  86. function Game.shutdown()
  87. World.destroy_debug_line(GameBase.world, Game.dl)
  88. end