Advertisement
Crazist

авп

Aug 8th, 2022
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4.  
  5. namespace GameInit.GameCycleModule
  6. {
  7.     [DisallowMultipleComponent]
  8.     public class GameCycle : MonoBehaviour
  9.     {
  10.         private readonly Dictionary<CycleMethod, List<ICallable>> _classesToUpdate = new Dictionary<CycleMethod, List<ICallable>>();
  11.  
  12.         // ====== TEST ======
  13.         private readonly List<IUpdate> _updates = new List<IUpdate>(100);
  14.         private readonly List<ILateUpdate> _lateUpdates = new List<ILateUpdate>(20);
  15.         // ==================
  16.  
  17.         public void Init()
  18.         {
  19.             _classesToUpdate[CycleMethod.Update] = new List<ICallable>();
  20.  
  21.             _classesToUpdate[CycleMethod.LateUpdate] = new List<ICallable>();
  22.         }
  23.        
  24.         public void Add(CycleMethod method, ICallable callable)
  25.         {
  26.             if (!_classesToUpdate[method].Contains(callable))
  27.             {
  28.                 _classesToUpdate[method].Add(callable);
  29.             }
  30.         }
  31.  
  32.         public void Remove(CycleMethod method, ICallable callable)
  33.         {
  34.             if (_classesToUpdate[method].Contains(callable))
  35.             {
  36.                 _classesToUpdate[method].Remove(callable);
  37.             }
  38.         }
  39.  
  40.         // ====== TEST ======
  41.         public void Add(IUpdate update)
  42.         {
  43.             _updates.Add(update);
  44.         }
  45.  
  46.         public void Add(ILateUpdate lateUpdate)
  47.         {
  48.             _lateUpdates.Add(lateUpdate);
  49.         }
  50.  
  51.         public void Remove(IUpdate update)
  52.         {
  53.             _updates.Remove(update);
  54.         }
  55.  
  56.         public void Remove(ILateUpdate lateUpdate)
  57.         {
  58.             _lateUpdates.Remove(lateUpdate);
  59.         }
  60.         // ==================
  61.  
  62.         private void Update()
  63.         {
  64.             foreach (var item in _classesToUpdate[CycleMethod.Update].ToArray())
  65.             {
  66.                 item.UpdateCall();
  67.             }
  68.  
  69.             // ====== TEST ======
  70.             foreach (var update in _updates.ToArray())
  71.             {
  72.                 update.OnUpdate();
  73.             }
  74.             // ==================
  75.         }
  76.  
  77.         private void LateUpdate()
  78.         {
  79.             foreach (var item in _classesToUpdate[CycleMethod.LateUpdate].ToArray())
  80.             {
  81.                 item.UpdateCall();
  82.             }
  83.  
  84.             // ====== TEST ======
  85.             foreach (var lateUpdate in _lateUpdates.ToArray())
  86.             {
  87.                 lateUpdate.OnLateUpdate();
  88.             }
  89.             // ==================
  90.         }
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement