Debian Codegraph @

dokidoki/game.lua

raw symbols SCIP
1 --- dokidoki.game
2 --- =============
3
4 --- Implementation
5 --- --------------
6
7 require "dokidoki.module" [[make_script, make_blueprint, make_game]]
8
9 import(require "dokidoki.base")
10
11 --- ### `make_script(name, init)`
12 --- Returns a new script with the given name and initialization function.
13 --- Whenever an instance of this script is to be initialized, `init` has its
14 --- environment set to be the script instance, so that any global variable
15 --- assignments are local to that script, and any initialization arguments are
16 --- available in the environment.
17 function make_script (name, init)
18 return {name = name, init = init}
19 end
20
21 --- ### `make_blueprint(name, {script, key=value...}...)`
22 --- Returns a new blueprint with the given name and scripts. Each script can be
23 --- given a number of key-value pairs, which are stored as variables in the
24 --- script's environment. Variables set in `game.new()` take precedence over
25 --- these variables.
26 function make_blueprint(name, ...)
27 return {name = name, ...}
28 end
29
30 --- ### `make_game(update_methods, draw_methods, init)`
31 --- Makes a game with the given update and draw phases. A scene for use with
32 --- `kernel.start_with_scene()` is returned, and during the first update `init`
33 --- is called with the game as a parameter.
34 function make_game (update_methods, draw_methods, init)
35 local game = {}
36 game.actors = {}
37
38 local actors_by_tag = {}
39 local scripts_by_method = {}
40
41 scripts_by_method = {}
42 scripts_by_method.handle_event = {}
43 for _, method in ipairs(update_methods) do scripts_by_method[method] = {} end
44 for _, method in ipairs(draw_methods) do scripts_by_method[method] = {} end
45
46 game.make_script = make_script
47 game.make_blueprint = make_blueprint
48
49 -- delayed_require(name)
50 -- Works like the regular require except that it returns a function
51 -- representing the body of the module instead of running it. Also, if the
52 -- module is not found, instead of signalling an error, nil and an error
53 -- message are returned. Bypasses the global packages.loaded table for
54 -- obvious reasons.
55 local function delayed_require(name)
56 local errors = {}
57 for _, loader in ipairs(package.loaders) do
58 local module = loader(name)
59 if type(module) == 'function' then
60 return module
61 else
62 table.insert(errors, module)
63 end
64 end
65 return nil, table.concat(errors)
66 end
67
68 local function generic_load(kind, constructor, cache, prefixes, name)
69 local errors = {}
70
71 if not cache[name] then
72 for _, prefix in ipairs(prefixes) do
73 local thunk, err = delayed_require(prefix .. name)
74 if thunk then
75 cache[name] = constructor(name, thunk)
76 else
77 table.insert(errors, err)
78 end
79 end
80 end
81
82 if not cache[name] then
83 error('couldn\'t find requested ' .. kind .. ' "' .. name .. '"' ..
84 table.concat(errors))
85 end
86
87 return cache[name]
88 end
89
90 -- load_script(name)
91 -- Loads a script by name.
92 local loaded_scripts = {}
93 local script_prefixes = {'scripts.', 'dokidoki.scripts.'}
94 local function load_script(name)
95 return generic_load('script', make_script, loaded_scripts, script_prefixes,
96 name)
97 end
98
99 -- game.load_component(name)
100 -- Loads a component by name.
101 local loaded_components = {}
102 local component_prefixes = {'components.', 'dokidoki.components.'}
103 local function load_component(name)
104 local function make_component(_, component) return component end
105 return generic_load('component', make_component, loaded_components,
106 component_prefixes, name)
107 end
108
109 --- ### `game.init_component(name)`
110 --- Loads and initializes the named component.
111 function game.init_component(name)
112 if game[name] ~= nil then
113 error('name collision with component "' .. name .. '"')
114 end
115
116 local component = {game=game}
117 game[name] = component
118
119 local component_init = load_component(name)
120 local env = getfenv(component_init)
121 setfenv(component_init, setmetatable({}, {
122 __index = function (_, k)
123 local ret = component[k]
124 if ret ~= nil then
125 return ret
126 else
127 return env[k]
128 end
129 end,
130 __newindex = function (_, k, v)
131 component[k] = v
132 end
133 }))
134 component_init(name)
135 setfenv(component_init, env)
136 end
137
138 --- ### `game.actors.new(blueprint, {script, key=value...}...)`
139 --- Instantiates a blueprint and adds the actor to the scene. Optionally,
140 --- initialization arguments can be given to any script in the blueprint.
141 --- These initialization arguments take precedence over the ones given in the
142 --- blueprint definition.
143 function game.actors.new(blueprint, ...)
144 local arguments = {}
145 for _, arg in ipairs{...} do
146 local script = arg[1]
147 if type(script) == 'string' then
148 script = load_script(script)
149 end
150 arguments[script] = arg
151 end
152
153 local actor = {
154 blueprint = blueprint,
155 tags = {[blueprint.name] = true},
156 dead = false,
157 paused = false,
158 hidden = false
159 }
160
161 for _, script_spec in ipairs(blueprint) do
162 local script = script_spec[1]
163 if type(script) == 'string' then
164 script = load_script(script)
165 end
166 local script_name = script.name
167 local script_init = script.init
168
169 -- create script environment
170 local script_env = {self = actor, game = game}
171
172 -- add it to the actor, checking that there's no script collision
173 if actor[script_name] ~= nil then
174 error('script name collision for "' .. script_name .. "'")
175 end
176 actor[script_name] = script_env
177
178 -- initialize default values from blueprint
179 for k, v in pairs(script_spec) do
180 if k ~= 1 then script_env[k] = v end
181 end
182
183 -- add constructor values
184 if arguments[script] then
185 for k, v in pairs(arguments[script]) do
186 if k ~= 1 then script_env[k] = v end
187 end
188 end
189
190 -- initialize the script
191
192 -- the script's global environment is its table in the actor, chaining up
193 -- to its original environment (usually the default global environment)
194 --
195 -- currently this is done using a dispatch function. this might be slow.
196 --
197 -- one alternative is to set the __index to be the script table itself,
198 -- with the script table chaining to global. this would be faster but
199 -- would expose the global environment through the script's table, which
200 -- would be wierd.
201 --
202 -- another alternative is to add a reference to _G to the script table
203 -- and not use chaining at all. this would probably be annoying for
204 -- script writing though.
205 local env = getfenv(script_init)
206 setfenv(script_init, setmetatable({}, {
207 __index = function (_, k)
208 local ret = script_env[k]
209 if ret ~= nil then
210 return ret
211 else
212 return env[k]
213 end
214 end,
215 __newindex = function (_, k, v)
216 script_env[k] = v
217 end
218 }))
219 script_init(script_name)
220 setfenv(script_init, env)
221
222 -- index the script by its methods
223 for method, t in pairs(scripts_by_method) do
224 if script_env[method] then t[#t+1] = script_env end
225 end
226 end
227
228 -- index the actor by its tags
229 for tag in pairs(actor.tags or {}) do
230 actors_by_tag[tag] = actors_by_tag[tag] or {}
231 table.insert(actors_by_tag[tag], actor)
232 end
233
234 return actor
235 end
236
237 --- ### `game.actors.new_generic(name, init)`
238 --- Adds a simple one-script actor, with the script initializer given by
239 --- `init`. This is a shortcut for creating a new blueprint with one script
240 --- and then instantiating it. The generated actor will have `name` as its
241 --- name, and a single script named `generic`.
242 function game.actors.new_generic(name, init)
243 return game.actors.new(
244 make_blueprint(name, {make_script('generic', init)}))
245 end
246
247 --- ### `game.actors.get(tag)`
248 --- Returns a list of the actors with the given tag. This is an internal data
249 --- structure, so copy it if you need to make changes.
250 function game.actors.get (tag)
251 return actors_by_tag[tag] or {}
252 end
253
254 --- ### Scene Interface
255 --- The next few functions handle dispatching kernel callbacks to all
256 --- applicable actors. They aren't accessible from the game object, only from
257 --- the scene object returned by `make_game()`.
258
259 local function handle_event (event)
260 for _, a in ipairs(scripts_by_method.handle_event) do
261 a.handle_event(event)
262 end
263 end
264
265 local function update (dt)
266 -- initialize
267 if init then
268 init(game)
269 init = false
270 end
271
272 -- update all actors
273 for _, update_type in ipairs(update_methods) do
274 for _, script in ipairs(scripts_by_method[update_type]) do
275 if not script.self.dead and not script.self.paused then
276 script[update_type]() end
277 end
278 end
279
280 -- cull dead actors
281 for k, _ in pairs(scripts_by_method) do
282 scripts_by_method[k] =
283 ifilter(function (s) return not s.self.dead end, scripts_by_method[k])
284 end
285 for k, _ in pairs(actors_by_tag) do
286 actors_by_tag[k] =
287 ifilter(function (a) return not a.dead end, actors_by_tag[k])
288 end
289 end
290
291 local function draw ()
292 for _, draw_type in ipairs(draw_methods) do
293 for _, script in ipairs(scripts_by_method[draw_type]) do
294 if not script.self.hidden then
295 script[draw_type]()
296 end
297 end
298 end
299 end
300
301 -- return the scene interface for the kernel's main loop
302 return { handle_event = handle_event, update = update, draw = draw }
303 end
304
305 return get_module_exports()