Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / InkLibs / InkCompiler / ParsedHierarchy / TunnelOnwards.cs
  1. using System.Collections.Generic;
  2. namespace Ink.Parsed
  3. {
  4. public class TunnelOnwards : Parsed.Object
  5. {
  6. public Divert divertAfter {
  7. get {
  8. return _divertAfter;
  9. }
  10. set {
  11. _divertAfter = value;
  12. if (_divertAfter) AddContent (_divertAfter);
  13. }
  14. }
  15. Divert _divertAfter;
  16. public override Runtime.Object GenerateRuntimeObject ()
  17. {
  18. var container = new Runtime.Container ();
  19. // Set override path for tunnel onwards (or nothing)
  20. container.AddContent (Runtime.ControlCommand.EvalStart ());
  21. if (divertAfter) {
  22. // Generate runtime object's generated code and steal the arguments runtime code
  23. var returnRuntimeObj = divertAfter.GenerateRuntimeObject ();
  24. var returnRuntimeContainer = returnRuntimeObj as Runtime.Container;
  25. if (returnRuntimeContainer) {
  26. // Steal all code for generating arguments from the divert
  27. var args = divertAfter.arguments;
  28. if (args != null && args.Count > 0) {
  29. // Steal everything betwen eval start and eval end
  30. int evalStart = -1;
  31. int evalEnd = -1;
  32. for (int i = 0; i < returnRuntimeContainer.content.Count; i++) {
  33. var cmd = returnRuntimeContainer.content [i] as Runtime.ControlCommand;
  34. if (cmd) {
  35. if (evalStart == -1 && cmd.commandType == Runtime.ControlCommand.CommandType.EvalStart)
  36. evalStart = i;
  37. else if (cmd.commandType == Runtime.ControlCommand.CommandType.EvalEnd)
  38. evalEnd = i;
  39. }
  40. }
  41. for (int i = evalStart + 1; i < evalEnd; i++) {
  42. var obj = returnRuntimeContainer.content [i];
  43. obj.parent = null; // prevent error of being moved between owners
  44. container.AddContent (returnRuntimeContainer.content [i]);
  45. }
  46. }
  47. }
  48. // Supply the divert target for the tunnel onwards target, either variable or more commonly, the explicit name
  49. var returnDivertObj = returnRuntimeObj as Runtime.Divert;
  50. if( returnDivertObj != null && returnDivertObj.hasVariableTarget ) {
  51. var runtimeVarRef = new Runtime.VariableReference (returnDivertObj.variableDivertName);
  52. container.AddContent(runtimeVarRef);
  53. } else {
  54. _overrideDivertTarget = new Runtime.DivertTargetValue ();
  55. container.AddContent (_overrideDivertTarget);
  56. }
  57. }
  58. // No divert after tunnel onwards
  59. else {
  60. container.AddContent (new Runtime.Void ());
  61. }
  62. container.AddContent (Runtime.ControlCommand.EvalEnd ());
  63. container.AddContent (Runtime.ControlCommand.PopTunnel ());
  64. return container;
  65. }
  66. public override void ResolveReferences (Story context)
  67. {
  68. base.ResolveReferences (context);
  69. if (divertAfter && divertAfter.targetContent)
  70. _overrideDivertTarget.targetPath = divertAfter.targetContent.runtimePath;
  71. }
  72. Runtime.DivertTargetValue _overrideDivertTarget;
  73. }
  74. }