game.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. -- Copyright (c) 2012-2025 Daniele Bartolini et al.
  2. -- SPDX-License-Identifier: MIT
  3. require "core/game/camera"
  4. GameBase = GameBase or {
  5. world = nil, -- Default world
  6. camera_unit = nil, -- Default camera
  7. game = nil, -- User Game
  8. game_level = nil,
  9. screen_gui = nil,
  10. show_help = true,
  11. show_build = false,
  12. _test_package = nil,
  13. }
  14. function GameBase.init()
  15. -- Create world.
  16. GameBase.world = Device.create_world()
  17. if GameBase.game and GameBase.game.init then
  18. GameBase.game.init()
  19. end
  20. -- Load test level if launched from Level Editor.
  21. if TEST then
  22. GameBase._test_package = Device.create_resource_package("_level_editor_test")
  23. ResourcePackage.load(GameBase._test_package)
  24. ResourcePackage.flush(GameBase._test_package)
  25. World.load_level(GameBase.world, "_level_editor_test")
  26. else
  27. if GameBase.game_level then
  28. World.load_level(GameBase.world, GameBase.game_level)
  29. end
  30. end
  31. -- Create default camera if it doesn't exist.
  32. GameBase.camera_unit = World.unit_by_name(GameBase.world, "camera")
  33. if GameBase.camera_unit == nil then
  34. GameBase.camera_unit = World.spawn_unit(GameBase.world, "core/units/camera")
  35. local scene_graph = World.scene_graph(GameBase.world)
  36. local camera_transform = SceneGraph.instance(scene_graph, GameBase.camera_unit)
  37. SceneGraph.set_local_position(scene_graph, camera_transform, Vector3(0, -15, 6.5))
  38. end
  39. if GameBase.game and GameBase.game.level_loaded then
  40. GameBase.game.level_loaded()
  41. end
  42. GameBase.screen_gui = World.create_screen_gui(GameBase.world)
  43. end
  44. function GameBase.update(dt)
  45. -- Update world
  46. World.update(GameBase.world, dt)
  47. if TEST then
  48. -- Stop the engine when the 'ESC' key is released.
  49. if Keyboard.released(Keyboard.button_id("escape")) then
  50. Device.quit()
  51. end
  52. end
  53. if GameBase.game and GameBase.game.update then
  54. GameBase.game.update(dt)
  55. end
  56. end
  57. function GameBase.render(dt)
  58. if GameBase.game and GameBase.game.render then
  59. GameBase.game.render(dt)
  60. end
  61. if GameBase.show_build then
  62. GameBase.draw_build()
  63. end
  64. Device.render(GameBase.world, GameBase.camera_unit)
  65. end
  66. function GameBase.shutdown()
  67. if GameBase.game and GameBase.game.shutdown then
  68. GameBase.game.shutdown()
  69. end
  70. World.destroy_gui(GameBase.world, GameBase.screen_gui)
  71. Device.destroy_world(GameBase.world)
  72. if GameBase._test_package then
  73. Device.destroy_resource_package(GameBase._test_package)
  74. end
  75. end
  76. function GameBase.draw_help(controls, title)
  77. if not GameBase.show_help then
  78. return
  79. end
  80. local window_w, window_h = Device.resolution()
  81. local line_y = window_h - 60
  82. local key_x = 40
  83. local desc_x = key_x + 180
  84. local title_size = 32
  85. local title_margin = title_size + 15
  86. local paragraph_size = 21
  87. local paragraph_margin = paragraph_size + 10
  88. local font = "core/game/hud/debug"
  89. local material = "core/game/hud/debug"
  90. local background_color = Color4(0, 0, 0, 200)
  91. local title_color = Color4(220, 220, 220, 255)
  92. local paragraph_color = Color4(200, 200, 200, 255)
  93. Gui.rect(GameBase.screen_gui, Vector2(0, 0), Vector2(window_w, window_h), background_color)
  94. Gui.text(GameBase.screen_gui, Vector2(key_x, line_y), title_size, title, font, material, title_color)
  95. line_y = line_y - title_margin
  96. for _, v in pairs(controls) do
  97. Gui.text(GameBase.screen_gui, Vector2(key_x, line_y), paragraph_size, v.key, font, material, paragraph_color)
  98. Gui.text(GameBase.screen_gui, Vector2(desc_x, line_y), paragraph_size, v.desc, font, material, paragraph_color)
  99. line_y = line_y - paragraph_margin
  100. end
  101. end
  102. function GameBase.draw_build()
  103. local window_w, window_h = Device.resolution()
  104. local font_size = 14
  105. local font = "core/game/hud/debug"
  106. local material = "core/game/hud/debug"
  107. local color = Color4(255, 255, 255, 160)
  108. local build = Device.version()
  109. .. " " .. Device.platform()
  110. .. " " .. Device.architecture() .. " "
  111. .. "(" .. Device.build() .. ")"
  112. local extents = Gui.text_extents(GameBase.screen_gui, font_size, build, font)
  113. local position = Vector2(window_w - extents.x - 12, 12)
  114. Gui.text(GameBase.screen_gui, position, font_size, build, font, material, color)
  115. end