amphibia89

Mirror Image - Wizard

Jul 28th, 2016
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. namespace OOPExercise.Models
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class Wizard
  7.     {
  8.         public static int ReflectionsCount;
  9.  
  10.         private static readonly Dictionary<int, Wizard> wizardById;
  11.  
  12.         private Wizard leftWizardReflection;
  13.  
  14.         private Wizard rightWizardReflection;
  15.  
  16.         static Wizard()
  17.         {
  18.             ReflectionsCount = -1;
  19.             wizardById = new Dictionary<int, Wizard>();
  20.         }
  21.  
  22.         private Wizard(string name, int magicalPower)
  23.         {
  24.             this.Id = ++ReflectionsCount;
  25.             this.Name = name;
  26.             this.MagicalPower = magicalPower;
  27.         }
  28.  
  29.         public event EventHandler ReflectionSpellCasted;
  30.        
  31.         public event EventHandler FireballSpellCasted;
  32.  
  33.         public string Name { get; }
  34.  
  35.         public int Id { get; }
  36.  
  37.         public int MagicalPower { get; }
  38.  
  39.         public Wizard this[int index] => wizardById[index];
  40.  
  41.         public static Wizard Parse(string wizardInfo)
  42.         {
  43.             var wizardArgs = wizardInfo
  44.                 .Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
  45.             var wizardName = wizardArgs[0];
  46.             var wizardMagicPower = int.Parse(wizardArgs[1]);
  47.            
  48.             var wizard = new Wizard(wizardName, wizardMagicPower);
  49.             wizardById.Add(wizard.Id, wizard);
  50.  
  51.             return wizard;
  52.         }
  53.  
  54.         public void CastReflectionSpell()
  55.         {
  56.             this.ReflectionSpellCasted?.Invoke(this, EventArgs.Empty);
  57.             this.CastReflectionSpell(ref this.leftWizardReflection);
  58.             this.CastReflectionSpell(ref this.rightWizardReflection);
  59.         }
  60.  
  61.         public void CastFireballSpell()
  62.         {
  63.             this.FireballSpellCasted?.Invoke(this, EventArgs.Empty);
  64.             this.CastFireballSpell(ref this.leftWizardReflection);
  65.             this.CastFireballSpell(ref this.rightWizardReflection);
  66.         }
  67.  
  68.         private void CastReflectionSpell(ref Wizard wizardReflection)
  69.         {
  70.             if (wizardReflection != null)
  71.             {
  72.                 wizardReflection.CastReflectionSpell();
  73.             }
  74.             else
  75.             {
  76.                 wizardReflection = new Wizard(this.Name, this.MagicalPower / 2);
  77.                 wizardById.Add(wizardReflection.Id, wizardReflection);
  78.             }
  79.         }
  80.  
  81.         private void CastFireballSpell(ref Wizard wizardReflection)
  82.         {
  83.             wizardReflection?.CastFireballSpell();
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment