game.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. _test_package = nil,
  12. }
  13. function GameBase.init()
  14. -- Create world.
  15. GameBase.world = Device.create_world()
  16. if GameBase.game and GameBase.game.init then
  17. GameBase.game.init()
  18. end
  19. -- Load test level if launched from Level Editor.
  20. if TEST then
  21. GameBase._test_package = Device.create_resource_package("_level_editor_test")
  22. ResourcePackage.load(GameBase._test_package)
  23. ResourcePackage.flush(GameBase._test_package)
  24. World.load_level(GameBase.world, "_level_editor_test")
  25. else
  26. if GameBase.game_level then
  27. World.load_level(GameBase.world, GameBase.game_level)
  28. end
  29. end
  30. -- Create default camera if it doesn't exist.
  31. GameBase.camera_unit = World.unit_by_name(GameBase.world, "camera")
  32. if GameBase.camera_unit == nil then
  33. GameBase.camera_unit = World.spawn_unit(GameBase.world, "core/units/camera")
  34. local scene_graph = World.scene_graph(GameBase.world)
  35. local camera_transform = SceneGraph.instance(scene_graph, GameBase.camera_unit)
  36. SceneGraph.set_local_position(scene_graph, camera_transform, Vector3(0, -15, 6.5))
  37. end
  38. if GameBase.game and GameBase.game.level_loaded then
  39. GameBase.game.level_loaded()
  40. end
  41. GameBase.screen_gui = World.create_screen_gui(GameBase.world)
  42. end
  43. function GameBase.update(dt)
  44. -- Update world
  45. World.update(GameBase.world, dt)
  46. if TEST then
  47. -- Stop the engine when the 'ESC' key is released.
  48. if Keyboard.released(Keyboard.button_id("escape")) then
  49. Device.quit()
  50. end
  51. end
  52. if GameBase.game and GameBase.game.update then
  53. GameBase.game.update(dt)
  54. end
  55. end
  56. function GameBase.render(dt)
  57. if GameBase.game and GameBase.game.render then
  58. GameBase.game.render(dt)
  59. end
  60. Device.render(GameBase.world, GameBase.camera_unit)
  61. end
  62. function GameBase.shutdown()
  63. if GameBase.game and GameBase.game.shutdown then
  64. GameBase.game.shutdown()
  65. end
  66. World.destroy_gui(GameBase.world, GameBase.screen_gui)
  67. Device.destroy_world(GameBase.world)
  68. if GameBase._test_package then
  69. Device.destroy_resource_package(GameBase._test_package)
  70. end
  71. end
  72. function GameBase.draw_help(controls, title)
  73. if not GameBase.show_help then
  74. return
  75. end
  76. local window_w, window_h = Device.resolution()
  77. local line_y = window_h - 60
  78. local key_x = 40
  79. local desc_x = key_x + 180
  80. local title_size = 32
  81. local title_margin = title_size + 15
  82. local paragraph_size = 21
  83. local paragraph_margin = paragraph_size + 10
  84. local font = "core/game/hud/debug"
  85. local material = "core/game/hud/debug"
  86. local background_color = Color4(0, 0, 0, 200)
  87. local title_color = Color4(220, 220, 220, 255)
  88. local paragraph_color = Color4(200, 200, 200, 255)
  89. Gui.rect(GameBase.screen_gui, Vector2(0, 0), Vector2(window_w, window_h), background_color)
  90. Gui.text(GameBase.screen_gui, Vector2(key_x, line_y), title_size, title, font, material, title_color)
  91. line_y = line_y - title_margin
  92. for _, v in pairs(controls) do
  93. Gui.text(GameBase.screen_gui, Vector2(key_x, line_y), paragraph_size, v.key, font, material, paragraph_color)
  94. Gui.text(GameBase.screen_gui, Vector2(desc_x, line_y), paragraph_size, v.desc, font, material, paragraph_color)
  95. line_y = line_y - paragraph_margin
  96. end
  97. end