You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

69 lines
2.0 KiB

  1. extends Object
  2. class_name YarnRunner
  3. var yarnScript : YarnScript
  4. var say_func : FuncRef = null
  5. var choices_func : FuncRef = null
  6. var commands := {}
  7. signal resume_workaround
  8. func run_all():
  9. var next_node = yarnScript.nodes["Start"]
  10. while next_node != null:
  11. print("-> Jump to " + next_node["title"])
  12. var next_node_key = yield(run_body(next_node["body"]), "completed")
  13. if (yarnScript.nodes.has(next_node_key)):
  14. next_node = yarnScript.nodes[next_node_key]
  15. else:
  16. next_node = null
  17. func run_body(body):
  18. yield(Controller.get_tree(), "idle_frame") # workaround before await
  19. for element in body:
  20. if element["type"] == "jump":
  21. return element["node"]
  22. elif element["type"] == "command":
  23. yield(run_command(element["command"], element["args"]), "completed")
  24. elif element["type"] == "choice_blocks":
  25. var block = yield(decide_choice_block(element["blocks"]), "completed")
  26. print(block)
  27. return block["node"] # todo change later with "-> choices"
  28. elif element["type"] == "condition_blocks":
  29. var block = decide_condition_block(element["blocks"])
  30. var next_node_key = yield(run_body(block["body"]), "completed")
  31. if next_node_key != "":
  32. return next_node_key
  33. else:
  34. if say_func == null or !say_func.is_valid():
  35. printerr("no say_func provided")
  36. else:
  37. yield(say_func.call_func(element), "completed")
  38. return ""
  39. func add_command(command_key, function):
  40. commands[command_key] = function
  41. func run_command(command_key, args):
  42. if (commands.has(command_key)):
  43. var command : FuncRef = commands[command_key]
  44. yield(command.call_func(args), "completed")
  45. func decide_choice_block(blocks):
  46. if choices_func == null or !choices_func.is_valid():
  47. printerr("no choices_func provided")
  48. else:
  49. return yield(choices_func.call_func(blocks), "completed")
  50. func decide_condition_block(blocks):
  51. for block in blocks:
  52. var expr := Expression.new()
  53. var err := expr.parse(block["condition"])
  54. if err == OK and expr.execute():
  55. return block
  56. return null