Created
February 9, 2016 11:18
-
-
Save sasamijp/dbe974a5c1e7a7a1c74e to your computer and use it in GitHub Desktop.
xml読んでマシン複製するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var besiege = require('besiege.js'); | |
const Block = require('./block.js'); | |
const parseString = require('xml2js').parseString; | |
var lines=[]; | |
var reader = require('readline').createInterface({ | |
input: process.stdin | |
}); | |
var machine = null; | |
var block_data = null; | |
function loadfromxml(xml_string) { | |
var mc_obj; | |
var data_id_counter = 0; | |
block_data = xml_string.match(/<Data>[\s\S]*?<\/Data>/g); | |
parseString(xml_string, function (err, result) { | |
mc_obj = result | |
}); | |
machine = new besiege.Machine({ | |
name: "c" + mc_obj["Machine"]["$"]["name"], | |
position: mc_obj["Machine"]["Global"][0]["Position"][0]["$"], | |
rotation: mc_obj["Machine"]["Global"][0]["Rotation"][0]["$"] | |
}); | |
mc_obj["Machine"]["Blocks"][0]["Block"].forEach((e) => { | |
if(e["Data"][0] == ""){ | |
machine.add(new Block({ | |
id: e["$"]["id"], | |
position: e["Transform"][0]["Position"][0]["$"], | |
rotation: e["Transform"][0]["Rotation"][0]["$"], | |
scale: e["Transform"][0]["Scale"][0]["$"], | |
})); | |
}else{ | |
machine.add(new Block({ | |
id: e["$"]["id"], | |
position: e["Transform"][0]["Position"][0]["$"], | |
rotation: e["Transform"][0]["Rotation"][0]["$"], | |
scale: e["Transform"][0]["Scale"][0]["$"], | |
vector3: { // this vector is flag of block data exists | |
x: data_id_counter, | |
y: 50000, | |
z: 50000 | |
} | |
})); | |
data_id_counter++; | |
} | |
}); | |
} | |
function duplicate(vector3){ | |
if(machine == null){ return; } | |
machine.blocks.forEach((e) => { | |
machine.add(new Block({ | |
id: e.id, | |
position: { | |
x: parseFloat(e.position['x']) + vector3['x'], | |
y: parseFloat(e.position['y']) + vector3['y'], | |
z: parseFloat(e.position['z']) + vector3['z'] | |
}, | |
rotation: e.rotation, | |
scale: e.scale, | |
vector3: e.vector3 | |
})); | |
}) | |
var xml = machine.toString(); | |
if(block_data != null){ | |
block_data.forEach((e, i) => { | |
var regexp = new RegExp("<Data>\n.*\n.*\n.*\n.*\n.*\n.*\n.*<X>"+i+"</X>.*\n.*\n.*\n.*\n.*<\/Data>", 'g'); | |
// this regexp matches flag of block data exists | |
xml = xml.replace(regexp, e); | |
}); | |
} | |
console.log(xml); | |
} | |
var lines = []; | |
reader.on('line', function (line) { | |
lines.push(line); | |
}); | |
reader.on('close', function () { | |
loadfromxml(lines.join('')); | |
duplicate({ | |
x:0, | |
y:15, | |
z:0 | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment