Skip to content

Commit

Permalink
Add 'speak' effect for NPCs to say multiple phrases over time
Browse files Browse the repository at this point in the history
  • Loading branch information
shawncplus committed Mar 26, 2017
1 parent da62962 commit d90a850
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions bundles/ranvier-areas/areas/limbo/npcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
keywords: ["wise", "old", "man"]
name: "Wise Old Man"
level: 99
script: '2-old-man'
pacifist: true
description: "A wise looking old man sits on the ground with legs crossed."
attributes:
Expand Down
35 changes: 35 additions & 0 deletions bundles/ranvier-areas/areas/limbo/scripts/npcs/2-old-man.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

'use strict';

module.exports = (srcPath) => {
const Broadcast = require(srcPath + 'Broadcast');

return {
listeners: {
playerEnter: state => function (player) {
if (this.hasEffectType('speaking')) {
return;
}

const speak = state.EffectFactory.create('speak', this, {}, {
messageList: [
"Welcome, %player%. The combat training area lies to the east.",
"To the west lies Wally's shop where you can stock up on potions.",
],
outputFn: message => {
message = message.replace(/%player%/, player.name);
state.ChannelManager.get('say').send(state, this, message);
}
});
this.addEffect(speak);
},

playerLeave: state => function (player) {
const speaking = this.effects.getByType('speaking');
if (speaking) {
speaking.remove();
}
}
}
};
};
40 changes: 40 additions & 0 deletions bundles/ranvier-effects/effects/speak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

/**
* Have an NPC speak phrases over time
*/
module.exports = srcPath => {
const Broadcast = require(srcPath + 'Broadcast');

return {
config: {
name: 'Speaking',
type: 'speaking',
tickInterval: 3,
persists: false
},
state: {
messageList: [],
remainingMessages: null,
outputFn: null
},
listeners: {
effectActivated: function () {
if (typeof this.state.outputFn !== 'function') {
throw new Error('Speak effect has no outputFn configured');
}

// copy original message list to remainingMessages
this.state.remainingMessages = this.state.messageList.concat([]);
},
updateTick: function () {
if (!this.state.remainingMessages.length) {
return this.remove();
}

const message = this.state.remainingMessages.shift();
this.state.outputFn(message);
},
}
};
};

0 comments on commit d90a850

Please sign in to comment.