Skip to content

Commit

Permalink
Adds an Ent.isA() function in order to know whether it is a Loc, Obj …
Browse files Browse the repository at this point in the history
…or Persona
  • Loading branch information
Baltasarq committed Nov 1, 2023
1 parent ab78181 commit 47c101d
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions lib/fi.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,47 @@ const EntTraits = (function() {
this.objs = [];
}

/** Determines whether this entity is subchild of obj.
* @param obj the object to test for parentship.
* @return true if this is subchild of obj, false otherwise.
*/
function isA(obj)
{
let toret = false;
let parent = this;

if ( obj != null ) {
while( parent != null ) {
if ( obj == parent ) {
toret = true;
break;
}

parent = parent.__proto__;
}
}

return toret;
}

/** @return whether this entity is a Loc. */
function isLoc()
{
return this.isA( LocTraits );
}

/** @return whether this entity is a an Obj. */
function isObj()
{
return this.isA( ObjTraits );
}

/** @return whether this entity is a Persona. */
function isPersona()
{
return this.isA( PersonaTraits );
}

/** Executed in order to initialize the entity
* at the beginning of the game.
*/
Expand Down Expand Up @@ -288,6 +329,15 @@ const EntTraits = (function() {
// constructor & ini
"ctor": ctor,
"ini": ini,
// isA
"isA": isA,
"esUn": isA,
"isLoc": isLoc,
"esLoc": isLoc,
"isObj": isObj,
"esObj": isObj,
"isPersona": isPersona,
"esPersona": isPersona,
// showObjectsInside
"isListingObjectsInside": isListingObjectsInside,
"setListsObjectsInside": setListsObjectsInside,
Expand Down Expand Up @@ -2426,15 +2476,15 @@ const ctrl = ( function() {
for(const ENT of pendingEnts.values()) {
ENT.ini();

if ( ENT instanceof Loc
|| ENT instanceof Persona )
if ( ENT.isLoc()
|| ENT.isPersona() )
{
ENT.objs.forEach( obj => newPendingEnts.add( obj ) );

if ( ENT instanceof Loc ) {
if ( ENT.isLoc() ) {
ENT.personas.forEach( persona => {
newPendingEnts.add( persona );
persona.objs.forEach( newPendingEnts.add( obj ) );
persona.objs.forEach( obj => newPendingEnts.add( obj ) );
});
}
}
Expand Down

0 comments on commit 47c101d

Please sign in to comment.