UNPKG

4.35 kBJavaScriptView Raw
1"use strict";
2
3const utilities = require("extra-utilities");
4const Tile = require("./tile.js");
5
6class TileAnimationTypeProperties {
7 constructor() {
8 let self = this;
9
10 let _properties = {
11 idCounter: 0
12 };
13
14 Object.defineProperty(self, "idCounter", {
15 enumerable: true,
16 get() {
17 return _properties.idCounter;
18 },
19 set(value) {
20 const newIDCounter = utilities.parseInteger(value);
21
22 if(!isNaN(newIDCounter) && isFinite(newIDCounter) && newIDCounter > _properties.idCounter) {
23 _properties.idCounter = newIDCounter;
24 }
25 }
26 });
27 }
28}
29
30class TileAnimationType {
31 constructor(id, name) {
32 let self = this;
33
34 if(typeof id === "string") {
35 name = id;
36 id = NaN;
37 }
38
39 let _properties = {
40 id: NaN,
41 name: null
42 };
43
44 Object.defineProperty(self, "id", {
45 enumerable: true,
46 get() {
47 return _properties.id;
48 },
49 set(value) {
50 let newID = utilities.parseInteger(value);
51
52 // only allow negative special values to be set manually
53 if(!isNaN(newID) && newID < 0) {
54 _properties.id = newID;
55 }
56 }
57 });
58
59 Object.defineProperty(self, "name", {
60 enumerable: true,
61 get() {
62 return _properties.name;
63 },
64 set(value) {
65 _properties.name = utilities.trimString(value);
66 }
67 });
68
69 self.id = id;
70 self.name = name;
71
72 if(isNaN(_properties.id)) {
73 _properties.id = TileAnimationType.idCounter++;
74 }
75 }
76
77 static getAnimationType(value) {
78 if(TileAnimationType.isTileAnimationType(value)) {
79 for(let i = 0; i < TileAnimationType.Types.length; i++) {
80 if(TileAnimationType.Types[i] === value) {
81 return TileAnimationType.Types[i];
82 }
83
84 return TileAnimationType.Invalid;
85 }
86 }
87 else if(typeof value === "string") {
88 const formattedValue = utilities.trimString(value);
89
90 for(let i = 0; i < TileAnimationType.Types.length; i++) {
91 const animationType = TileAnimationType.Types[i];
92 const id = utilities.parseInteger(formattedValue);
93
94 if(animationType.id === id ||
95 utilities.stringEqualsIgnoreCase(animationType.name, formattedValue)) {
96 return animationType;
97 }
98 }
99
100 return TileAnimationType.Invalid;
101 }
102 else if(Number.isInteger(value)) {
103 for(let i = 0; i < TileAnimationType.Types.length; i++) {
104 if(TileAnimationType.Types[i].id === value) {
105 return TileAnimationType.Types[i];
106 }
107 }
108
109 return TileAnimationType.Invalid;
110 }
111
112 return TileAnimationType.Invalid;
113 }
114
115 equals(value) {
116 let self = this;
117
118 if(!self.isValid() || !TileAnimationType.isValid(value)) {
119 return false;
120 }
121
122 return utilities.stringEqualsIgnoreCase(self.name, value.name);
123 }
124
125 toString() {
126 let self = this;
127
128 return self.name;
129 }
130
131 static isTileAnimationType(value) {
132 return value instanceof TileAnimationType;
133 }
134
135 isValid() {
136 let self = this;
137
138 return self.id >= 0 &&
139 utilities.isNonEmptyString(self.name);
140 }
141
142 static isValid(value) {
143 return TileAnimationType.isTileAnimationType(value) &&
144 value.isValid();
145 }
146}
147
148Object.defineProperty(Tile, "AnimationType", {
149 value: TileAnimationType,
150 enumerable: true
151});
152
153Object.defineProperty(TileAnimationType, "properties", {
154 value: new TileAnimationTypeProperties(),
155 enumerable: false
156});
157
158Object.defineProperty(TileAnimationType, "idCounter", {
159 enumerable: true,
160 get() {
161 return TileAnimationType.properties.idCounter;
162 },
163 set(value) {
164 TileAnimationType.properties.idCounter = value;
165 }
166});
167
168Object.defineProperty(TileAnimationType, "Invalid", {
169 value: new TileAnimationType(-1, "Invalid"),
170 enumerable: true
171});
172
173Object.defineProperty(TileAnimationType, "None", {
174 value: new TileAnimationType("None"),
175 enumerable: true
176});
177
178Object.defineProperty(TileAnimationType, "Oscillating", {
179 value: new TileAnimationType("Oscillating"),
180 enumerable: true
181});
182
183Object.defineProperty(TileAnimationType, "Forward", {
184 value: new TileAnimationType("Forward"),
185 enumerable: true
186});
187
188Object.defineProperty(TileAnimationType, "Backward", {
189 value: new TileAnimationType("Backward"),
190 enumerable: true
191});
192
193Object.defineProperty(TileAnimationType, "Default", {
194 value: TileAnimationType.None,
195 enumerable: true
196});
197
198Object.defineProperty(TileAnimationType, "Types", {
199 value: [
200 TileAnimationType.None,
201 TileAnimationType.Oscillating,
202 TileAnimationType.Forward,
203 TileAnimationType.Backward
204 ],
205 enumerable: true
206});
207
208module.exports = TileAnimationType;