Skip to content

Commit

Permalink
Updated TiledLoader to link entities defined by object pointers in Ti…
Browse files Browse the repository at this point in the history
…led.
  • Loading branch information
probityrules committed Mar 11, 2021
1 parent 3c466a2 commit 8de92d4
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 58 deletions.
116 changes: 116 additions & 0 deletions src/EntityLinker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* This class defines a linker for TiledLoader to connect entity pointer properties as soon as both entities are created.
*
* @namespace platypus
* @class EntityLinker
* @constructor
* @return {EntityLinker} Returns the new EntityLinker object.
*/
import {arrayCache, greenSplice} from './utils/array';
import Data from './Data';
import DataMap from './DataMap';
import config from 'config';
import recycle from 'recycle';

export default (function () {
var
EntityLinker = function () {
this.ids = this.ids || arrayCache.setUp();
this.entities = this.entities || DataMap.setUp();
this.currentId = 0;
this.count = 1;
},
proto = EntityLinker.prototype;

proto.attemptRecycle = function () {
this.count -= 1;
if (this.count === 0) {
this.recycle();
}
};

proto.linkObject = function (id) {
this.currentId = id;
this.count += 1;
};

proto.linkEntity = function (entity) {
const
id = entity.tiledId,
list = this.ids;
let i = list.length;

while (i--) {
const
item = list[i];
let updated = false;

if (item.onEntity === entity.tiledId) {
item.onEntity = entity;
updated = true;
}
if (item.toEntity === entity.tiledId) {
item.toEntity = entity;
updated = true;
}
if (updated && typeof item.onEntity === 'object' && typeof item.toEntity === 'object') {
item.onEntity[item.property] = item.toEntity;
item.recycle();
greenSplice(list, i);
}
}

this.entities[id] = entity;
this.attemptRecycle();
};

proto.getEntity = function (id, property) {
const entity = this.entities[id];

if (entity) {
return entity;
} else {
this.ids.push(Data.setUp(
'onEntity', this.currentId,
'toEntity', id,
'property', property
));
return null;
}
};

/**
* Returns EntityLinker from cache or creates a new one if none are available.
*
* @method EntityLinker.setUp
* @return {platypus.EntityLinker} The instantiated EntityLinker.
*/
/**
* Returns EntityLinker back to the cache. Prefer the EntityLinker's recycle method since it recycles property objects as well.
*
* @method EntityLinker.recycle
* @param {platypus.EntityLinker} The EntityLinker to be recycled.
*/
/**
* Relinquishes EntityLinker properties and recycles it.
*
* @method recycle
*/
recycle.add(EntityLinker, 'EntityLinker', EntityLinker, function () {
const
entities = this.entities,
ids = this.ids;

for (const key in entities) {
if (entities.hasOwnProperty(key)) {
delete entities[key];
}
}
for (let i = 0; i < ids.length; i++) {
ids[i].recycle();
}
ids.length = 0;
}, true, config.dev);

return EntityLinker;
}());
2 changes: 1 addition & 1 deletion src/components/HandlerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default createComponentClass({
/**
* Whether 'handle-controller' event should fire based on the 'handle-logic' event instead of the 'tick' event.
*
* @property alwaysOn
* @property useHandleLogic
* @type Boolean
* @default false
*/
Expand Down
61 changes: 48 additions & 13 deletions src/components/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export default (function () {
return createComponentClass({

id: 'Node',

properties: {
/**
* If provided, treats these property names as neighbors, assigning them to the neighbors object. For example, ["east", "west"] creates `entity.east` and `entity.west` entity properties that are pointers to those neighbors.
*
* @property neighborProperties
* @type Array
* @default null
*/
neighborProperties: null
},

publicProperties: {
x: 0,
Expand All @@ -38,36 +49,60 @@ export default (function () {
},

initialize: function (definition) {
this.nodeId = definition.nodeId || this.owner.nodeId || this.owner.id || String(Math.random());
const owner = this.owner;

this.nodeId = definition.nodeId || owner.nodeId || owner.id || String(Math.random());

if ((typeof this.nodeId !== 'string') && (this.nodeId.length)) {
this.nodeId = definition.nodeId.join('|');
}

this.owner.nodeId = this.nodeId;
owner.nodeId = this.nodeId;

owner.isNode = true;
this.map = owner.map = owner.map || owner.parent || null;
this.contains = owner.contains = arrayCache.setUp();
this.edgesContain = owner.edgesContain = arrayCache.setUp();

this.owner.isNode = true;
this.map = this.owner.map = this.owner.map || this.owner.parent || null;
this.contains = this.owner.contains = arrayCache.setUp();
this.edgesContain = this.owner.edgesContain = arrayCache.setUp();
Vector.assign(owner, 'position', 'x', 'y', 'z');

Vector.assign(this.owner, 'position', 'x', 'y', 'z');
this.neighbors = owner.neighbors = definition.neighbors || owner.neighbors || {};

this.neighbors = this.owner.neighbors = definition.neighbors || this.owner.neighbors || {};
if (this.neighborProperties) {
const properties = this.neighborProperties;

for (let i = 0; i < properties.length; i++) {
const
propertyName = properties[i],
value = owner[propertyName];

if (value) {
this.neighbors[propertyName] = value;
}
Object.defineProperty(owner, propertyName, {
get: () => this.neighbors[propertyName],
set: (value) => {
if (value !== this.neighbors[propertyName]) {
this.neighbors[propertyName] = value;
for (let i = 0; i < this.contains.length; i++) {
this.contains[i].triggerEvent('set-directions');
}
}
}
});
}
}
},

events: {
"add-neighbors": function (neighbors) {
var i = 0,
direction = null;

for (direction in neighbors) {
for (const direction in neighbors) {
if (neighbors.hasOwnProperty(direction)) {
this.neighbors[direction] = neighbors[direction];
}
}

for (i = 0; i < this.contains.length; i++) {
for (let i = 0; i < this.contains.length; i++) {
this.contains[i].triggerEvent('set-directions');
}
},
Expand Down
Loading

0 comments on commit 8de92d4

Please sign in to comment.