1-- This is the main Lua script of your project.
2-- You will probably make a title screen and then start a game.
3-- See the Lua API! http://www.solarus-games.org/doc/latest
4
5require("scripts/features")
6local solarus_logo = require("scripts/menus/solarus_logo")
7local intro        = require("scripts/menus/intro")
8local title_screen = require("scripts/menus/title_screen")
9local inventory    = require("scripts/menus/inventory.lua")
10
11local debug_enabled = false
12function sol.main.is_debug_enabled()
13  return debug_enabled
14end
15
16-- This function is called when Solarus starts.
17function sol.main:on_started()
18
19  -- HACK: Solarus can't do certain things such as display built-in dialogs
20  --       or read joypad inputs unless a game has been started.
21  --       We start a fake game now, and it's later replaced by the real game.
22  local fakegame = sol.game.load("save1.dat")
23  fakegame:set_starting_location("0000", nil)
24  fakegame:start()
25  sol.main.game = fakegame
26
27  -- Setting a language is useful to display text and dialogs.
28  sol.language.set_language("en")
29
30  -- If there is a file called "debug" in the write directory, enable debug mode.
31  debug_enabled = sol.file.exists("debug")
32
33  -- Preload sound effects
34  sol.audio.preload_sounds()
35
36  -- Show the Solarus logo initially.
37  function fakegame:on_started()
38    sol.menu.start(fakegame, solarus_logo)
39  end
40
41  -- Intro plays after Solarus logo
42  function solarus_logo:on_finished()
43    sol.menu.start(fakegame, intro)
44  end
45
46  -- Title screen after intro
47  function intro:on_finished()
48    sol.menu.start(fakegame, title_screen)
49  end
50
51end
52
53-- Event called when the player pressed a keyboard key.
54function sol.main:on_key_pressed(key, modifiers)
55
56  local handled = false
57  if key == "f11" or
58    (key == "return" and (modifiers.alt or modifiers.control)) then
59    -- F11 or Ctrl + return or Alt + Return: switch fullscreen.
60    local is_fullscreen = sol.video.is_fullscreen()
61    sol.video.set_fullscreen(not is_fullscreen)
62    sol.video.set_cursor_visible(is_fullscreen) -- hide mouse on fullscreen
63    handled = true
64  elseif key == "f4" and modifiers.alt then
65    -- Alt + F4: stop the program.
66    sol.main.exit()
67    handled = true
68  elseif key == "escape" and sol.main.game == nil then
69    -- Escape in title screens: stop the program.
70    sol.main.exit()
71    handled = true
72  elseif key == "escape" and sol.video.is_fullscreen() then
73    -- Escape fullscreen
74    sol.video.set_fullscreen(false)
75    sol.video.set_cursor_visible(true)
76    handled = true
77  end
78
79  return handled
80end
81
82-- Starts a game.
83function sol.main:start_savegame(game)
84
85  -- Skip initial menus if any.
86  sol.menu.stop(solarus_logo)
87
88  sol.main.game = game
89
90  -- Disable game overs for this game
91  function game:on_game_over_started()
92    log("Game over technically happened")
93    game:set_life(12)
94    game:stop_game_over()
95  end
96
97  -- Returns an item for item_ command strings
98  function get_item_for_command(command)
99
100    local slot = tonumber(string.match(command, "^item_([12])$"))
101
102    if slot then
103      local item = game:get_item_assigned(slot)
104      return item
105    else
106      return nil
107    end
108  end
109
110
111  function game:on_command_pressed(command)
112    -- Don't handle any of this stuff when a dialog box is open
113    if game:is_dialog_enabled() or game:is_picker_enabled() then
114      return false
115    end
116
117    -- Disable attacking; the stick is a regular item
118    if command == "attack" then
119      return true
120    end
121
122    -- Handle items with item:on_command_pressed()
123    local item = get_item_for_command(command)
124    if item and item.on_command_pressed ~= nil then
125      item:on_command_pressed(command)
126      return true
127    else
128      -- Fall back to item:on_using()
129      return false
130    end
131
132  end
133
134  function game:on_command_released(command)
135    -- Handle items with item:on_command_released()
136    local item = get_item_for_command(command)
137    if item and item.on_command_released ~= nil then
138      item:on_command_released(command)
139      return true
140    else
141      return false
142    end
143
144  end
145
146  function game:on_paused()
147    sol.menu.start(sol.main.game, inventory)
148  end
149
150  function game:on_unpaused()
151    sol.menu.stop(inventory)
152  end
153
154  game:start()
155end
156