public void Shoot(IList <Soldier> soldiers)
        {
            // 0 is not a magic number, refers to empty, nothing, default, dead, etc..
            var isDead = HP <= 0;

            if (isDead)
            {
                return;
            }

            Soldier target = PickRandomTarget(soldiers);

            // Logging has been decoupled.
            // Usign event instead, which is still part of shooting (still doing one thing).
            ShotsFired?.Invoke(this, new ShotFiredEventArgs(Name, target.Name));

            // 100 is not a magic number, usually refers to 100%.
            var hitRoll = _random.Next(100);

            if (IsTargetHit(hitRoll))
            {
                _consequitiveShots++;
                DealDamage(target);
            }
            else
            {
                _consequitiveShots = 0;
            }
        }
Ejemplo n.º 2
0
 public void OnShoot()
 {
     while (true)
     {
         if (rng.Next(0, 100) % 2 == 0)
         {
             ShotsFired?.Invoke(this, new ShotsFiredEventArgs(DateTime.Now));
         }
         else
         {
             Console.WriteLine("I missed!");
         }
         Thread.Sleep(500);
     }
 }
Ejemplo n.º 3
0
        public void OnShoot()
        {
            while (true)
            {
                if (rng.Next(0, 100) % 2 == 0)
                {
                    ShotsFired?.Invoke(this, new ShotsFiredEventArgs(DateTime.Now)); // EventName? means if Event is not Null (if it has subscribers)
                }
                else
                {
                    Console.WriteLine("I Missed!");
                }

                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 4
0
 public void OnShoot()
 {
     while (true)
     {
         if (rng.Next(0, 100) % 2 == 0)
         {
             //if(ShotsFired != null)
             //{
             //    ShotsFired.Invoke(this, new ShotsFiredEventArgs(DateTime.Now));
             //}
             //replace with
             ShotsFired?.Invoke(this, new ShotsFiredEventArgs(DateTime.Now));
         }
         else
         {
             Console.WriteLine("I Missed!");
         }
         Thread.Sleep(500);
     }
 }
Ejemplo n.º 5
0
        // Now we need a method that will invoke this event
        public void OnShoot()
        {
            while (true)                       // Infinite loop
            {
                if (rng.Next(0, 100) % 2 == 0) // 50% chance to kill
                {
                    if (ShotsFired != null)    // Everytime we call on events, we need to check to see if the event is empty. If NOT EMPTY (then it has some subscribers), then INVOKE
                    {
                        // Raising the event using Invoke. In here we put the signature of our delegate. Who is our sender, who is raising the event? The Shooter class.
                        // We will need to use the this keyword, then we can pass empty EventArgs for now
                        ShotsFired.Invoke(this, new ShotsFiredEventArgs()
                        {
                            TimeOfKill = DateTime.Now
                        });                                                                             // "this" is what sends information about the object that raises the event. If we put the name property here like above, our subscriber methods will have access to them
                    }
                }
                else
                {
                    Console.WriteLine("I missed!");
                }

                Thread.Sleep(500);
            }
        }