Skip to content

Commit

Permalink
UDMFArray переписан под ООП
Browse files Browse the repository at this point in the history
  • Loading branch information
PROPHESSOR committed Jul 8, 2018
1 parent 54ce821 commit 802e7b8
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 1 deletion.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const OUT_FILE = 'output/TEXTMAP';
const fs = require('fs');
const path = require('path');

const { UDMFBlock, Line, Sector } = require('./lib/oop');

let chalk = null;
let inquirer = null;
try {
Expand Down Expand Up @@ -46,7 +48,16 @@ console.info(`
`);

const udmfarray = udmf2json(IN_FILE, OUT_FILE);
const udmfarray = udmf2json(IN_FILE, OUT_FILE).map(e => {
switch (e[0]) {
case 'sector':
return new Sector(e);
case 'line':
return new Line(e);
default:
return new UDMFBlock(e);
}
});
const udmfobject = jsonDecompress(udmfarray);

console.log(chalk.cyan(Lang.log.connecting_scripts));
Expand Down
51 changes: 51 additions & 0 deletions lib/oop/Line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2018 PROPHESSOR
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

'use strict';

const UDMFBlock = require('./UDMFBlock');

module.exports = class Line extends UDMFBlock { // TODO:Direction
constructor(udmfblock, udmfarray) {
super(udmfblock);

this.udmfblock = udmfblock;
this.udmfarray = udmfarray;
}

get v1() {
if (typeof this._v1 === 'undefined')
this._v1 = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'vertex')[this.udmfblock.v1]);
return this._v1;
}

get v2() {
if (typeof this._v2 === 'undefined')
this._v2 = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'vertex')[this.udmfblock.v2]);
return this._v2;
}

get front() {
if (typeof this._front === 'undefined')
this._front = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'sidedef')[this.udmfblock.sidefront]);
return this._front;
}

get back() {
if (typeof this.udmfblock.sideback === 'undefined' || this.udmfblock.sideback === -1) return null;
if (typeof this._back === 'undefined')
this._back = new UDMFBlock(this.udmfarray.filter(e => e[0] === 'sidedef')[this.udmfblock.sideback]);
return this._back;
}

/** Возвращает координаты середины линии
* @returns {array} [x, y]
*/
get avg() {
return [(this.v1.x + this.v2.x) / 2, (this.v1.y + this.v2.y) / 2];
}
};
30 changes: 30 additions & 0 deletions lib/oop/Sector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2018 PROPHESSOR
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

'use strict';

const UDMFBlock = require('./UDMFBlock');
const Line = require('./Line');

module.exports = class Sector extends UDMFBlock {
constructor(udmfblock, udmfarray) {
super(udmfblock);

this.udmfblock = udmfblock;
this.udmfarray = udmfarray;
}

get id() {
return this.udmfblock.id;
}

get lines() {
if (typeof this._lines === 'undefined')
this._lines = this.udmfarray.filter(e => e[0] === 'linedef' && e[1].sector === this.id).map(e => new Line(new UDMFBlock(e), this.udmfarray));
return this._lines;
}
};
59 changes: 59 additions & 0 deletions lib/oop/UDMFBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2018 PROPHESSOR
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

'use strict';

module.exports = class UDMFBlock {
constructor(udmfblock) {
if (!(udmfblock instanceof Array || udmfblock instanceof UDMFBlock)) throw new TypeError('UDMFBlock must be a Array');

[this[0], this[1]] = udmfblock;
this.blocktype = udmfblock[0];
Object.assign(this, udmfblock);
}

toUDMFArrayBlock() {
return [this[0], this[1]];
}

toString() {
return `[UDMF Block <${this[0]}>]`;
}

*[Symbol.iterator]() {
yield this[0];
yield this[1];
}


[Symbol.toStringTag]() {
return this.toString();
}

[Symbol.toPrimitive]() {
return this.toString();
}

/** Ищет блок в udmf.json массиве и возвращает его
* @param {array} json - udmf.json массив
* @param {string} blockname - Тип блока
* @param {number} no - Номер блока
* @returns {array} udmf.json блок
*/
static find(json, blockname, no) {
let i = 0;

for(const block of json) {
if(block[0] === blockname) {
if(no === i) return block[1];
i++;
}
}

return null;
}
};
14 changes: 14 additions & 0 deletions lib/oop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) 2018 PROPHESSOR
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

'use strict';

module.exports = {
UDMFBlock: require('./UDMFBlock'),
Line: require('./Line'),
Sector: require('./Sector')
};

0 comments on commit 802e7b8

Please sign in to comment.