Skip to content

Commit

Permalink
samples: draw help menu
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartolini committed Nov 12, 2024
1 parent fd68bc8 commit 4204324
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions samples/01-physics/boot.package
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ unit = [
level = [
"levels/test"
]
font = [
"core/game/hud/debug"
]
material = [
"core/game/hud/debug"
]
15 changes: 15 additions & 0 deletions samples/01-physics/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ function Game.update(dt)
Window.set_cursor_mode(Game.cursor.modes[Game.cursor.current_mode])
end

-- Toggle help.
if Keyboard.pressed(Keyboard.button_id("f1")) then
GameBase.show_help = not GameBase.show_help
end

GameBase.draw_help({{ key = "f1", desc = "Toggle help" },
{ key = "w/a/s/d", desc = "Move" },
{ key = "left click", desc = "Shoot" },
{ key = "space", desc = "Toggle mouse lock" },
{ key = "z", desc = "Toggle physics debug" },
{ key = "x", desc = "Toggle graphics debug" },
{ key = "esc", desc = "Quit" }}
, "Crown Physics Sample"
)

-- Update camera.
local delta = Mouse.axis(Mouse.axis_id("cursor_delta"))
Game.camera:update(dt, delta.x, delta.y)
Expand Down
6 changes: 6 additions & 0 deletions samples/02-animation/boot.package
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ unit = [
level = [
"levels/game"
]
font = [
"core/game/hud/debug"
]
material = [
"core/game/hud/debug"
]
12 changes: 12 additions & 0 deletions samples/02-animation/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ function Game.update(dt)
else
AnimationStateMachine.trigger(Game.sm, asm, "idle")
end

-- Toggle help.
if Keyboard.pressed(Keyboard.button_id("f1")) then
GameBase.show_help = not GameBase.show_help
end

GameBase.draw_help({{ key = "f1", desc = "Toggle help" },
{ key = "w/a/s/d", desc = "Move" },
{ key = "j", desc = "Switch characters" },
{ key = "esc", desc = "Quit" }}
, "Crown Animation Sample"
)
end

function Game.render(dt)
Expand Down
35 changes: 35 additions & 0 deletions samples/core/game/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ GameBase = GameBase or {
camera_unit = nil, -- Default camera
game = nil, -- User Game
game_level = nil,
screen_gui = nil,
show_help = true,

_test_package = nil,
}
Expand Down Expand Up @@ -41,6 +43,8 @@ function GameBase.init()
if GameBase.game and GameBase.game.level_loaded then
GameBase.game.level_loaded()
end

GameBase.screen_gui = World.create_screen_gui(GameBase.world)
end

function GameBase.update(dt)
Expand Down Expand Up @@ -72,9 +76,40 @@ function GameBase.shutdown()
GameBase.game.shutdown()
end

World.destroy_gui(GameBase.world, GameBase.screen_gui)
Device.destroy_world(GameBase.world)

if GameBase._test_package then
Device.destroy_resource_package(GameBase._test_package)
end
end

function GameBase.draw_help(controls, title)
if not GameBase.show_help then
return
end

local window_w, window_h = Device.resolution()
local line_y = window_h - 60
local key_x = 40
local desc_x = key_x + 180
local title_size = 32
local title_margin = title_size + 15
local paragraph_size = 21
local paragraph_margin = paragraph_size + 10
local font = "core/game/hud/debug"
local material = "core/game/hud/debug"
local background_color = Color4(0, 0, 0, 200)
local title_color = Color4(220, 220, 220, 255)
local paragraph_color = Color4(200, 200, 200, 255)

Gui.rect(GameBase.screen_gui, Vector2(0, 0), Vector2(window_w, window_h), background_color)
Gui.text(GameBase.screen_gui, Vector2(key_x, line_y), title_size, title, font, material, title_color)
line_y = line_y - title_margin

for _, v in pairs(controls) do
Gui.text(GameBase.screen_gui, Vector2(key_x, line_y), paragraph_size, v.key, font, material, paragraph_color)
Gui.text(GameBase.screen_gui, Vector2(desc_x, line_y), paragraph_size, v.desc, font, material, paragraph_color)
line_y = line_y - paragraph_margin
end
end

0 comments on commit 4204324

Please sign in to comment.