Advertisement
ralke23

voiceModule.lua

May 2nd, 2020
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.06 KB | None | 0 0
  1. -- VoiceModule
  2. VoiceModule = {
  3.    voices = nil,
  4.    voiceCount = 0,
  5.    lastVoice = 0,
  6.    timeout = nil,
  7.    chance = nil,
  8.    npcHandler = nil
  9. }
  10.  
  11. -- Creates a new instance of VoiceModule
  12. function VoiceModule:new(voices, timeout, chance)
  13.    local obj = {}
  14.    setmetatable(obj, self)
  15.    self.__index = self
  16.  
  17.    obj.voices = voices
  18.    for i = 1, #obj.voices do
  19.        if obj.voices[i].yell then
  20.            obj.voices[i].yell = nil
  21.            obj.voices[i].talktype = TALKTYPE_YELL
  22.        else
  23.            obj.voices[i].talktype = TALKTYPE_SAY
  24.        end
  25.    end
  26.  
  27.    obj.voiceCount = #voices
  28.    obj.timeout = timeout or 5
  29.    obj.chance = chance or 90
  30.    return obj
  31. end
  32.  
  33. function VoiceModule:init(handler)
  34.    return true
  35. end
  36.  
  37. function VoiceModule:callbackOnThink()
  38.    if self.lastVoice < os.time() then
  39.        self.lastVoice = os.time() + self.timeout
  40.        if math.random(100) < self.chance  then
  41.            local voice = self.voices[math.random(self.voiceCount)]
  42.            Npc():say(voice.text, voice.talktype)
  43.        end
  44.    end
  45.    return true
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement