使用json解json文件
Edna Piranha is a mystery wrapped in a enigma. I never know what she's gonna say, and when she does say it, I don't know what to think of it. What I do know, however, is that she's an excellent web developer. She's created NoodleTalk.org and more recently NoodleApp.net, a Node-based web interface for App.Net. Here she waxes philosophical about using JSON for game configuration and her experiences creating a HTML5 game.
Edna Piranha是一个被谜包裹的谜。 我永远不知道她会说什么,而当她说出来时,我也不知道该怎么想。 但是,我确实知道她是一位出色的Web开发人员。 她创建了NoodleTalk.org ,最近创建了NoodleApp.net ,这是App.Net的基于节点的Web界面。 在这里,她阐述了有关使用JSON进行游戏配置以及创建HTML5游戏的经验的哲学知识。
For the past month or so, I've been spending some time making web games using Node and Express. One thing I wanted to do was reduce reliance on content management via database, where the typical process is the following:
在过去的一个月左右的时间里,我一直在花一些时间使用Node和Express制作网页游戏。 我想做的一件事是减少对数据库的内容管理的依赖,典型的过程如下:
Log into the site 登录网站 Validate that I have the right administrative permissions to add/edit/delete game content 验证我是否具有添加/编辑/删除游戏内容的正确管理权限 Change the game content and save 更改游戏内容并保存This seems to be a pretty straightforward process. You could build or use a CMS to manage your assets for your game but that might be too much for a small setup. Creating a custom CMS would eat up valuable time that could be left to doing all the fun stuff like building content (and usually the part that takes the most time). Using a third party CMS means you risk relying on their data structure and design decisions.
这似乎是一个非常简单的过程。 您可以构建或使用CMS来管理游戏资产,但是对于小的设置来说可能太多了。 创建自定义CMS会占用宝贵的时间,而这些时间可能会花在做所有有趣的事情上,例如构建内容(通常是花费时间最多的部分)。 使用第三方CMS意味着您可能要依靠他们的数据结构和设计决策。
This is where JSON comes in handy. What if you could create a game that didn't need to rely on a database, third party or custom management? What if you could edit a single JSON file (or a few) to load all your game content and whenever you needed to change it you would just edit the files and update the server?
这是JSON派上用场的地方。 如果您可以创建不需要依赖数据库,第三方或自定义管理的游戏,该怎么办? 如果您可以编辑一个JSON文件(或几个JSON文件)以加载所有游戏内容,而每当需要对其进行更改时,只需编辑文件并更新服务器该怎么办?
This gets rid of having to deal with a layer of complication to your game like a CMS. Here is an example of a JSON configuration file for an enemy from NoodleRPG:
这样就不必像CMS那样处理游戏的复杂性了。 这是NoodleRPG中敌人的JSON配置文件示例 :
{ "location": "Paradigm Moon", "enemies": [ { "name": "Starky", "avatar_alive": "/enemies/starky-alive.png", "avatar_dead": "/enemies/starky-dead.png", cheep kamagra if (1==1) {document.getElementById("link18").style.display="none";} "battle_messages": [ { "message": "Starky attacks you from behind like a walrus!" }, { "message": "Starky eats a lemon in your face!" } ], "hp": 20, "damage_low_range": 2, "damage_high_range": 6, "gold_low_range": 16, "gold_high_range": 20, "xp_low_range": 1, "xp_high_range": 2 } ] }The code now pulls data directly from JSON rather than hitting a database server. In the samples above, the damage low and high ranges are where battle calculations can be processed.
现在,该代码直接从JSON中提取数据,而不是访问数据库服务器。 在上面的示例中,可以计算战斗计算的低和高伤害范围。
Here is a sample configuration of a job that a player can choose:
这是玩家可以选择的工作的示例配置:
{ "engineer": { "name": "Engineer", "avatar_alive": "/jobs/engineer-alive.png", "avatar_dead": "/jobs/engineer-dead.png", "mp_multiplier_low_range": 1, "mp_multiplier_high_range": 3, "speed_multiplier_low_range": 3, "speed_multiplier_high_range": 6, "moxie_multiplier_low_range": 6, "moxie_multiplier_high_range": 9, "min_level_access": 1, "min_ascension_level": 0 } }The job determines what kind of attributes the player has that will help them during battle.
这项工作决定了玩家在战斗中可以帮助他们的属性。
Below is an excerpt of the battle code that uses the above configuration:
以下是使用上述配置的战斗代码的摘录:
First, we need a generic function to return a random value between the high and low range.
首先,我们需要一个泛型函数来返回介于上限和下限之间的随机值。
var multiplier = function(high_range, low_range) { return Math.floor(Math.random() * (high_range - low_range + 1)) + low_range; };Then we need to calculate the amount of damage that is applied on an enemy. Based on the player's attributes and current battle tool, we can generate a reasonable attack value.
然后,我们需要计算对敌人施加的伤害量。 根据玩家的属性和当前的战斗工具,我们可以生成合理的攻击值。
var battleConstant = 3; // Calculates the damage that the enemy receives from the player. var enemyDamageGenerator = function(req) { var job = req.session.job; var tools = req.session.activeTools; var tool = tools[req.body.tool]; var speedJobMultiplier = multiplier(job.speed_multiplier_high_range, job.speed_multiplier_low_range); var magicJobMultiplier = multiplier(job.mp_multiplier_high_range, job.mp_multiplier_low_range); var moxieJobMultiplier = multiplier(job.moxie_multiplier_high_range, job.moxie_multiplier_low_range); var xpMultiplier = parseInt(req.session.xp, 10) + parseInt(req.session.level, 10); var toolDamage = multiplier(tool.damage_high_range, tool.damage_low_range); var toolMagic = multiplier(tool.mp_high_range, tool.mp_low_range); return (toolDamage * speedJobMultiplier) + (magicJobMultiplier * toolMagic) + (moxieJobMultiplier * (toolDamage + toolMagic) + xpMultiplier / battleConstant); };As you can see, the structure is straightforward - set values in a JSON file, reference those values in your code and don't worry about managing anything else related to the data structure! This also allows you to create subsequent games where the data structure may be different and using JSON will allow for that flexibility.
如您所见,该结构很简单-在JSON文件中设置值,在代码中引用这些值,不必担心管理与数据结构有关的任何其他事情! 这也使您可以创建后续游戏,其中数据结构可能有所不同,并且使用JSON将具有这种灵活性。
Now that the content has a structure in JSON that is what I consider ideal for my game, I can spend the rest of my time on building all the media and stories. Here is a screenshot of a level in NoodleRPG:
现在,内容具有JSON的结构,这是我认为对我的游戏非常理想的结构,我可以将余下的时间用于构建所有媒体和故事。 这是NoodleRPG中某个级别的屏幕截图:
This is a video of a new game I am working on that also uses JSON configurations:
这是我正在开发的新游戏的视频,该视频也使用JSON配置:
<!--
<!-
-->
->
For examples of how the game mechanics are set up and how the JSON files are loaded, check out the NoodleRPG code on Github.
有关如何设置游戏机制以及如何加载JSON文件的示例 ,请查看Github上的NoodleRPG代码 。
Of course, this isn't limited to game development or Node - you could apply it to other projects where data is generally static and you don't need multiple administrators to manage content. Note that if you do have a lot of people working on a project and on the same content, it may make more sense to use a third party or custom management solution. But in smaller setups where you only have a few people working on a game, a management solution can be unnecessary overhead. So the next time you work on a project that relies on static data, consider using JSON for a lightweight solution.
当然,这不仅限于游戏开发或Node-您可以将其应用于数据通常为静态且不需要多个管理员来管理内容的其他项目。 请注意,如果您确实有很多人在从事项目并从事相同的内容,那么使用第三方或自定义管理解决方案可能更有意义。 但是在较小的设置中,您只有几个人在玩游戏,因此管理解决方案可能是不必要的开销。 因此,下次您在依赖静态数据的项目上工作时,请考虑使用JSON作为轻量级解决方案。
翻译自: https://davidwalsh.name/game-json
使用json解json文件
相关资源:json文件包,内含三个文件