Guest User

Untitled

a guest
Apr 26th, 2017
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node
  2.  
  3. var panelNode
  4. var story = { }
  5. var events = { }
  6. var choices = { }
  7. var currDialogue
  8. var inDialogue = false
  9. var currChoice
  10. var buttonPressed
  11. var isChoice
  12. var isChoiceDialogue
  13. var oldChoices = { }
  14. var oldWasChoice
  15. var dialogueOptions = []
  16. func load_file_as_JSON(path, target):
  17. var file = File.new()
  18. file.open(path, file.READ)
  19. var content = (file.get_as_text())
  20. target.parse_json(content)
  21. file.close()
  22. func _ready():
  23. set_process_input(true)
  24. set_fixed_process(true)
  25. load_file_as_JSON("Narrative/cycle1.json", story)
  26. load_file_as_JSON("Narrative/events.json", events)
  27. load_file_as_JSON("Narrative/choices.json", choices)
  28.  
  29. panelNode = get_node("../CanvasLayer/Panel")
  30.  
  31. var initButton = panelNode.get_node("Button")
  32. initButton.connect("pressed", self, "_on_button_pressed", [initButton])
  33.  
  34. opening()
  35.  
  36. func _fixed_process(delta):
  37. if(inDialogue == false):
  38. panelNode.hide()
  39.  
  40. func opening():
  41. inDialogue = true
  42. panelNode.show()
  43. currDialogue = story["awakening"]["content"]
  44. panelNode.get_node("Label").set_text(currDialogue[0])
  45.  
  46. func load_next():
  47. if currDialogue[1].has("divert"):
  48. oldWasChoice == false # used for update_choices, make sure to clear each iteration
  49. currDialogue = story[currDialogue[1]["divert"]]["content"]
  50. else:
  51. oldWasChoice = true
  52. oldChoices = currDialogue.size() # used for update_choices, make sure to clear each iteration
  53. currDialogue = story[currDialogue[get_user_choice(currChoice)]["linkPath"]]["content"]
  54. print("succesfully transitioned to choice " + str(currDialogue))
  55.  
  56.  
  57. func _on_Button_pressed():
  58. load_next()
  59. panelNode.get_node("Label").set_text(currDialogue[0]) #prints next line to screen
  60. update_choices(currDialogue)
  61.  
  62.  
  63. func _on_button_pressed_target(target):
  64. currChoice = target
  65. load_next()
  66. panelNode.get_node("Label").set_text(currDialogue[0]) #prints next line to screen
  67. update_choices(currDialogue)
  68.  
  69.  
  70. func get_user_choice(target):
  71. var buttonName = target.get_name()
  72. var id = buttonName.to_int()
  73. return id
  74.  
  75. func update_choices(dialogue): # takes dialogue and makes enough buttons and labels for the corresponding choice
  76. # must be called after load_next()
  77. if (oldWasChoice == true): #if previous dialogue was a choice
  78. for i in range (1, oldChoices):
  79. panelNode.get_node("ChoiceButton" + str(i)).queue_free()
  80. print("queued freed button " + str(i))
  81.  
  82.  
  83. for item in currDialogue:
  84. if(typeof(item) != TYPE_STRING and item.has("linkPath")):
  85. dialogueOptions.append(item)
  86.  
  87. for i in range(1, dialogue.size()):
  88. var choiceButton = Button.new()
  89. choiceButton.set_name("ChoiceButton" + str(i))
  90. panelNode.add_child(choiceButton)
  91. choiceButton.set_pos(Vector2(10, (20*i) + 72))
  92. choiceButton.set_size(Vector2(512,20))
  93. choiceButton.connect("pressed", self, "_on_button_pressed_target", [choiceButton])
  94.  
  95. var choiceLabel = Label.new()
  96. choiceLabel.set_name("Choice Label" + str(i))
  97. panelNode.get_node("ChoiceButton" + str(i)).add_child(choiceLabel)
  98. choiceLabel.set_pos(Vector2(10, 5))
  99. choiceLabel.set_size(Vector2(512, 20))
  100. choiceLabel.set_autowrap(true)
  101. choiceLabel.set_text(dialogueOptions[i-1]["option"])
  102. choiceLabel.show()
  103. print("____________")
  104. print(str(i))
  105. print(choiceLabel.get_text())
  106. print(choiceLabel.get_visible_characters())
  107. print(choiceLabel.get_lines_skipped())
  108. print(choiceLabel.get_max_lines_visible())
  109. print(choiceLabel.is_visible())
  110. print(choiceLabel.is_hidden())
  111.  
  112. oldWasChoice = null
  113. oldChoices = null
  114. dialogueOptions.clear()
RAW Paste Data