在requireJS中加载一个文件字典,就像它是一个文件一样



我试图在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文件以访问其中定义的所有组件。我只是不知道现在怎么做。

要创建单个(可能是最小化的)文件,请使用RequireJS优化器。

关于文件的布局:

engine.js

engine是在这里定义的,它依赖于引擎的所有子组件。

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;
    }
);

index . html

<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>

相关内容

最新更新