Advertisement
tieudattai

Sephiroth.cs

Mar 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1.   class Sephiroth
  2.     {
  3.         static Sephiroth instance;
  4.         static Random Ran = new Random();
  5.  
  6.         public static Sephiroth Instance
  7.         {
  8.             get
  9.             {
  10.                 if (instance == null)
  11.                     instance = new Sephiroth(0);
  12.                 return instance;
  13.             }
  14.         }
  15.  
  16.         public Sephiroth(int id)
  17.         {
  18.             Key = id;
  19.             Root.Add(Key, this);
  20.             Baby.Add(Key, this);
  21.             Baby = new Dictionary<int, object>();
  22.             Root = new Dictionary<int, object>();
  23.         }
  24.  
  25.         public int Key { get; set; }
  26.         public Sephiroth Mother { get; set; }
  27.         public string Name { get; set; } = string.Empty;
  28.  
  29.         public bool IsSurvival
  30.         {
  31.             get
  32.             {
  33.                 return Root.ContainsKey(Key);
  34.             }
  35.         }
  36.  
  37.         public Dictionary<int, object> Baby;
  38.         public Dictionary<int, object> Root;
  39.         public Dictionary<int, object> Children => new Dictionary<int, object>();
  40.  
  41.         public Sephiroth Pregnant()
  42.         {
  43.             Sephiroth element;
  44.             while (true)
  45.             {
  46.                 int key = Ran.Next();
  47.                 if (!Baby.ContainsKey(key))
  48.                 {
  49.                     element = new Sephiroth(key);
  50.                     element.Mother = this;
  51.                     Baby.Add(key, element);
  52.                     break;
  53.                 }
  54.             }
  55.             return element;
  56.         }
  57.  
  58.         public IEnumerable<Sephiroth> GetFamily()
  59.         {
  60.             foreach (Sephiroth element in Children.ToList().Select(e => e.Value).ToList())
  61.             {
  62.                 yield return element;
  63.                 element.GetFamily();
  64.             }
  65.         }
  66.  
  67.         public void Destroy()
  68.         {
  69.             Root.Remove(Key);
  70.         }
  71.  
  72.         public void Relive()
  73.         {
  74.             if (Baby.ContainsKey(Key))
  75.             {
  76.                 if (!Root.ContainsKey(Key))
  77.                 {
  78.                     Root.Add(Key, Baby[Key]);
  79.                 }
  80.             }
  81.         }
  82.  
  83.         public bool IsAncestorOf(Sephiroth element)
  84.         {
  85.             return GetFamily().Contains(element);
  86.         }
  87.  
  88.         public bool IsDescendantOf(Sephiroth element)
  89.         {
  90.             return element.GetFamily().Contains(element);
  91.         }
  92.  
  93.         public bool DoesShareHierarchyWith(Sephiroth element)
  94.         {
  95.             return IsAncestorOf(element) || IsDescendantOf(element);
  96.         }
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement