I am making a multiplayer HTML5 game, and have chosen Node.JS so that I can write the game engine and use it both on the client side (For collision detection without network latency) and on the server side (To drive the main game and verify movements and such).
(我正在制作一款多人HTML5游戏,并且选择了Node.JS,以便可以编写游戏引擎并在客户端(用于无网络延迟的冲突检测)和服务器端(用于驱动主游戏和验证动作等)。)
For example, GameEngine.js
requires Collision.js
, which requires Primitives.js
.
(例如, GameEngine.js
需要Collision.js
,而Collision.js
需要Primitives.js
。)
On my frint-end (for clients) I do this:(在我的客户端上(针对客户),我这样做:)
<script src="js/Primitives.js"></script>
<script src="js/Collision.js"></script>
<script src="js/GameEngine.js"></script>
This puts everything in the global scope and I just basically use anything I need:
(这将所有内容置于全局范围内,而我基本上只是使用所需的任何内容:)
In Primitives.js
:
(在Primitives.js
:)
class Rect {
// ...
}
In Collision.js
:
(在Collision.js
:)
class CollisionObject extends Rect {
// ...
}
In Node.JS, things are modular, so I can't just import things like that.
(在Node.JS中,事物是模块化的,所以我不能仅仅导入类似的东西。)
My first solution was something like this, if placed at the top of Collision.js
:(如果放在Collision.js
的顶部,我的第一个解决方案是这样的:)
if (typeof module !== 'undefined'){
const {Rect} = require("./Primitives");
}
A solution I quickly abandoned because it obviously only places Rect
in a scope which immediately dies.
(我很快放弃了一个解决方案,因为它显然只会将Rect
放在立即消失的范围内。)
Next, I started using the terrible global
object and that works but I do not approve of this solution.
(接下来,我开始使用可怕的global
对象,并且该方法可行,但我不同意该解决方案。)
What's the solution here?(这里有什么解决方案?)
I am willing to modify both how the client loads scripts AND how the server loads scripts in order to achieve my dreams.
(我愿意修改客户端加载脚本的方式以及服务器加载脚本的方式,以实现自己的梦想。)
ask by AlgoRythm translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…