ChatPlane.gd 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. extends Popper
  2. var chat_distance = 0.8
  3. var author_displacement = 3.0
  4. var max_msg = 3
  5. var max_length = 10
  6. var tw = Tween.new()
  7. var messages = Array()
  8. var sc = 1.0/(max_msg+1)*0.4
  9. var chosen = 0
  10. var cur_choice
  11. var choicebar
  12. var refracter = false
  13. var rmargin = 6.0*sc*10
  14. var lmargin = 6.0*sc*10
  15. var vmargin = .2
  16. var mycolor = Color8(200,100,130)
  17. var cur_dialog = Array()
  18. var cur_position = 0
  19. onready var PLAYER = find_parent("MAIN").find_node("Player")
  20. onready var CHATTABLE = get_parent().get_parent()
  21. class_name ChatPlane
  22. func advance():
  23. if cur_position>=cur_dialog.size():
  24. PLAYER.end_chat()
  25. return
  26. var el = cur_dialog[cur_position]
  27. if el is Saying:
  28. if el.text!="":
  29. if el.actor==Saying.MYSELF:
  30. isay(el.text)
  31. elif el.actor == Saying.DRUNK:
  32. drunksay(el.text)
  33. else:
  34. itsays(el.text)
  35. else:
  36. print("skipping line")
  37. cur_position+=1
  38. advance()
  39. return
  40. if el.action and el.action!="":
  41. CHATTABLE.call_deferred(el.action)
  42. if el is Array:
  43. choice(el)
  44. if el is Unconditional:
  45. start(el.text)
  46. advance()
  47. return #preventing side-effects when this function DOES end
  48. cur_position+=1
  49. func _process(delta):
  50. if cur_choice and !refracter:
  51. if Input.is_action_just_pressed("interact"):
  52. choose()
  53. return
  54. if Input.is_action_just_pressed("ui_right"):
  55. chosen+=1
  56. update_choice()
  57. if Input.is_action_just_pressed("ui_left"):
  58. chosen-=1
  59. update_choice()
  60. refracter=false
  61. if !cur_choice:
  62. if Input.is_action_just_pressed("interact"):
  63. advance()
  64. func _ready():
  65. # "trying to summon label")
  66. randomize()
  67. if find_parent("MAIN").find_node("Clock"):#enables going from endscreen also
  68. find_parent("MAIN").find_node("Clock").stop_time()
  69. PLAYER.find_node("Spatial").visible = false
  70. rmargin+=CHATTABLE.rmargin
  71. rmargin+=CHATTABLE.lmargin
  72. vmargin+=CHATTABLE.vmargin
  73. #get_world().environment.tonemap_exposure=0.8
  74. translation = Vector3(0, -vmargin, -chat_distance)
  75. scale = Vector3(0.09,0.09,0.09)
  76. cur_dialog = CHATTABLE.init_() # it is a starting dialog not _init
  77. advance()
  78. func isay(msg):
  79. var l = message(msg, mycolor)
  80. var s = l.text.length()
  81. var margin = lmargin #from the center
  82. l.translation += Vector3(-margin+(s*(0.5-0.1))*6.0*sc,0,0)
  83. func drunksay(msg):
  84. itsays(msg, Color8(50,200,100))
  85. func itsays(msg, col = null):
  86. var l
  87. if col!=null:
  88. l = message(msg, col)
  89. else:
  90. l = message(msg)
  91. var margin = rmargin #from the center
  92. var s = l.text.length()
  93. l.translation += Vector3(margin-s*(0.5-0.1)*6.0*sc,0,0)
  94. func message(msg, col = Color8(200,200,110)):
  95. play_sound()
  96. # print("message started")
  97. var label = Label3D.new()
  98. label.text = msg
  99. label.color = col
  100. #label.power = 2.5
  101. #label.power = 2.4
  102. label.power = 1.8
  103. label.scale = Vector3(sc,sc,sc)
  104. # tw.interpolate_property(label,"translation",Vector3(0,-8,0),Vector3(0,0,0),1.0,Tween.TRANS_CUBIC,Tween.EASE_OUT)
  105. # tw.start()
  106. add_2_stack(label)
  107. # print("message ended")
  108. return label
  109. func add_2_stack(label):
  110. label.translation = Vector3(0,-10.0*sc*(float(max_msg)/2.0),0)
  111. add_child(label)
  112. for m in messages:
  113. m.translation+=Vector3(0,10.0*sc,0)
  114. messages.push_front(label)
  115. if messages.size()>max_msg:
  116. messages.pop_back().queue_free()
  117. func unpush_stack():
  118. var t = messages.pop_front()
  119. t.queue_free()
  120. for m in messages:
  121. m.translation+=Vector3(0,-10.0*sc,0)
  122. func choice(choices):
  123. var nonzero_choices = Array()
  124. for c in choices:
  125. if c.text != "":
  126. nonzero_choices.append(c)
  127. cur_choice = nonzero_choices
  128. chosen = 0
  129. update_choice()
  130. func update_choice():
  131. chosen=wrapi(chosen,0,cur_choice.size())
  132. if choicebar:
  133. unpush_stack()
  134. choicebar=message(cur_choice[chosen].text)
  135. generate_arrows(cur_choice.size()>1)
  136. func start(method):
  137. cur_dialog=CHATTABLE.call(method)
  138. cur_position=0
  139. func choose():
  140. unpush_stack()
  141. var target_method=cur_choice[chosen].scenario
  142. start(target_method)
  143. choicebar = null
  144. cur_choice = null
  145. advance()
  146. func generate_arrows(active):
  147. # for c in choicebar.get_children():
  148. # if c is Arrow:
  149. # c.queue_free()
  150. var width = choicebar.text.length()*6
  151. var ar1 = Arrow.new()
  152. var ar2 = Arrow.new()
  153. ar2.left=false
  154. var offset = Vector3(float(width)/2+5.0,0,0)
  155. ar1.translation = -offset
  156. ar2.translation = offset
  157. for a in [ar1,ar2]:
  158. a.active = active
  159. choicebar.add_child(a)
  160. func close():
  161. if not find_parent("MAIN").find_node("Clock").startlock:
  162. find_parent("MAIN").find_node("Clock").start_time()
  163. PLAYER.find_node("Spatial").visible = true
  164. #TODO: it might make sense to child player hand to a camera. But it will also often look glitchy
  165. queue_free()