Skip to content

Commit

Permalink
fixes #52 Refactor AreaAudience to use Area#getBroadcastTargets
Browse files Browse the repository at this point in the history
Also fixes area scripts in general
  • Loading branch information
shawncplus committed Feb 24, 2019
1 parent ceb8c60 commit 4f42494
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/Area.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ class Area extends GameEntity {
room.hydrate(state);
}
}

/**
* Get all possible broadcast targets within an area. This includes all npcs,
* players, rooms, and the area itself
* @return {Array<Broadcastable>}
*/
getBroadcastTargets() {
const roomTargets = [...this.rooms].reduce((acc, [, room]) => acc.concat(room.getBroadcastTargets()), []);
return [this, ...roomTargets];
}
}

module.exports = Area;
12 changes: 2 additions & 10 deletions src/AreaAudience.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,8 @@ class AreaAudience extends ChannelAudience {
return [];
}

// It would be more elegant to just pass the area but that could be very
// inefficient as it's much more likely that there are fewer players than
// there are rooms in the area
const players = this.state.PlayerManager.filter(player =>
player.room &&
(player.room.area === this.sender.room.area) &&
(player !== this.sender)
);

return players.concat(this.sender.room.area.npcs);
const { area } = this.sender.room;
return area.getBroadcastTargets().filter(target => target !== this.sender);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/AreaFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AreaFactory extends EntityFactory {
const area = new Area(definition.bundle, entityRef, definition.manifest);

if (this.scripts.has(entityRef)) {
this.scripts.get(entityRef).attach(entity);
this.scripts.get(entityRef).attach(area);
}

return area;
Expand Down
10 changes: 5 additions & 5 deletions src/BundleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,13 @@ class BundleManager {
const scriptPath = this._getAreaScriptPath(bundle, areaName);

if (manifest.script) {
const scriptPath = `${scriptPath}/${area.script}.js`;
if (!fs.existsSync(scriptPath)) {
Logger.warn(`\t\t\t[${areaName}] has non-existent script "${area.script}"`);
const areaScriptPath = `${scriptPath}/${manifest.script}.js`;
if (!fs.existsSync(areaScriptPath)) {
Logger.warn(`\t\t\t[${areaName}] has non-existent script "${manifest.script}"`);
}

Logger.verbose(`\t\t\tLoading Item Script [${entityRef}] ${item.script}`);
this.loadEntityScript(this.state.AreaFactory, entityRef, scriptPath);
Logger.verbose(`\t\t\tLoading Area Script for [${areaName}]: ${manifest.script}`);
this.loadEntityScript(this.state.AreaFactory, areaName, areaScriptPath);
}

Logger.verbose(`\t\tLOAD: Quests...`);
Expand Down

0 comments on commit 4f42494

Please sign in to comment.