Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New hierarchy find functions #4603

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
db2c2e0
Add functions to find parents
NewboO Aug 31, 2022
6028413
Add functions to find script instances and components
NewboO Aug 31, 2022
d0722b9
Unit test for new graph node functions
NewboO Aug 31, 2022
9a6cbbe
Unit test for new find component functions
NewboO Aug 31, 2022
6b3ece6
Unit test for new find script functions
NewboO Aug 31, 2022
3f2c647
graph-node.js lint fix
NewboO Aug 31, 2022
9505c6d
entity.test.mjs lint fix
NewboO Aug 31, 2022
c3f30c2
Fix doc
NewboO Aug 31, 2022
56577ec
Fix doc
NewboO Aug 31, 2022
f9ee083
Const and array spreading in all find functions
NewboO Aug 31, 2022
4c803a9
Further refactoring of find functions
NewboO Sep 1, 2022
edb8038
Another refactoring in findOne
NewboO Sep 1, 2022
dc35892
Avoid creating and pushing empty arrays
NewboO Sep 1, 2022
90fa55e
Pluralize and make non-recursive when possible
NewboO Sep 1, 2022
b4ecb30
Allow looking for falsey values
NewboO Sep 2, 2022
4b4636a
Renaming, factorizing and adding forEachParent
NewboO Sep 2, 2022
cffd4dc
Simplifying a bit more some find functions
NewboO Sep 2, 2022
582f2d4
More simplifications
NewboO Sep 4, 2022
8419b28
Always ignore self node + renaming parents to ancestors
NewboO Sep 6, 2022
5ef124b
Let the user choose to include self node
NewboO Sep 7, 2022
78b52ca
Doc
NewboO Sep 7, 2022
6e4597b
Tests descriptions
NewboO Sep 7, 2022
f126819
Specific functions for descendants only search
NewboO Sep 12, 2022
fdd3f98
Doc
NewboO Sep 12, 2022
1c08147
Doc
NewboO Sep 13, 2022
ee4f7ad
Update src/scene/graph-node.js
NewboO Sep 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions src/framework/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { AppBase } from './app-base.js';
/** @typedef {import('./components/scroll-view/component.js').ScrollViewComponent} ScrollViewComponent */
/** @typedef {import('./components/sound/component.js').SoundComponent} SoundComponent */
/** @typedef {import('./components/sprite/component.js').SpriteComponent} SpriteComponent */
/** @typedef {import('../script/script-type.js').ScriptType} ScriptType */

/**
* @type {GraphNode[]}
Expand Down Expand Up @@ -408,6 +409,186 @@ class Entity extends GraphNode {
});
}

/**
* Search the entity and all of its descendants for the first script instance of specified type.
*
* @param {string|Class<ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @returns {ScriptType} A script instance of specified type, if the entity or any of its descendants
* has one. Returns undefined otherwise.
* @example
* // Get the first found "playerController" instance in the hierarchy tree that starts with this entity
* var controller = entity.findScript("playerController");
*/
findScript(nameOrType) {
const entity = this.findOne(function (node) {
return node.c && node.c.script && node.c.script.has(nameOrType);
});
return entity && entity.c.script.get(nameOrType);
}

/**
* Search the entity and all of its descendants for all script instances of specified type.
*
* @param {string|Class<ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @returns {ScriptType[]} All script instances of specified type in the entity or any of its
* descendants. Returns empty array if none found.
* @example
* // Get all "playerController" instances in the hierarchy tree that starts with this entity
* var controllers = entity.findScripts("playerController");
*/
findScripts(nameOrType) {
const entities = this.find(function (node) {
return node.c && node.c.script && node.c.script.has(nameOrType);
});
return entities.map(function (entity) {
return entity.c.script.get(nameOrType);
});
}

/**
* Search all the entity descendants for the first component of specified type.
*
* @param {string} type - The name of the component type to retrieve.
* @returns {Component} A component of specified type, if any of entity descendants has one.
* Returns undefined otherwise.
* @example
* // Get the first found light component in the hierarchy tree excluding this entity
* var light = entity.findComponentInDescendants("light");
*/
findComponentInDescendants(type) {
const entity = this.findDescendant(function (node) {
return node.c && node.c[type];
});
return entity && entity.c[type];
}

