-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'speak' effect for NPCs to say multiple phrases over time
- Loading branch information
1 parent
da62962
commit d90a850
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
bundles/ranvier-areas/areas/limbo/scripts/npcs/2-old-man.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
} | ||
}; | ||
}; |