-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.js
96 lines (82 loc) · 2.59 KB
/
element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @file element.js
* @copyright Copyright (c) 2022 MapMaths
* @license MIT
* @author MapMaths
* @repo https://GitHub.com/MapMaths/plax
* @description Provide element editing functions.
*/
const base = require("./tool/base.js");
class Element {
constructor(Editor, id=null, type=null, n=null) {
let json = Editor.json ? Editor.json : Editor;
let elements = JSON.parse(json.StatusSave).Elements;
let found = false;
if (id != null) {
if (type != null || n != null) throw SyntaxError("Unexpected arguments 'type' and 'n'.");
for (let i = 0; i < elements.length; i++) {
if (elements[i].Identifier == id) {
this.json = elements[i];
this.index = i;
found = true;
break;
}
}
found || base.elementError();
} else if (type != null) {
let num = -1;
for (let i = 0; i < elements.length; i++) {
if (elements[i].ModelID == type) {
if (n != null) {
num++;
if(num < n) continue;
}
this.json = elements[i];
this.index = i;
found = true;
break;
}
}
found || base.elementError();
} else if (n != null) {
this.json = elements[n] || base.elementError();
this.index = n;
} else {
throw SyntaxError("Too few arguments.");
}
}
setPos(pos) {
this.json.Position = `${pos[0] != null ? pos[0] : this.pos[0]},${pos[2] != null ? pos[2] : this.pos[1]},${pos[1] != null ? pos[1] : this.pos[2]}`;
}
setRot(rot) {
this.json.Rotation = `${rot[0]},${rot[2]},${rot[1]}`;
}
move(steps) {
this.json.Position = `${this.pos[0] + steps[0]},${this.pos[2] + steps[2]},${this.pos[1] + steps[1]}`
}
lock() {
this.json.Properties.锁定 = 1;
}
unlock() {
delete this.json.Properties.锁定;
}
burn() {
this.json.IsBroken = true;
}
fix() {
this.json.IsBroken = false;
}
newID(Editor) {
let json = Editor.json ? Editor.json : Editor;
this.json.Identifier = base.generateNewElementID(json);
}
get id() {
return this.json.Identifier;
}
get pos() {
let p = this.json.Position.split(",").map(Number);
return [p[0], p[2], p[1]];
}
}
// Export the functions
exports.Element = Element;