Guest User

DialogSystem

a guest
May 30th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. //Contains connected dialog nodes, creating a dialog tree
  2. IncludeScript("dialog/dialog_info");
  3.  
  4. const SELECTION_COLOR_RGB = "255 255 255"
  5. const SELECTION_MARKER = "x";
  6.  
  7. //Current dialog info
  8. startNodeIndex <- 0;
  9. currentNodeIndex <- null;
  10. topLine <- "";
  11. options <- [];
  12. sndPath <- "";
  13. sndDur <- 0;
  14.  
  15. dialogString <- "";
  16.  
  17. //Entities
  18. gameText <- null;
  19. sound <- null;
  20. gameUI <- null;
  21. trigger <- null;
  22.  
  23. //User input
  24. player <- null;
  25. selection <- 0;
  26. playerControl <- false;
  27.  
  28.  
  29. function GetPlayer()
  30. {
  31. player = Entities.FindByClassname(null, "player")
  32. }
  33.  
  34. //Not actually used yet, planning on moving as much as possible out of hammer into vscript
  35. function OnPostSpawn() {
  36.  
  37. GetPlayer()
  38. if (gameText == null) {
  39. gameText = Entities.CreateByClassname("game_text")
  40.  
  41. gameText.ValidateScriptScope()
  42. local scope = gameText.GetScriptScope()
  43.  
  44. }
  45.  
  46. if(gameUI == null){
  47. gameUI = Entities.CreateByClassname("game_ui")
  48.  
  49. gameUI.ValidateScriptScope()
  50. local scope = gameUI.GetScriptScope()
  51. scope.Add <- AddSelection
  52. scope.Substract <- SubstractSelection
  53. scope.Accept <- AcceptOption
  54. gameUI.ConnectOutput("Add", "PressedForward")
  55. gameUI.ConnectOutput("Substract", "PressedBack")
  56. gameUI.ConnectOutput("Accept", "PressedAttack")
  57. }
  58.  
  59. if (sound == null){
  60. sound = Entities.CreateByClassname("ambient_generic")
  61. sound.__KeyValueFromInt("Volume", 10)
  62.  
  63. sound.ValidateScriptScope()
  64. local scope = sound.GetScriptScope()
  65. }
  66. }
  67.  
  68. function ShowDialog(){
  69. EntFire("HudHint", "ShowHudHint", "", 0, player)
  70. }
  71.  
  72. function HideDialog(){
  73. EntFire("HudHint", "HideHudHint", "", 0, player)
  74. }
  75.  
  76. //Get next in dialog tree
  77. function SetCurrentNode(index) {
  78. if (currentNodeIndex == null){
  79. printl("SetCurrentNode: Starting Node: " + startNodeIndex)
  80. currentNodeIndex = startNodeIndex;
  81. }else{
  82. local tempIndex = nodes[currentNodeIndex][3][index]
  83. local newIndex = nodes[tempIndex][3][0]
  84. printl("SetCurrentNode: Old Node: " + currentNodeIndex + " -> new Node: " + newIndex)
  85. currentNodeIndex = newIndex;
  86. }
  87. }
  88.  
  89. function SetOptions(nodeIndex)
  90. {
  91. options.clear()
  92. options.resize(0)
  93. for (local i = 0; i < nodes[nodeIndex][3].len(); i++)
  94. {
  95. options.insert(i, nodes[nodes[nodeIndex][3][i]][0])
  96. }
  97. /*
  98. for (local i = 3; i < nodes[nodeIndex].len(); i++)
  99. {
  100. options.insert(i - 3, nodes[nodes[nodeIndex][i]][0])
  101. }
  102. */
  103. printl("SetOptions:" + options)
  104. }
  105.  
  106. function SetTopLine(nodeIndex)
  107. {
  108. topLine = nodes[nodeIndex][0]
  109. printl("SetTopLine: " + topLine)
  110. }
  111.  
  112. function SetSndPath(nodeIndex)
  113. {
  114. sndPath = nodes[nodeIndex][1]
  115. printl("SetSndPath: " + sndPath)
  116. }
  117.  
  118. function SetSndDur(nodeIndex)
  119. {
  120. sndDur = nodes[nodeIndex][2]
  121. printl("SetSndDur: " + sndDur)
  122. }
  123.  
  124. function FetchOptionsCount() {
  125. return options.len()
  126. }
  127.  
  128. //Progress to next Node and get new dialog info
  129. function FetchDialogInfo(){
  130. printl("FetchDialogInfo")
  131. SetCurrentNode(selection)
  132. SetTopLine(currentNodeIndex)
  133. SetOptions(currentNodeIndex)
  134. SetSndPath(currentNodeIndex)
  135. SetSndDur(currentNodeIndex)
  136. CreateDialogString()
  137. }
  138.  
  139. function startNodeFuntion(nodeIndex){
  140.  
  141. }
  142.  
  143. //Create String of availiable options, one marked
  144. function OptionsToString(index){
  145. printl("OptionsToString")
  146. local optionsString = "";
  147. for (local i = 0; i < options.len(); i++)
  148. {
  149. if (i == index){
  150. optionsString = optionsString + SELECTION_MARKER + options[i] + SELECTION_MARKER + "\n";
  151. }else if(i != index){
  152. optionsString = optionsString + options[i] + "\n";
  153. }
  154. }
  155. return optionsString;
  156. }
  157.  
  158. //Put Strings together
  159. function CreateDialogString(){
  160. printl("CreateDialogString")
  161. dialogString = topLine + "\n" + "\n" + OptionsToString(selection);
  162. printl(dialogString);
  163. }
  164.  
  165. function PlayNode() {
  166. printl("PlayNode")
  167.  
  168. EntFire("HudHint", "AddOutput", "message " + topLine, 0)
  169. ShowDialog()
  170.  
  171. printl("Playing Sound: " + sndPath + "(" + sndDur + "s)")
  172. EntFire("Sound", "AddOutput", "message " + sndPath, 0)
  173. EntFire("Sound", "PlaySound", "", 0)
  174. EntFire("Sound", "StopSound", "", sndDur)
  175.  
  176. if (options.len() > 1){
  177. printl("Multiple Nodes found")
  178. }else {
  179. printl("Single Node found")
  180. }
  181. EntFire("HudHint", "AddOutput", "message " + dialogString, sndDur)
  182. EntFire("HudHint", "ShowHudHint", "", sndDur, activator)
  183. EntFire("Script", "RunScriptCode", "SetPlayerControl(1)", sndDur)
  184.  
  185. }
  186.  
  187. //Change the selected option
  188. function AddSelection() {
  189. if (playerControl == true){
  190. local upperBound = FetchOptionsCount() - 1;
  191. printl("upperBound: " + upperBound)
  192. if (selection < upperBound) {
  193. selection++;
  194. }else if(selection == upperBound){
  195. selection = upperBound;
  196. }
  197. CreateDialogString();
  198. EntFire("HudHint", "AddOutput", "message " + dialogString, 0);
  199. ShowDialog()
  200. }
  201. }
  202.  
  203. //Change the selected option
  204. function SubstractSelection() {
  205. if (playerControl == true){
  206. local lowerBound = 0;
  207. if (selection > lowerBound) {
  208. selection--;
  209. }else if(selection == lowerBound){
  210. selection = lowerBound;
  211. }
  212. CreateDialogString();
  213. EntFire("HudHint", "AddOutput", "message " + dialogString, 0);
  214. ShowDialog()
  215. }
  216. }
  217.  
  218. //Get new node info based on selected option and play node
  219. function AcceptOption() {
  220. printl("AcceptOption")
  221. if (playerControl == true){
  222. SetPlayerControl(0)
  223. FetchDialogInfo()
  224. PlayNode()
  225. selection = 0
  226. }
  227. }
  228.  
  229. function SetPlayerControl(mode){
  230. switch(mode){
  231. case 0: playerControl = false;
  232. case 1: playerControl = true;
  233. }
  234. printl("SetPlayerControl: " + playerControl)
  235. }
  236.  
  237. function RefreshDialog(){
  238. printl("Timer Fired")
  239. EntFire("HudHint", "ShowHudHint", "", 0, player)
  240. }
  241.  
  242. function StartDialog(){
  243.  
  244. }
Add Comment
Please, Sign In to add comment