roguelike-pumpkin-patch
Version:
A roguelike development library in JavaScript.
228 lines • 9.27 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
var baseClassName = "pumpkin-tile";
/** Class to keep track of each individual tile in the display */
var Tile = /** @class */ (function () {
function Tile(tileOptions, position, tileSize) {
// Create necessary elements and apply classes
this.element = document.createElement('div');
this.element.classList.add(baseClassName);
// Set tile content and colour scheme
var _a = tileOptions.content, content = _a === void 0 ? '' : _a, _b = tileOptions.color, color = _b === void 0 ? '' : _b, _c = tileOptions.background, background = _c === void 0 ? '' : _c, _d = tileOptions.className, className = _d === void 0 ? '' : _d, _e = tileOptions.classList, classList = _e === void 0 ? [] : _e, rest = __rest(tileOptions, ["content", "color", "background", "className", "classList"]);
this.content = content;
this.color = color;
this.background = background;
if (classList.length > 0) {
this.classList = classList;
}
else {
this.className = className;
}
// Set the tile size
this.tileWidth = (tileSize === null || tileSize === void 0 ? void 0 : tileSize.tileWidth) ? tileSize.tileWidth : 16;
this.tileHeight = (tileSize === null || tileSize === void 0 ? void 0 : tileSize.tileHeight) ? tileSize.tileHeight : this.tileWidth;
// Set the tile position
this.position = position;
}
;
Object.defineProperty(Tile.prototype, "content", {
/** Get or set the tile contents */
get: function () {
return this._content;
},
set: function (newContent) {
// Create contentElement if it doesn't already exist
this.confirmContentElement();
// Only update if the new and old content don't match
if (this._content !== newContent) {
// If content is a string, just add it
if (typeof newContent === 'string') {
this.contentElement.innerHTML = newContent;
}
// If it is an element, empty the tile and append the new content
else {
while (this.contentElement.lastElementChild) {
this.contentElement.removeChild(this.contentElement.lastElementChild);
}
this.contentElement.appendChild(newContent);
}
this._content = newContent;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(Tile.prototype, "background", {
/** Get or set the background colour */
get: function () {
return this._background;
},
set: function (newBackground) {
if (newBackground !== this._background) {
this._background = newBackground;
this.element.style.backgroundColor = newBackground;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(Tile.prototype, "color", {
/** Get or set the color colour */
get: function () {
return this._color;
},
set: function (newcolor) {
if (newcolor !== this._color) {
this._color = newcolor;
this.element.style.color = newcolor;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(Tile.prototype, "position", {
/** Get or set position */
get: function () {
return this._position;
},
set: function (position) {
this._position = __assign({}, position);
this.element.style.left = position.x * this.tileWidth + "px";
this.element.style.top = position.y * this.tileHeight + "px";
},
enumerable: false,
configurable: true
});
Object.defineProperty(Tile.prototype, "tileWidth", {
/** Get or set tile width */
get: function () {
return this._tileWidth;
},
set: function (newWidth) {
this._tileWidth = newWidth;
this.element.style.width = newWidth + "px";
},
enumerable: false,
configurable: true
});
Object.defineProperty(Tile.prototype, "tileHeight", {
/** Get or set the tile height */
get: function () {
return this._tileHeight;
},
set: function (newHeight) {
this._tileHeight = newHeight;
this.element.style.height = newHeight + "px";
},
enumerable: false,
configurable: true
});
Object.defineProperty(Tile.prototype, "className", {
/** Get or set the classname */
get: function () {
return this.classList.join(" ");
},
set: function (newClass) {
if (newClass) {
this.classList = newClass.split(" ");
}
else {
this.classList = [];
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(Tile.prototype, "classList", {
/** Get or set the list of classes */
get: function () {
return __spreadArrays([baseClassName], this._classList);
},
set: function (newClassList) {
var _this = this;
if (!this._classList) {
this._classList = [];
}
// Only add/remove classes if the two lists are actually different
if (newClassList.length !== this._classList.length ||
!newClassList.every(function (className, i) { return className === _this._classList[i]; })) {
this._classList = newClassList;
// Set using the getter, to ensure baseClassName is still on the list.
this.element.className = this.classList.join(' ');
}
},
enumerable: false,
configurable: true
});
/** Set options for the tile */
Tile.prototype.setOptions = function (newOptions) {
var _a = newOptions.content, content = _a === void 0 ? "" : _a, _b = newOptions.background, background = _b === void 0 ? "" : _b, _c = newOptions.color, color = _c === void 0 ? "" : _c, _d = newOptions.className, className = _d === void 0 ? "" : _d, classList = newOptions.classList;
this.content = content;
this.background = background;
this.color = color;
if (classList) {
this.classList = classList;
}
else {
this.className = className;
}
};
/**
* Update options for the tile
*/
Tile.prototype.updateOptions = function (newOptions) {
var content = newOptions.content, background = newOptions.background, color = newOptions.color, className = newOptions.className, classList = newOptions.classList;
if (typeof content !== "undefined") {
this.content = content;
}
if (typeof background !== "undefined") {
this.background = background;
}
if (typeof color !== "undefined") {
this.color = color;
}
if (classList && classList.length > 0) {
this.classList = classList;
}
else if (typeof className !== "undefined") {
this.className = className;
}
};
/** Check if a contentElement exists, and if it doesn't, add it */
Tile.prototype.confirmContentElement = function () {
if (!this.contentElement) {
this.contentElement = document.createElement('div');
this.element.appendChild(this.contentElement);
}
};
return Tile;
}());
export default Tile;
//# sourceMappingURL=Tile.js.map