/**
* Search all the entity descendants for all components of specified type.
*
* @param {string} type - The name of the component type to retrieve.
* @returns {Component[]} All components of specified type in all of entity descendants.
* Returns empty array if none found.
* @example
* // Get all light components in the hierarchy tree excluding this entity
* var lights = entity.findComponentsInDescendants("lights");
*/
findComponentsInDescendants(type) {
const entities = this.findDescendants(function (node) {
return node.c && node.c[type];
});
return entities.map(function (entity) {
return entity.c[type];
});
}

/**
* Search all the entity descendants for the first script instance of specified type.
*
* @param {string|Class<ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @returns {ScriptType} A script instance of specified type, if any of entity descendants has one.
* Returns undefined otherwise.
* @example
* // Get the first found "playerController" instance in the hierarchy tree excluding this entity
* var controller = entity.findScriptInDescendants("playerController");
*/
findScriptInDescendants(nameOrType) {
const entity = this.findDescendant(function (node) {
return node.c && node.c.script && node.c.script.has(nameOrType);
});
return entity && entity.c.script.get(nameOrType);
}

/**
* Search all the entity descendants for all script instances of specified type.
*
* @param {string|Class<ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @returns {ScriptType[]} All script instances of specified type in all of entity descendants.
* Returns empty array if none found.
* @example
* // Get all "playerController" instance in the hierarchy tree excluding this entity
* var controllers = entity.findScriptsInDescendants("playerController");
*/
findScriptsInDescendants(nameOrType) {
const entities = this.findDescendants(function (node) {
return node.c && node.c.script && node.c.script.has(nameOrType);
});
return entities.map(function (entity) {
return entity.c.script.get(nameOrType);
});
}

/**
* Search all the entity ascendants for the first component of specified type.
*
* @param {string} type - The name of the component type to retrieve.
* @returns {Component} A component of specified type, if any of entity ascendants has one.
* Returns undefined otherwise.
* @example
* // Get the first found light component in the ancestor tree excluding this entity
* var light = entity.findComponentInAncestors("light");
*/
findComponentInAncestors(type) {
const entity = this.findAncestor(function (node) {
return node.c && node.c[type];
});
return entity && entity.c[type];
}

/**
* Search all the entity ascendants for all components of specified type.
*
* @param {string} type - The name of the component type to retrieve.
* @returns {Component[]} All components of specified type in all of entity ascendants.
* Returns empty array if none found.
* @example
* // Get all element components in the ancestor tree excluding this entity
* var elements = entity.findComponentsInAncestors("element");
*/
findComponentsInAncestors(type) {
const entities = this.findAncestors(function (node) {
return node.c && node.c[type];
});
return entities.map(function (entity) {
return entity.c[type];
});
}

/**
* Search all the entity ascendants for the first script instance of specified type.
*
* @param {string|Class<ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @returns {ScriptType} A script instance of specified type, if any of entity ascendants has one.
* Returns undefined otherwise.
* @example
* // Get the first found "playerController" instance in the ancestor tree excluding this entity
* var controller = entity.findScriptInAncestors("playerController");
*/
findScriptInAncestors(nameOrType) {
const entity = this.findAncestor(function (node) {
return node.c && node.c.script && node.c.script.has(nameOrType);
});
return entity && entity.c.script.get(nameOrType);
}

/**
* Search all the entity ascendants for all script instances of specified type.
*
* @param {string|Class<ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @returns {ScriptType[]} All script instances of specified type in all of entity ascendants.
* Returns empty array if none found.
* @example
* // Get all "playerController" instance in the ancestor tree excluding this entity
* var controllers = entity.findScriptsInAncestors("playerController");
*/
findScriptsInAncestors(nameOrType) {
const entities = this.findAncestors(function (node) {
return node.c && node.c.script && node.c.script.has(nameOrType);
});
return entities.map(function (entity) {
return entity.c.script.get(nameOrType);
});
}

/**
* Get the GUID value for this Entity.
*
Expand Down
Loading