game.lua 3.2 KB

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