public void BeginNextRoundSetup(IApiClient client, SetupState setupState)
 {
     SetupState = setupState;
     SetupState.IsAttackerReady = false;
     SetupState.IsDefenderReady = false;
     client.SetupStarted();
 }
        private void AttackerMove(IApiClient client, SetupState setupState)
        {
            RoundState.AttackerInfo.PositionX += RoundState.AttackerInfo.Speed * setupState.AttackerUpgrades.Last().SpeedMultiplier;

            System.Console.Out.WriteLine("MOVING >:D (x:{0} y:{1})", RoundState.AttackerInfo.PositionX, RoundState.AttackerInfo.PositionY);
            client.AttackerMoved((int)RoundState.AttackerInfo.PositionX, (int)RoundState.AttackerInfo.PositionY);
        }
        public void StartGameLoop(IApiClient client, SetupState setupState, SetupController setupController)
        {
            if (RoundState != null && RoundState.IsRoundStarted)
            {
                System.Console.WriteLine("Game is already started");
                client.ErrorOccured("Game is already started");
                return;
            }

            RoundState = new RoundState();
            RoundState.IsRoundStarted = true;

            client.RoundStarted();

            //bool success = true;
            Task.Factory.StartNew(() =>
            {
                InitializeAttackerInfo(setupState);
                while (!IsRoundOver(setupState)&&RoundState.IsRoundStarted)
                {
                    IsAttackerInRange(client, setupState);
                    AttackerMove(client, setupState);
                    //AttackerRecievedDamage(client);

                    Task.Delay(300).Wait();
                }

                StopShooting(client);
                RoundState.IsRoundStarted = false;
                client.RoundFinished();
                EndOfGame(client);
            });
        }
        public void BeginFirstRoundSetup(IApiClient client)
        {
            SetupState = new SetupState();
            Map defMap = new Map();
            SetupState.Map = defMap.defaultMap();

            client.GameInitialized(SetupState.Map);
            client.SetupStarted();
        }
 //Damage recieved by tower
 private void AttackerRecievedDamage(IApiClient client, SetupState setupState, uint damage)
 {
     RoundState.AttackerInfo.CurrentHealth -= damage * setupState.AttackerUpgrades.Last().ArmorMultiplier;
     if (RoundState.AttackerInfo.CurrentHealth <= 0)
     {
         System.Console.Out.WriteLine("Attacker lost!");
     }
     else
     {
         client.AttackerReceivedDamage((int)RoundState.AttackerInfo.CurrentHealth);
         System.Console.Out.WriteLine("-"+damage * setupState.AttackerUpgrades.Last().ArmorMultiplier+"hp!!! HEALTH LEFT:" + RoundState.AttackerInfo.CurrentHealth);
     }
 }
 private bool IsRoundOver(SetupState setupState)
 {
     Cell finishCell = setupState.Map.Cells.First(cell => cell.Type == "Finish");
     bool hasAttackerReachedFinish = RoundState.AttackerInfo.PositionX >= finishCell.PosX;
     bool isAttackerDead = RoundState.AttackerInfo.CurrentHealth <= 0;
     return hasAttackerReachedFinish || isAttackerDead;
 }
 private void IsAttackerInRange(IApiClient client, SetupState setupState)
 {
     foreach(Tower tower in setupState.Towers)
     {
         Cell towerCell = setupState.Map.Cells.First(cell => cell.CellId == tower.CellId);
         AttackerInfo attackerInfo = RoundState.AttackerInfo;
         float x = Math.Abs(towerCell.PosX - attackerInfo.PositionX);
         float y = Math.Abs(towerCell.PosY - attackerInfo.PositionY);
         double diagonal = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
         if (tower.Range >= diagonal)
         {
             TowerIsShooting(client, tower.CellId);
             AttackerRecievedDamage(client, setupState, tower.Damage);
         }
         else
         {
             TowerIsIdle(client, tower.CellId);
         }
     }
 }
        private void InitializeAttackerInfo(SetupState setupState)
        {
            Cell startCell = setupState.Map.Cells.First(cell => cell.Type == "Start");

            RoundState.AttackerInfo = new AttackerInfo();
            RoundState.AttackerInfo.CurrentHealth = 100;
            RoundState.AttackerInfo.MaxHealth = 100;
            RoundState.AttackerInfo.PositionX = startCell.PosX;
            RoundState.AttackerInfo.PositionY = startCell.PosY;
            RoundState.AttackerInfo.Speed = 1;
        }