Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / InkLibs / InkCompiler / ParsedHierarchy / Conditional.cs
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Ink.Runtime;
  4. namespace Ink.Parsed
  5. {
  6. public class Conditional : Parsed.Object
  7. {
  8. public Expression initialCondition { get; private set; }
  9. public List<ConditionalSingleBranch> branches { get; private set; }
  10. public Conditional (Expression condition, List<ConditionalSingleBranch> branches)
  11. {
  12. this.initialCondition = condition;
  13. if (this.initialCondition) {
  14. AddContent (condition);
  15. }
  16. this.branches = branches;
  17. if (this.branches != null) {
  18. AddContent (this.branches.Cast<Parsed.Object> ().ToList ());
  19. }
  20. }
  21. public override Runtime.Object GenerateRuntimeObject ()
  22. {
  23. var container = new Runtime.Container ();
  24. // Initial condition
  25. if (this.initialCondition) {
  26. container.AddContent (initialCondition.runtimeObject);
  27. }
  28. // Individual branches
  29. foreach (var branch in branches) {
  30. var branchContainer = (Container) branch.runtimeObject;
  31. container.AddContent (branchContainer);
  32. }
  33. // If it's a switch-like conditional, each branch
  34. // will have a "duplicate" operation for the original
  35. // switched value. If there's no final else clause
  36. // and we fall all the way through, we need to clean up.
  37. // (An else clause doesn't dup but it *does* pop)
  38. if (this.initialCondition != null && branches [0].ownExpression != null && !branches [branches.Count - 1].isElse) {
  39. container.AddContent (Runtime.ControlCommand.PopEvaluatedValue ());
  40. }
  41. // Target for branches to rejoin to
  42. _reJoinTarget = Runtime.ControlCommand.NoOp ();
  43. container.AddContent (_reJoinTarget);
  44. return container;
  45. }
  46. public override void ResolveReferences (Story context)
  47. {
  48. var pathToReJoin = _reJoinTarget.path;
  49. foreach (var branch in branches) {
  50. branch.returnDivert.targetPath = pathToReJoin;
  51. }
  52. base.ResolveReferences (context);
  53. }
  54. Runtime.ControlCommand _reJoinTarget;
  55. }
  56. }