123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- namespace KairoEngine.CharacterSystem
- {
- public class CharacterManager : MonoBehaviour
- {
- public static CharacterManager instance;
- public List<CharacterController> sceneCharacters = new List<CharacterController>();
- public List<string> worldCharacterNames = new List<string>();
- public List<string> spawnerIds = new List<string>();
- void OnEnable()
- {
- RegisterEvents.OnRegisterCharacter += RegisterCharacter;
- }
- void OnDisable()
- {
- RegisterEvents.OnRegisterCharacter += RegisterCharacter;
- }
- void Awake()
- {
- if(instance == null) instance = this;
- else Destroy(this.gameObject);
- }
- void Update()
- {
- for (int i = 0; i < sceneCharacters.Count; i++)
- {
- if(sceneCharacters[i] == null)
- {
- sceneCharacters.RemoveAt(i);
- i -= 1;
- }
- }
- }
- private void RegisterCharacter(CharacterController character)
- {
- sceneCharacters.Add(character);
- if(nameIsRegistered(character.unique_name)) return;
- worldCharacterNames.Add(character.unique_name);
- }
- public static void RegisterSpawner(string id) => instance.spawnerIds.Add(id);
- public static bool nameIsRegistered(string characterName)
- {
- for (int i = 0; i < instance.worldCharacterNames.Count; i++)
- {
- if(instance.worldCharacterNames[i] == characterName) return true;
- }
- return false;
- }
- public static bool spawnerIsRegistered(string id)
- {
- for (int i = 0; i < instance.spawnerIds.Count; i++)
- {
- if(instance.spawnerIds[i] == id) return true;
- }
- return false;
- }
- public static void Reset()
- {
- instance.sceneCharacters.Clear();
- instance.worldCharacterNames.Clear();
- instance.spawnerIds.Clear();
- }
- public static CharacterController GetCharacter(string name)
- {
- for (int i = 0; i < instance.sceneCharacters.Count; i++)
- {
- if(instance.sceneCharacters[i].unique_name == name) return instance.sceneCharacters[i];
- }
- return null;
- }
- }
- }
|