InteractionToolsManager.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.GameTools.InteractionTools
  6. {
  7. /// <summary>
  8. /// Manages which interaction tool is currently on.
  9. /// </summary>
  10. public class InteractionToolsManager : MonoBehaviour
  11. {
  12. public static InteractionToolsManager instance;
  13. [ReadOnly] public int currentTools = 0;
  14. public List<GameObject> tools = new List<GameObject>();
  15. private List<IInteractionTool> toolInterfaces = new List<IInteractionTool>();
  16. private void Awake()
  17. {
  18. if(instance == null) instance = this;
  19. else Destroy(this);
  20. }
  21. private void Start()
  22. {
  23. RegisterInterfaces();
  24. ChangeTool(currentTools);
  25. }
  26. private void RegisterInterfaces()
  27. {
  28. toolInterfaces = new List<IInteractionTool>();
  29. for (int i = 0; i < tools.Count; i++)
  30. {
  31. IInteractionTool toolInterface = tools[i].GetComponent<IInteractionTool>();
  32. if(toolInterface == null) toolInterface = tools[i].GetComponentInChildren<IInteractionTool>();
  33. if(toolInterface != null) toolInterfaces.Add(toolInterface);
  34. else Debug.LogError($"No Interaction tool interface found in components on \"{tools[i].name}\"", tools[i]);
  35. }
  36. }
  37. public void ChangeTool(int toolIndex)
  38. {
  39. currentTools = toolIndex;
  40. for (int i = 0; i < toolInterfaces.Count; i++)
  41. {
  42. if(i != toolIndex) toolInterfaces[i].DisableTool();
  43. else toolInterfaces[i].EnableTool();
  44. }
  45. }
  46. }
  47. }