javascript - How can I distribute the two pieces of a game engine in an efficient way?
问题描述
I am using VUE and JavaScript to develop an educational 2D game engine for the purpose of teaching kids game design. The breakdown of how I plan on it working is as follows:
- VUE based editor is used to create game content
- The actual engine is written in pure JS
- When the "play," button is clicked in the editor, a function from the engine
run_debug(inputGameData)
is called which returns specific errors to the editor process for it to handle if they appear IE: Display error message and highlight location in editor that the error occurred. - When the "export," button is clicked in the editor, the game data and engine are packaged together in a single HTML file to be run as a stand-alone application.
The editor is almost completed, which means I'm now thinking more about the engine side of things. What I'm really scratching my head about is how I should distribute the two on the server without having multiple duplicates of the engine; one version that is generated with the HTML wrapper template, and one that is imported into the VUE application to be run-able from the editor.
I see a few solutions to this off the top of my head, but all have drawbacks:
- Distribute all separate versions on the server. This will cause a longer page load and hurts my insides as a programmer due to how inefficient this feels
- Dynamically build the engine from source at export/run time in whichever version is needed. This would cause problems as one of my goals for accessibility is having the editor able to run locally on a machine, and browsers currently don't handle local file reading too well without having to mess about with permissions.
- Import the HTML5 wrapped version into the VUE app on build, and somehow both call it from the engine (in an Iframe or similar) and be able to read it as text so I can insert the game data on export and download it to the user's machine. I have absolutely no clue how to do this using VUE.
It's a weird niche problem so I'm having some difficulty finding resources on the topic. If anyone has experience in this subject matter I appreciate any help/direction you can give or point me to
解决方案
Here's the solution I ended up coming up with (basically option #3 from above, just slightly modified):
Create build system for the engine that generates a
engine.js
file where the contents of the file is stored in the format of:let engineCode = 'engineCodeHere'; let sharedCode = 'code shared between engine and editor goes here';
import the
engine.js
file into the vue editor and on initial load, put the code into<script>
tagsconst engineTag = document.createElement('script'); const sharedTag = document.createElement('script'); engineTag.id = 'engineCode'; sharedTag.id = 'sharedCode'; engineTag.innerHTML = engineCode; sharedTag.innerHTML = sharedCode; document.body.appendChild(engineTag); document.body.appendChild(sharedTag); engineCode = null; sharedCode = null;
once they are imported as script tags the original variables are cleared so we're not story duplicate data in memory.
Now, not only do I have access to the runnable js code as if I had imported it normally, but I also have the added benefit of being able to get that code as text so I can use it when packaging the final game "executable."
let engineText = document.getElementById('engineCode').innerHTML; let sharedText = document.getElementById('sharedCode').innerHTML;
This will allow me to pack the engine just a single time, instead of once as JS and once as text. For added efficiency I can even hold off creating the engine tag until the game is run in the editor to save on resources.
推荐阅读
- python - 需要一些帮助:_tkinter.TclError: 无法识别图像文件中的数据
- html - 浏览器渲染引擎使用盒子模型来表示html元素?a)如果是,那么浏览器可以在没有盒子模型的情况下呈现 html 元素吗?乙)?
- javascript - js循环和对象数组的问题
- c++ - C ++算法对连续的整数块求和
- javascript - Vuejs如何为表格中的每一行添加一个类
- android - 在没有单选按钮的 Jetpack Compose 中创建切换按钮组
- javascript - 如何使用按钮(不是手风琴)隐藏多个部分并在 Elementor 中仅显示一个部分?
- c# - Action Type C#可以返回一个类型吗?
- c++ - 如何处理 float *array
- matlab - 特征值和特征向量计算中的通缩