GameActionContextStatsController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Stats;
  7. using KairoEngine.Core.GameActions;
  8. namespace KairoEngine.Stats.GameActions
  9. {
  10. public class GameActionContextStatsController : GameActionContextVariableBase
  11. {
  12. public StatsController value {
  13. get
  14. {
  15. if(_value == null && searchTarget != null) UpdateSearchTarget();
  16. return _value;
  17. }
  18. set => _value = value; }
  19. [HorizontalGroup("value"), LabelText("@name"), ShowInInspector, PropertyOrder(1), ReadOnly, OnInspectorInit("UpdateSearchTarget")]
  20. private string result = "";
  21. [HorizontalGroup("value", 0.14f), Button("@editButtonName"), ShowIf("@canEdit"), PropertyOrder(2)]
  22. private void EditValue()
  23. {
  24. if(showEdit == false)
  25. {
  26. showEdit = true;
  27. editButtonName = "Done";
  28. }
  29. else
  30. {
  31. showEdit = false;
  32. editButtonName = "Edit";
  33. }
  34. }
  35. private string editButtonName = "Edit";
  36. [NonSerialized] internal StatsController _value;
  37. [NonSerialized, ShowInInspector, ShowIf("@showEdit && canEdit"), PropertyOrder(4), LabelText("Search in"), OnValueChanged("UpdateSearchTarget")]
  38. internal GameObject searchTarget;
  39. private void UpdateSearchTarget()
  40. {
  41. if(searchTarget == null) return;
  42. var statsComponent = searchTarget.GetComponent<StatsComponent>();
  43. if(statsComponent == null)
  44. {
  45. statsComponent = searchTarget.GetComponentInChildren<StatsComponent>();
  46. }
  47. if(statsComponent != null)
  48. {
  49. _value = statsComponent.statsController;
  50. result = $"{statsComponent.gameObject.name} StatsComponent";
  51. }
  52. else
  53. {
  54. if(_value == null) result = "";
  55. else result = "StatController";
  56. }
  57. }
  58. public override string GetTypeName() => "GameActionContextStatsController";
  59. public override string GetVariableName() => "StatsController";
  60. public override T GetValue<T>(T defaultValue)
  61. {
  62. var result = value;
  63. try
  64. {
  65. return (T)System.Convert.ChangeType(value, typeof(T));
  66. }
  67. catch
  68. {
  69. return defaultValue;
  70. }
  71. }
  72. public static GameActionContextStatsController JSONToGameActionContextStatsController(string data)
  73. {
  74. return JsonUtility.FromJson<GameActionContextStatsController>(data);
  75. }
  76. public override void OnBeforeSerialize(GameActionObjectSerializer serializer)
  77. {
  78. serializer.SerializeGameObject($"{name}-searchTarget", searchTarget);
  79. }
  80. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer)
  81. {
  82. searchTarget = serializer.DeserializeGameObject($"{name}-searchTarget");
  83. //UpdateSearchTarget();
  84. }
  85. }
  86. }