CharacterManager.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.CharacterSystem
  6. {
  7. public class CharacterManager : MonoBehaviour
  8. {
  9. public static CharacterManager instance;
  10. public List<CharacterController> sceneCharacters = new List<CharacterController>();
  11. public List<string> worldCharacterNames = new List<string>();
  12. public List<string> spawnerIds = new List<string>();
  13. void OnEnable()
  14. {
  15. RegisterEvents.OnRegisterCharacter += RegisterCharacter;
  16. }
  17. void OnDisable()
  18. {
  19. RegisterEvents.OnRegisterCharacter += RegisterCharacter;
  20. }
  21. void Awake()
  22. {
  23. if(instance == null) instance = this;
  24. else Destroy(this.gameObject);
  25. }
  26. void Update()
  27. {
  28. for (int i = 0; i < sceneCharacters.Count; i++)
  29. {
  30. if(sceneCharacters[i] == null)
  31. {
  32. sceneCharacters.RemoveAt(i);
  33. i -= 1;
  34. }
  35. }
  36. }
  37. private void RegisterCharacter(CharacterController character)
  38. {
  39. sceneCharacters.Add(character);
  40. if(nameIsRegistered(character.unique_name)) return;
  41. worldCharacterNames.Add(character.unique_name);
  42. }
  43. public static void RegisterSpawner(string id) => instance.spawnerIds.Add(id);
  44. public static bool nameIsRegistered(string characterName)
  45. {
  46. for (int i = 0; i < instance.worldCharacterNames.Count; i++)
  47. {
  48. if(instance.worldCharacterNames[i] == characterName) return true;
  49. }
  50. return false;
  51. }
  52. public static bool spawnerIsRegistered(string id)
  53. {
  54. for (int i = 0; i < instance.spawnerIds.Count; i++)
  55. {
  56. if(instance.spawnerIds[i] == id) return true;
  57. }
  58. return false;
  59. }
  60. public static void Reset()
  61. {
  62. instance.sceneCharacters.Clear();
  63. instance.worldCharacterNames.Clear();
  64. instance.spawnerIds.Clear();
  65. }
  66. public static CharacterController GetCharacter(string name)
  67. {
  68. for (int i = 0; i < instance.sceneCharacters.Count; i++)
  69. {
  70. if(instance.sceneCharacters[i].unique_name == name) return instance.sceneCharacters[i];
  71. }
  72. return null;
  73. }
  74. }
  75. }