OnStorylineGameActionTrigger.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using KairoEngine.Core.GameActions;
  8. using KairoEngine.StorySystem;
  9. namespace KairoEngine.StorySystem.GameActions
  10. {
  11. [System.Serializable, HideReferenceObjectPicker]
  12. public class OnStorylineGameActionTrigger : GameActionTriggerBase
  13. {
  14. public enum StorylineCondition
  15. {
  16. Contains,
  17. Equal,
  18. NotEqual
  19. }
  20. public override string name
  21. {
  22. get
  23. {
  24. return $"On Storyline";
  25. }
  26. }
  27. public override GameActionTriggersController controller {
  28. get => _controller;
  29. set
  30. {
  31. _controller = value;
  32. typeName = "OnStorylineGameActionTrigger";
  33. }
  34. }
  35. public override string GetTypeName() => "OnStorylineGameActionTrigger";
  36. public override string GetTriggerName() => "On Storyline";
  37. [IconFoldoutGroup("@name","Assets/Plugins/KairoEngine/StorySystem/Editor/Icons/OnStorylineGameActionTriggerIcon.png")]
  38. public string storyName = "Storyline";
  39. [IconFoldoutGroup("@name")] public StorylineCondition condition = StorylineCondition.Contains;
  40. [IconFoldoutGroup("@name")] public StoryStepType storyStepType = StoryStepType.Line;
  41. [IconFoldoutGroup("@name")] public string value = "";
  42. public override void OnEnable()
  43. {
  44. EventManager.broadcast.StartListening(storyName, OnStoryStep);
  45. }
  46. public override void Update()
  47. {
  48. }
  49. public override void OnDisable()
  50. {
  51. EventManager.broadcast.StopListening(storyName, OnStoryStep);
  52. }
  53. private void OnStoryStep(StoryStepData storyStep)
  54. {
  55. if(storyStep.category != storyStepType) return;
  56. switch (storyStep.category)
  57. {
  58. case StoryStepType.Line:
  59. if(condition == StorylineCondition.Equal && value == storyStep.text) TriggerActions();
  60. if(condition == StorylineCondition.Contains && storyStep.text.Contains(value)) TriggerActions();
  61. if(condition == StorylineCondition.NotEqual && value != storyStep.text) controller.TriggerActions();
  62. break;
  63. case StoryStepType.Branch:
  64. foreach (var branch in storyStep.branches)
  65. {
  66. if(condition == StorylineCondition.Equal && value == branch) TriggerActions();
  67. if(condition == StorylineCondition.Contains && branch.Contains(value)) TriggerActions();
  68. if(condition == StorylineCondition.NotEqual && value != branch) controller.TriggerActions();
  69. }
  70. break;
  71. case StoryStepType.Path:
  72. if(condition == StorylineCondition.Equal && value == storyStep.text) TriggerActions();
  73. if(condition == StorylineCondition.Contains && storyStep.text.Contains(value)) TriggerActions();
  74. if(condition == StorylineCondition.NotEqual && value != storyStep.text) TriggerActions();
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. public void TriggerActions()
  81. {
  82. EventManager.broadcast.StopListening(storyName, OnStoryStep);
  83. controller.TriggerActions();
  84. }
  85. public static OnStorylineGameActionTrigger JSONToOnStorylineGameActionTrigger(string data)
  86. {
  87. return JsonUtility.FromJson<OnStorylineGameActionTrigger>(data);
  88. }
  89. private OnStorylineGameActionTrigger Duplicate()
  90. {
  91. OnStorylineGameActionTrigger trigger = new OnStorylineGameActionTrigger();
  92. trigger.controller = controller;
  93. return trigger;
  94. }
  95. }
  96. }