Skip to content

Commit 802e7b8

Browse files
committed
UDMFArray переписан под ООП
1 parent 54ce821 commit 802e7b8

File tree

5 files changed

+166
-1
lines changed

5 files changed

+166
-1
lines changed

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const OUT_FILE = 'output/TEXTMAP';
1111
const fs = require('fs');
1212
const path = require('path');
1313

14+
const { UDMFBlock, Line, Sector } = require('./lib/oop');
15+
1416
let chalk = null;
1517
let inquirer = null;
1618
try {
@@ -46,7 +48,16 @@ console.info(`
4648
4749
`);
4850

49-
const udmfarray = udmf2json(IN_FILE, OUT_FILE);
51+
const udmfarray = udmf2json(IN_FILE, OUT_FILE).map(e => {
52+
switch (e[0]) {
53+
case 'sector':
54+
return new Sector(e);
55+
case 'line':
56+
return new Line(e);
57+
default:
58+
return new UDMFBlock(e);
59+
}
60+
});
5061
const udmfobject = jsonDecompress(udmfarray);
5162

5263
console.log(chalk.cyan(Lang.log.connecting_scripts));

lib/oop/Line.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) 2018 PROPHESSOR
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
8+
'use strict';
9+
10+
const UDMFBlock = require('./UDMFBlock');
11+
12+
module.exports = class Line extends UDMFBlock { // TODO:Direction
13+
constructor(udmfblock, udmfarray) {
14+
super(udmfblock);
15+
16+
this.udmfblock = udmfblock;
17+
this.udmfarray = udmfarray;
18+
}
19+
20+
get v1() {
21+
if (typeof this._v1 === 'undefined')
22+
this._v1 = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'vertex')[this.udmfblock.v1]);
23+
return this._v1;
24+
}
25+
26+
get v2() {
27+
if (typeof this._v2 === 'undefined')
28+
this._v2 = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'vertex')[this.udmfblock.v2]);
29+
return this._v2;
30+
}
31+
32+
get front() {
33+
if (typeof this._front === 'undefined')
34+
this._front = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'sidedef')[this.udmfblock.sidefront]);
35+
return this._front;
36+
}
37+
38+
get back() {
39+
if (typeof this.udmfblock.sideback === 'undefined' || this.udmfblock.sideback === -1) return null;
40+
if (typeof this._back === 'undefined')
41+
this._back = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'sidedef')[this.udmfblock.sideback]);
42+
return this._back;
43+
}
44+
45+
/** Возвращает координаты середины линии
46+
* @returns {array} [x, y]
47+
*/
48+
get avg() {
49+
return [(this.v1.x + this.v2.x) / 2, (this.v1.y + this.v2.y) / 2];
50+
}
51+
};

lib/oop/Sector.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) 2018 PROPHESSOR
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
8+
'use strict';
9+
10+
const UDMFBlock = require('./UDMFBlock');
11+
const Line = require('./Line');
12+
13+
module.exports = class Sector extends UDMFBlock {
14+
constructor(udmfblock, udmfarray) {
15+
super(udmfblock);
16+
17+
this.udmfblock = udmfblock;
18+
this.udmfarray = udmfarray;
19+
}
20+
21+
get id() {
22+
return this.udmfblock.id;
23+
}
24+
25+
get lines() {
26+
if (typeof this._lines === 'undefined')
27+
this._lines = this.udmfarray.filter(e => e[0] === 'linedef' && e[1].sector === this.id).map(e => new Line(new UDMFBlock(e), this.udmfarray));
28+
return this._lines;
29+
}
30+
};

lib/oop/UDMFBlock.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) 2018 PROPHESSOR
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
8+
'use strict';
9+
10+
module.exports = class UDMFBlock {
11+
constructor(udmfblock) {
12+
if (!(udmfblock instanceof Array || udmfblock instanceof UDMFBlock)) throw new TypeError('UDMFBlock must be a Array');
13+
14+
[this[0], this[1]] = udmfblock;
15+
this.blocktype = udmfblock[0];
16+
Object.assign(this, udmfblock);
17+
}
18+
19+
toUDMFArrayBlock() {
20+
return [this[0], this[1]];
21+
}
22+
23+
toString() {
24+
return `[UDMF Block <${this[0]}>]`;
25+
}
26+
27+
*[Symbol.iterator]() {
28+
yield this[0];
29+
yield this[1];
30+
}
31+
32+
33+
[Symbol.toStringTag]() {
34+
return this.toString();
35+
}
36+
37+
[Symbol.toPrimitive]() {
38+
return this.toString();
39+
}
40+
41+
/** Ищет блок в udmf.json массиве и возвращает его
42+
* @param {array} json - udmf.json массив
43+
* @param {string} blockname - Тип блока
44+
* @param {number} no - Номер блока
45+
* @returns {array} udmf.json блок
46+
*/
47+
static find(json, blockname, no) {
48+
let i = 0;
49+
50+
for(const block of json) {
51+
if(block[0] === blockname) {
52+
if(no === i) return block[1];
53+
i++;
54+
}
55+
}
56+
57+
return null;
58+
}
59+
};

lib/oop/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) 2018 PROPHESSOR
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
8+
'use strict';
9+
10+
module.exports = {
11+
UDMFBlock: require('./UDMFBlock'),
12+
Line: require('./Line'),
13+
Sector: require('./Sector')
14+
};

0 commit comments

Comments
 (0)