我试图用javascript编写游戏引擎,我想做的是在游戏代码执行之前加载所有引擎代码。现在,这只是第一步,因为我还想做的是将游戏代码中的所有引擎代码加载到一个文件中。所以我会有一个启动文件,我加载所有引擎代码,返回主游戏对象并返回游戏代码。
define(['engine'[,...[]]], function(engine){
// do what you want in order for you to make a game
});
在我的引擎文件中,我会加载所有引擎代码,例如:
requirejs.config({
baseUrl: '/js/libraries/engine',
paths: {
message : "message",
debug : "debug",
vector2 : "helpers/vector2",
settings : "settings",
engine : "engine",
loop : "loop",
extra : "extras",
scene : "scene",
scenemanager : "scenemanager",
audio : "audio",
input : "input",
canvas : "canvas",
drawable : "drawables/drawable",
sprite : "drawables/sprite",
animation : "drawables/animation",
boxcollider : "colliders/boxcollider",
spherecollider : "colliders/spherecollider",
actor : "actor",
world : "world"
}
});
require(["message", "debug", "vector2", "settings", "engine", "loop", "extra", "scene", "scenemanager",
"audio", "input","canvas","drawable","sprite","animation","boxcollider", "spherecollider", "actor", "world"]
, function(
message, debug, vector2, settings, engine, loop, extra, scene, scenemanager,
audio, input, canvas, drawable, sprite, animation, boxcollider, spherecollider, actor, world)
{
return engine;
}
);
基本上,engine.js文件将成为引擎代码的清单,我只想加载engine.js文件以访问那里定义的所有组件。我目前还没看到如何做到这一点。
答案 0 :(得分:0)
要创建单个(可能缩小的)文件,请使用RequireJS optimiser。
就文件的布局而言:
引擎在这里定义,并且依赖于所有其余的引擎子组件。
define(["message", "debug", "vector2", "settings", "loop", "extra", "scene", "scenemanager",
"audio", "input","canvas","drawable","sprite","animation","boxcollider", "spherecollider", "actor", "world"]
, function(
message, debug, vector2, settings, loop, extra, scene, scenemanager,
audio, input, canvas, drawable, sprite, animation, boxcollider, spherecollider, actor, world) {
var engine = ...;
return engine;
}
);
<head>
...
<script>
require.config({
baseUrl: '/js/libraries/',
paths: {
engine : "engine",
message : "message",
debug : "debug",
vector2 : "helpers/vector2",
settings : "settings",
engine : "engine",
loop : "loop",
extra : "extras",
scene : "scene",
scenemanager : "scenemanager",
audio : "audio",
input : "input",
canvas : "canvas",
drawable : "drawables/drawable",
sprite : "drawables/sprite",
animation : "drawables/animation",
boxcollider : "colliders/boxcollider",
spherecollider : "colliders/spherecollider",
actor : "actor",
world : "world"
}
});
</script>
<script>
require(['engine'], function(engine) {
// do something with the game
});
</script>
...
</head>