UNPKG

sam-ecs

Version:

A specialized entity component system

77 lines (62 loc) 2.84 kB
'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } //Family.js// /** * @description - More robust than a set for choosing entities to * be included in a processor * @author - Sam Faulkner */ var FastSet = require('collections/fast-set.js'); var Family = function () { function Family(mustHave, cannotHave) { _classCallCheck(this, Family); if ((typeof mustHave === 'undefined' ? 'undefined' : _typeof(mustHave)) == 'object') { mustHave = new FastSet(mustHave); } if ((typeof cannotHave === 'undefined' ? 'undefined' : _typeof(cannotHave)) == 'object') { cannotHave = new FastSet(cannotHave); } this._mustHaves = mustHave || new FastSet(); this._cannotHave = cannotHave || new FastSet(); } _createClass(Family, [{ key: 'getHaves', value: function getHaves() { return this._mustHaves; } }, { key: 'getCannotHaves', value: function getCannotHaves() { return this._cannotHave; } }, { key: 'has', value: function has(singleComponent) { return this._mustHaves.has(singleComponent) && !this._cannotHave.has(singleComponent); } /** * @description - Returns a boolean if this entity belongs in this * family * @param {Entity} entity - the entity to test */ }, { key: 'testEntity', value: function testEntity(entity) { return this.testComponentSet(entity.getComponents().keysArray()); } /** * @description - Returns true if the set of components could belong * in this set or not * @param {Set} set - the set of components to test */ }, { key: 'testComponentSet', value: function testComponentSet(set) { return this._mustHaves.intersection(set).length == this._mustHaves.length && this._cannotHave.intersection(set).length == 0; } }]); return Family; }(); module.exports = Family;