UNPKG

4.19 kBJavaScriptView Raw
1"use strict";
2
3const utilities = require("extra-utilities");
4const Tile = require("./tile.js");
5const TileAttribute = require("./tile-attribute.js");
6
7class TileAttributes {
8 constructor(xOffset, yOffset, numberOfFrames, animationType, animationSpeed, extra) {
9 let self = this;
10
11 let _properties = { };
12
13 for(let i = 0; i < TileAttributes.Attribute.Attributes.length; i++) {
14 const attribute = TileAttributes.Attribute.Attributes[i];
15
16 Object.defineProperty(self, attribute.attributeName, {
17 enumerable: true,
18 get() {
19 return _properties[attribute.attributeName];
20 },
21 set(value) {
22 const newValue = utilities.parseInteger(value);
23
24 if(isNaN(newValue)) {
25 throw new Error("Invalid " + attribute.name + " value: " + value + " expected valid integer value.");
26 }
27
28 if(newValue < attribute.min || newValue > attribute.max) {
29 throw new Error(attribute.name + " value: " + newValue + " out of range, expected integer value between " + attribute.min + " and " + attribute.max + ", inclusively.");
30 }
31
32 _properties[attribute.attributeName] = newValue;
33 }
34 });
35 }
36
37 self.xOffset = xOffset;
38 self.yOffset = yOffset;
39 self.numberOfFrames = numberOfFrames;
40 self.animationType = animationType;
41 self.animationSpeed = animationSpeed;
42 self.extra = extra; // note: extra value is not used
43 }
44
45 getAnimationType() {
46 let self = this;
47
48 const animationType = Tile.AnimationType.Types[self.animationType];
49
50 return Tile.AnimationType.isValid(animationType) ? animationType : Tile.AnimationType.Invalid;
51 }
52
53 getMetadata() {
54 let self = this;
55
56 return {
57 offset: {
58 x: self.xOffset,
59 y: self.yOffset
60 },
61 numberOfFrames: self.numberOfFrames,
62 animation: {
63 type: self.getAnimationType().name,
64 speed: self.animationSpeed
65 },
66 extra: self.extra
67 };
68 }
69
70 pack() {
71 let self = this;
72
73 let packedValue = 0;
74
75 for(let i = 0; i < TileAttributes.Attribute.Attributes.length; i++) {
76 const attribute = TileAttributes.Attribute.Attributes[i];
77
78 packedValue += utilities.leftShift(self[attribute.attributeName] & attribute.mask, attribute.offset);
79 }
80
81 return packedValue;
82 }
83
84 static unpack(packedValue) {
85 packedValue = utilities.parseInteger(packedValue);
86
87 if(!Number.isInteger(packedValue)) {
88 return null;
89 }
90
91 let tileAttributes = [];
92
93 for(let i = 0; i < TileAttributes.Attribute.Attributes.length; i++) {
94 const attribute = TileAttributes.Attribute.Attributes[i];
95
96 tileAttributes[i] = utilities.rightShift((packedValue & attribute.offsetMask), attribute.offset) & attribute.mask;
97
98 if(attribute.signed) {
99 tileAttributes[i] = ((tileAttributes[i] & attribute.signBitMask) === attribute.signBitMask) ? -(attribute.mask - tileAttributes[i] + 1) : tileAttributes[i];
100 }
101 }
102
103 return new (Function.prototype.bind.apply(TileAttributes, [null].concat(tileAttributes)));
104 }
105
106 equals(value) {
107 let self = this;
108
109 if(!TileAttributes.isTileAttributes(value)) {
110 return false;
111 }
112
113 return self.xOffset === value.xOffset &&
114 self.yOffset === value.yOffset &&
115 self.numberOfFrames === value.numberOfFrames &&
116 self.animationType === value.animationType &&
117 self.animationSpeed=== value.animationSpeed;
118 }
119
120 toString() {
121 let self = this;
122
123 let tileAttributeString = "Tile Attributes (";
124
125 for(let i = 0; i < TileAttributes.Attribute.Attributes.length; i++) {
126 const attribute = TileAttributes.Attribute.Attributes[i];
127
128 if(i !== 0) {
129 tileAttributeString += ", ";
130 }
131
132 tileAttributeString += attribute.name + ": ";
133
134 if(attribute === TileAttributes.AnimationType) {
135 tileAttributeString += self.getAnimationType().name;
136 }
137 else {
138 tileAttributeString += self[attribute.attributeName];
139 }
140 }
141
142 tileAttributeString += ")";
143
144 return tileAttributeString;
145 }
146
147 clone() {
148 const self = this;
149
150 return new TileAttributes(self.xOffset, self.yOffset, self.numberOfFrames, self.animationType, self.animationSpeed, self.extra);
151 }
152
153 static isTileAttributes(value) {
154 return value instanceof TileAttributes;
155 }
156}
157
158Object.defineProperty(TileAttributes, "Attribute", {
159 value: TileAttribute,
160 enumerable: true
161});
162
163module.exports = TileAttributes;