| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
require "dokidoki.module" [[make_script, make_blueprint, make_game]] |
| 8 |
|
| 9 |
import(require "dokidoki.base") |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
function make_script (name, init) |
| 18 |
return {name = name, init = init} |
| 19 |
end |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
function make_blueprint(name, ...) |
| 27 |
return {name = name, ...} |
| 28 |
end |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 91 |
|
| 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 |
|
| 100 |
|
| 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 |
|
| 110 |
|
| 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 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 170 |
local script_env = {self = actor, game = game} |
| 171 |
|
| 172 |
|
| 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 |
|
| 179 |
for k, v in pairs(script_spec) do |
| 180 |
if k ~= 1 then script_env[k] = v end |
| 181 |
end |
| 182 |
|
| 183 |
|
| 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 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 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 |
|
| 248 |
|
| 249 |
|
| 250 |
function game.actors.get (tag) |
| 251 |
return actors_by_tag[tag] or {} |
| 252 |
end |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 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 |
|
| 267 |
if init then |
| 268 |
init(game) |
| 269 |
init = false |
| 270 |
end |
| 271 |
|
| 272 |
|
| 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 |
|
| 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 |
|
| 302 |
return { handle_event = handle_event, update = update, draw = draw } |
| 303 |
end |
| 304 |
|
| 305 |
return get_module_exports() |