Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace OOPExercise.Models
- {
- using System;
- using System.Collections.Generic;
- public class Wizard
- {
- public static int ReflectionsCount;
- private static readonly Dictionary<int, Wizard> wizardById;
- private Wizard leftWizardReflection;
- private Wizard rightWizardReflection;
- static Wizard()
- {
- ReflectionsCount = -1;
- wizardById = new Dictionary<int, Wizard>();
- }
- private Wizard(string name, int magicalPower)
- {
- this.Id = ++ReflectionsCount;
- this.Name = name;
- this.MagicalPower = magicalPower;
- }
- public event EventHandler ReflectionSpellCasted;
- public event EventHandler FireballSpellCasted;
- public string Name { get; }
- public int Id { get; }
- public int MagicalPower { get; }
- public Wizard this[int index] => wizardById[index];
- public static Wizard Parse(string wizardInfo)
- {
- var wizardArgs = wizardInfo
- .Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
- var wizardName = wizardArgs[0];
- var wizardMagicPower = int.Parse(wizardArgs[1]);
- var wizard = new Wizard(wizardName, wizardMagicPower);
- wizardById.Add(wizard.Id, wizard);
- return wizard;
- }
- public void CastReflectionSpell()
- {
- this.ReflectionSpellCasted?.Invoke(this, EventArgs.Empty);
- this.CastReflectionSpell(ref this.leftWizardReflection);
- this.CastReflectionSpell(ref this.rightWizardReflection);
- }
- public void CastFireballSpell()
- {
- this.FireballSpellCasted?.Invoke(this, EventArgs.Empty);
- this.CastFireballSpell(ref this.leftWizardReflection);
- this.CastFireballSpell(ref this.rightWizardReflection);
- }
- private void CastReflectionSpell(ref Wizard wizardReflection)
- {
- if (wizardReflection != null)
- {
- wizardReflection.CastReflectionSpell();
- }
- else
- {
- wizardReflection = new Wizard(this.Name, this.MagicalPower / 2);
- wizardById.Add(wizardReflection.Id, wizardReflection);
- }
- }
- private void CastFireballSpell(ref Wizard wizardReflection)
- {
- wizardReflection?.CastFireballSpell();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment