GameModule.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. namespace KairoEngine.Core.ModuleSystem
  8. {
  9. [System.Serializable, HideReferenceObjectPicker]
  10. public class GameModule : IComparable<GameModule>
  11. {
  12. [SerializeField, HideInInspector] public virtual string name => "Empty Module Slot";
  13. [SerializeField, HideInInspector] public string typeName = "GameModule";
  14. [SerializeField, HideInInspector] public string className;
  15. [InlineButton("Reset", "Reset")]
  16. [InlineButton("Remove", "Remove")]
  17. [FoldoutGroup("@name")] public bool enableModule = true;
  18. [HideInInspector] public bool isInitialized = false;
  19. [HideInInspector] public GameConfig gameConfig;
  20. public GameModule(GameConfig config)
  21. {
  22. this.gameConfig = config;
  23. this.className = this.GetType().AssemblyQualifiedName;
  24. }
  25. public virtual void Load(Transform parent)
  26. {
  27. }
  28. public virtual void Reset()
  29. {
  30. }
  31. public virtual void Destroy()
  32. {
  33. }
  34. [FoldoutGroup("@name")]
  35. public virtual void Remove()
  36. {
  37. gameConfig.modules.Remove(this);
  38. }
  39. public virtual int CompareTo(GameModule other)
  40. {
  41. if(other == null) return 1;
  42. else return -1;
  43. }
  44. public static GameModuleBase InvokeStringMethod(string typeName, string methodName, string data)
  45. {
  46. Type calledType = Type.GetType(typeName);
  47. if(calledType == null)
  48. {
  49. Debug.LogError($"Could not find type: \"{typeName}\"");
  50. return null;
  51. }
  52. GameModuleBase module = (GameModuleBase)calledType.InvokeMember($"JSONTo{methodName}",
  53. System.Reflection.BindingFlags.InvokeMethod |
  54. System.Reflection.BindingFlags.Public |
  55. System.Reflection.BindingFlags.Static,
  56. null, null, new object[] { data });
  57. return module;
  58. }
  59. public static GameModule JSONToGameModule(string data)
  60. {
  61. return JsonUtility.FromJson<GameModule>(data);
  62. }
  63. public virtual void OnBeforeSerialize(ObjectSerializer serializer) { }
  64. public virtual void OnBeforeDeserialize(ObjectSerializer serializer) { }
  65. }
  66. [System.Serializable, HideReferenceObjectPicker]
  67. public class GameModuleBase : GameModule
  68. {
  69. public GameModuleBase(GameConfig config) : base(config)
  70. {
  71. this.gameConfig = config;
  72. this.className = this.GetType().AssemblyQualifiedName;
  73. this.typeName = "GameModuleBase";
  74. }
  75. }
  76. }