GameAction.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.Core.GameActions
  8. {
  9. [System.Serializable, HideReferenceObjectPicker]
  10. public abstract class GameAction : IConvertible, IComparable<GameAction>
  11. {
  12. public virtual string name { get; }
  13. public virtual GameActionsController controller { get => _controller; set => _controller = value; }
  14. [NonSerialized] protected internal GameActionsController _controller;
  15. [HideInInspector] public string typeName = "GameAction";
  16. public virtual string GetTypeName() => "GameAction";
  17. public virtual string GetActionName() => "Generic Action";
  18. [HideInInspector] public string className;
  19. /// <summary>This is set to True if the Start function has already been called.</summary>
  20. public bool started { get => _started; }
  21. protected internal bool _started = false;
  22. /// <summary>Set to True when the action has finished execution. In the next frame the controller will move to the next GameAction in its list.</summary>
  23. public bool done { get => _done; }
  24. protected internal bool _done = false;
  25. public GameAction(GameActionsController controller = null)
  26. {
  27. if(controller != null) this.controller = controller;
  28. className = this.GetType().AssemblyQualifiedName;
  29. }
  30. public virtual void Initialize(GameActionObjectSerializer objectSerializer) { }
  31. /// <summary>The Start function is fired once when the action starts executing.</summary>
  32. public virtual void Start() { }
  33. /// <summary>The Update function is fired every frame while executing this GameAction.</summary>
  34. public virtual void Update() { }
  35. /// <summary>The Restart function is fired once on each GameAction in a controller when all actions have the bool done set to True.</summary>
  36. public virtual void Restart()
  37. {
  38. _started = false;
  39. _done = false;
  40. }
  41. public TypeCode GetTypeCode()
  42. {
  43. return this.GetTypeCode();
  44. }
  45. public bool ToBoolean(IFormatProvider provider)
  46. {
  47. throw new NotImplementedException();
  48. }
  49. public byte ToByte(IFormatProvider provider)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. public char ToChar(IFormatProvider provider)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. public DateTime ToDateTime(IFormatProvider provider)
  58. {
  59. throw new NotImplementedException();
  60. }
  61. public decimal ToDecimal(IFormatProvider provider)
  62. {
  63. throw new NotImplementedException();
  64. }
  65. public double ToDouble(IFormatProvider provider)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. public short ToInt16(IFormatProvider provider)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. public int ToInt32(IFormatProvider provider)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. public long ToInt64(IFormatProvider provider)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. public sbyte ToSByte(IFormatProvider provider)
  82. {
  83. throw new NotImplementedException();
  84. }
  85. public float ToSingle(IFormatProvider provider)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. public string ToString(IFormatProvider provider)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. public object ToType(Type conversionType, IFormatProvider provider)
  94. {
  95. //Debug.Log("Coverting type....");
  96. if(conversionType == typeof(DebugLogGameAction)) return this as DebugLogGameAction;
  97. else return this;
  98. }
  99. public ushort ToUInt16(IFormatProvider provider)
  100. {
  101. throw new NotImplementedException();
  102. }
  103. public uint ToUInt32(IFormatProvider provider)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. public ulong ToUInt64(IFormatProvider provider)
  108. {
  109. throw new NotImplementedException();
  110. }
  111. public static GameActionBase InvokeStringMethod(string typeName, string methodName, string data)
  112. {
  113. Type calledType = Type.GetType(typeName);
  114. if(calledType == null)
  115. {
  116. Debug.LogError($"Could not find type: \"{typeName}\"");
  117. return null;
  118. }
  119. GameActionBase action = (GameActionBase)calledType.InvokeMember($"JSONTo{methodName}",
  120. System.Reflection.BindingFlags.InvokeMethod |
  121. System.Reflection.BindingFlags.Public |
  122. System.Reflection.BindingFlags.Static,
  123. null, null, new object[] { data });
  124. return action;
  125. }
  126. public int CompareTo(GameAction other)
  127. {
  128. if(other == null) return 1;
  129. else return -1;
  130. }
  131. public virtual void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { }
  132. public virtual void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { }
  133. }
  134. public class GameActionBase : GameAction
  135. {
  136. public override GameActionsController controller {
  137. get => _controller;
  138. set
  139. {
  140. _controller = value;
  141. typeName = "GameActionBase";
  142. }
  143. }
  144. public GameActionBase(GameActionsController controller) : base(controller)
  145. {
  146. this.controller = controller;
  147. className = this.GetType().AssemblyQualifiedName;
  148. }
  149. }
  150. }