关于二次开发 BrowserQuest 中,对于地图修改的部分记录
关于二次开发 BrowserQuest 中,对于地图修改的部分记录
在这里要感谢的是网友:神灯,land007
步骤:
1 通过地图编辑器tiled 修改 BrowserQuest-master\tools\maps\tmx 文件夹下的map.tmx
2 通过 地图编辑器 导出 map.json
3 通过命令nodejs脚本把map.json生成系统能够适别的json数据。
命令工具脚本:map.bat,exportmap.js 和 file.js (注:网友”神灯“提供)
1) map.bat:
@color 3F
@echo.
@echo 操作完成自动退出程序!
@echo.
node exportmap.js map.json direct
@echo.
@echo 操作完成按任意键退出程序!
@echo.
@pause >nul
exit
2) exportmap.js
#!/usr/bin/env node
var util = require('util'),
Log = require('log'),
path = require("path"),
fs = require("fs"),
file = require("../../shared/js/file"),
processMap = require('./processmap'),
log = new Log(Log.DEBUG);
// 此处用了nodejs的process进程处理,读取参数,即 map.bat 中 node exportmap.js map.json direct
// node 为参数0 process.argv[0] exportmap.js 为参数1 map.json 为参数3 依次类推...
var source = process.argv[2],
mode = process.argv[3],
destination = process.argv[4];
// source 代表的是map.json
if(!mode){
mode = "direct";
}
if(!source || (mode!="direct" && mode!="root" && mode!="both" && mode!="client" && mode!="server") || (mode!="root" && mode!="direct" && !destination)) {
util.puts("Usage : ./exportmap.js tiled_json_file [mode] [destination]");
util.puts("Optional parameters : mode & destination. Values:");
util.puts(" - "direct" (default) → updates current server and map files (WARNING: SHOULD ONLY BE CALLED FROM BrowserQuest/tools/maps !!!);");
util.puts(" - "client destination_file" → will generate destination_file.js and destination_file.json for client side map;");
util.puts(" - "server destination_file.json" → will generate destination_file.json for server side map;");
util.puts(" - "both destination_directory" → will generate world_client.js, world_client.json and world_server.json in directory.");
process.exit(0);
}
function main() {
getTiledJSONmap(source, callback_function);
}
function callback_function(json) {
switch(mode){
case "client":
processClient(json, destination);
break;
case "server":
processServer(json, destination);
break;
case "direct":
processClient(json, "../../client/maps/world_client");
processServer(json, "../../server/maps/world_server.json");
break;
case "both":
var directory=destination.replace(/\/+$/,'');//strip last path slashes
processClient(json, directory+"/world_client");
processServer(json, directory+"/world_server.json");
break;
case "root":
processClient(json, "client/maps/world_client");
processServer(json, "server/maps/world_server.json");
break;
default:
util.puts("Unrecognized mode, how on earth did you manage that ?");
}
}
function processClient(json, dest){
var jsonMap = JSON.stringify(processMap(json, {mode:"client"})); // Save the processed map object as JSON data
// map in a .json file for ajax loading
fs.writeFile(dest+".json", jsonMap, function(err, file) {
if(err){
log.error(JSON.stringify(err));
}
else{
log.info("Finished processing map file: "+ dest + ".json was saved.");
}
});
// map in a .js file for web worker loading
jsonMap = "var mapData = "+jsonMap;
fs.writeFile(dest+".js", jsonMap, function(err, file) {
if(err){
log.error(JSON.stringify(err));
}
else{
log.info("Finished processing map file: "+ dest + ".js was saved.");
}
});
}
function processServer(json, dest){
var jsonMap = JSON.stringify(processMap(json, {mode:"server"})); // Save the processed map object as JSON data
fs.writeFile(dest, jsonMap, function(err, file) {
if(err){
log.error(JSON.stringify(err));
}
else{
log.info("Finished processing map file: "+ dest + " was saved.");
}
});
}
function getTiledJSONmap(filename, callback) {
var self = this;
// 此处调用的file.js中的exists函数
file.exists(filename, function(exists) {
if(!exists) {
log.error(filename + " doesn't exist.")
return;
}
fs.readFile(filename, function(err, file) {
callback(JSON.parse(file.toString()));
});
});
}
main();
3) file.js
var exists, existsSync;
(function () {
var semver = require('semver');
var module = (semver.satisfies(process.version, '>=0.7.1') ? require('fs') : require('path'));
exists = module.exists;
existsSync = module.existsSync;
})();
if (!(typeof exports === 'undefined')) {
module.exports.exists = exists;
module.exports.existsSync = existsSync;
}
// 其中semver为node的一个模块,可以通过 npm install semver 安装模块 (前提是先弄清楚,安装node)
4 修改processmap.js
在官方提供的源码直接运行脚本,并不能完成功能,因为之前看过官方wiki,知道地图这一块有一个
https://github.com/browserquest/BrowserQuest/blob/master/tools/maps/processmap.js
把这个processmap.js 替换源码 BrowserQuest-master\tools\maps 下的processmap.js即可。
最后运行map.bat批处理文件,即可在对应目录生成json数据。。
然后你重启node server/js/main.js 重启服务器tomcat或apache,访问,可以看到,此时的地图为你修改后的地图。地图编辑算是成功了。