private void CreateNewGame(FormType formType)
 {
   FormForSelection selectorForm = new FormForSelection(formType);
   if (selectorForm.ShowDialog() == DialogResult.OK)
   {
     if (_game != null)
     {
       if (_timer != null)
         _timer.Stop();
       _game = null;
     }
     try
     {
       _game = Game.Factory(selectorForm.ReturnFileName(),
                            formType == FormType.GameConfiguration ? FactoryAct.Create : FactoryAct.Load,
                            _graphObject);
     }
     catch
     {
       _game = null;
     }
     if (_game != null)
     {
       _game.Scaling = _currentScale;
       _gameMenu = null;
       MessageBox.Show("Game conf loaded successeful");
     }
     if (_timer != null)
       _timer.Start();
   }
 }
 /// <summary>
 /// Creates the new game.
 /// </summary>
 /// <param name="formType">Type of the form.</param>
 private void CreateNewGame(FormType formType)
 {
   FormForSelection selectorForm = new FormForSelection(formType);
   if(selectorForm.ShowDialog() != DialogResult.OK)
   {
     return;
   }
   _game = null;
   try
   {
     _game = Game.Factory(selectorForm.ReturnFileName(),
                          formType == FormType.GameConfiguration ? FactoryAct.Create : FactoryAct.Load,
                          _graphObject);
   }
   catch
   {
     _game = null;
   }
   if(_game == null)
   {
     return;
   }
   _game.Scaling = _currentScale;
   _gameMenu = null;
   _graphObject.ClearCache();
   MessageBox.Show(Resources.Game_created_successeful);
 }
    /// <summary>
    /// Render all game objects
    /// </summary>
    /// <param name="gameObj">The game obj.</param>
    internal void Show(Game gameObj)
    {
      //Fill background
      _graphObject.FillRectangle(new SolidBrush(_backgroundColor), 0, 0, Convert.ToInt32(Settings.WindowWidth * gameObj.Scaling),
        Convert.ToInt32(Settings.WindowHeight * gameObj.Scaling));
      //Show map area
      MapAreaShowing(gameObj);
#if Debug
      _graphObject.DrawString(gameObj.Monsters.Count.ToString(CultureInfo.InvariantCulture), new Font("Arial", Settings.ElemSize),
        new SolidBrush(Color.Black), new Point(0, 0));
#endif

      #region Controls

      {
        //The line of breakup
        _graphObject.DrawLine(new Pen(new SolidBrush(Color.White), 3 * gameObj.Scaling),
          new Point(Convert.ToInt32(Settings.BreakupLineXPosition * gameObj.Scaling), 0),
          new Point(Convert.ToInt32(Settings.BreakupLineXPosition * gameObj.Scaling), Convert.ToInt32(Settings.WindowHeight * gameObj.Scaling)));
        ShowMoney(gameObj);//Gold
        ShowLives(gameObj);//lives
        /*ShowPageSelector(gameObj);//Shop menu
        ShowTowerShopPage(gameObj);//Shop page*/
        if ((gameObj.TowerConfSelectedID != -1) || (gameObj.TowerMapSelectedID != -1))//If needs to show tower params
          ShowTowerParams(gameObj);
      }

      #endregion

      //Will be removed later(May be), useless thing, change picture when game paused
      if (gameObj.Paused)
        _graphObject.MakeGray(0, 0, Convert.ToInt32(Settings.WindowWidth * gameObj.Scaling), Convert.ToInt32(Settings.WindowHeight * gameObj.Scaling));
    }
 private void TimerTick(object obj, EventArgs e)
 {
   _timer.Stop();
   if (_gameMenu != null)
   {
     _gameMenu.Show();
   }
   if (_game != null)
   {
     _game.Tick(PBGame.PointToClient(MousePosition));
     _game.Render();
     if (_game.Lose || _game.Won)
     {
       //_timer.Stop();
       MessageBox.Show(_game.Lose
         ? GameCoClassLibrary.Properties.Resources.Looser_message
         : GameCoClassLibrary.Properties.Resources.Winner_message);
       float scaling = _game.Scaling;
       _game = null;
       _gameMenu = new MainMenu(_graphObject);
       GameResize(scaling);
       //_timer.Start();
       return;
     }
   }
   _graphObject.Render();
   _timer.Start();
 }
 /// <summary>
 /// Recreates the constant map image.
 /// </summary>
 /// <param name="gameObj">The game obj.</param>
 /// <param name="scale">The scale.</param>
 internal void RecreateConstantImage(Game gameObj, float scale)
 {
   _constantMapImage = new Bitmap(Convert.ToInt32((gameObj.Map.VisibleXFinish - gameObj.Map.VisibleXStart) * Settings.ElemSize * scale),
         Convert.ToInt32((gameObj.Map.VisibleYFinish - gameObj.Map.VisibleYStart) * Settings.ElemSize * scale));
   RepaintConstImage = true;
 }
 /// <summary>
 /// Shows cost for tower upgrading
 /// </summary>
 /// <param name="gameObj">The game obj.</param>
 //TODO: remove magic
 private void ShowUpgradeCost(Game gameObj)
 {
   if (!gameObj.Towers[gameObj.TowerMapSelectedID].CanUpgrade) return;
   _graphObject.DrawString(
     "Upgrade cost: " + gameObj.Towers[gameObj.TowerMapSelectedID].GetUpgradeCost,
     new Font("Arial", Settings.ElemSize * gameObj.Scaling,
       FontStyle.Italic | FontStyle.Bold),
     new SolidBrush(Color.Black),
     new Point(
       Convert.ToInt32((Settings.MapAreaSize + Settings.DeltaX * 2) * gameObj.Scaling) + 3,
       gameObj.GetUpgradeButtonPos.Y - Convert.ToInt32(25 * gameObj.Scaling)));
 }
 /// <summary>
 /// Shows the money.
 /// </summary>
 /// <param name="gameObj">The game obj.</param>
 private void ShowMoney(Game gameObj)
 {
   //Money pict
   _graphObject.DrawImage(Res.MoneyPict,
     Convert.ToInt32((Settings.BreakupLineXPosition + Settings.DeltaX) * gameObj.Scaling),
     Convert.ToInt32(Settings.MoneyYPos * gameObj.Scaling),
     Convert.ToInt32(Res.MoneyPict.Width * gameObj.Scaling),
     Convert.ToInt32(Res.MoneyPict.Height * gameObj.Scaling));
   //Number of gold
   _graphObject.DrawString(gameObj.Gold.ToString(CultureInfo.InvariantCulture),
     new Font("Arial", 14 * gameObj.Scaling),
     new SolidBrush(Color.Black),
     new Point(
       Convert.ToInt32((Settings.BreakupLineXPosition + Res.MoneyPict.Width + Settings.DeltaX) * gameObj.Scaling),
       Convert.ToInt32(Settings.DeltaY * gameObj.Scaling)));
 }
    //This region have so small methods, thats for future development
    #region Information for player

    /// <summary>
    /// Shows the lives.
    /// </summary>
    /// <param name="gameObj">The game obj.</param>
    private void ShowLives(Game gameObj)
    {
      _graphObject.DrawString(
        "Lives: " + gameObj.NumberOfLives.ToString(CultureInfo.InvariantCulture),
        new Font("Arial", Settings.FontSize * gameObj.Scaling),
        new SolidBrush(Color.Black),
        new Point(
          Convert.ToInt32((Settings.BreakupLineXPosition + Settings.DeltaX) * gameObj.Scaling),
          Convert.ToInt32((Res.MoneyPict.Height + Settings.DeltaX) * gameObj.Scaling)));
    }
 /// <summary>
 /// Shows the square and circle at tower.
 /// </summary>
 /// <param name="gameObj">The game obj.</param>
 /// <param name="position">The position.</param>
 /// <param name="radius">The radius.</param>
 /// <param name="circleColor">Color of the circle.</param>
 private void ShowSquareAndCircleAtTower(Game gameObj, Point position, int radius, Color circleColor)
 {
   //Square
   _graphObject.DrawRectangle(new Pen(Color.Black),
       Convert.ToInt32(position.X * Settings.ElemSize * gameObj.Scaling + Settings.DeltaX),
       Convert.ToInt32(position.Y * Settings.ElemSize * gameObj.Scaling + Settings.DeltaY),
       Convert.ToInt32(Settings.ElemSize * 2 * gameObj.Scaling),
       Convert.ToInt32(Settings.ElemSize * 2 * gameObj.Scaling));
   //Circle
   //+1 for position centering
   _graphObject.DrawEllipse(
     new Pen(circleColor),
     ((position.X + 1) * Settings.ElemSize - radius) * gameObj.Scaling + Settings.DeltaX,
     ((position.Y + 1) * Settings.ElemSize - radius) * gameObj.Scaling + Settings.DeltaY,
     radius * 2 * gameObj.Scaling, radius * 2 * gameObj.Scaling);
 }
 /// <summary>
 /// Shows the tower params.
 /// </summary>
 /// <param name="gameObj">The game obj.</param>
 //TODO: remove magic
 private void ShowTowerParams(Game gameObj)
 {
   string strToShow = "";
   int xLeftPos = Convert.ToInt32((Settings.MapAreaSize + Settings.DeltaX * 2) * gameObj.Scaling) + 5;
   if (gameObj.TowerConfSelectedID != -1)//Information about tower, which player want to build
   {
     strToShow = gameObj.TowerParamsForBuilding[gameObj.TowerConfSelectedID]
       + gameObj.TowerParamsForBuilding[gameObj.TowerConfSelectedID].UpgradeParams[0].ToString();
     _graphObject.DrawString("Cost: " + gameObj.TowerParamsForBuilding[gameObj.TowerConfSelectedID].UpgradeParams[0].Cost,
       new Font("Arial", Settings.ElemSize * gameObj.Scaling, FontStyle.Italic | FontStyle.Bold), new SolidBrush(Color.Black),
       new Point(xLeftPos, Convert.ToInt32(390 * gameObj.Scaling)));
   }
   if (gameObj.TowerMapSelectedID != -1)//Information about tower, which player selected on the map
   {
     strToShow = gameObj.Towers[gameObj.TowerMapSelectedID].ToString();
     //Icon
     _graphObject.DrawImage(gameObj.Towers[gameObj.TowerMapSelectedID].Icon, xLeftPos, Convert.ToInt32(375 * gameObj.Scaling),
       Convert.ToInt32(gameObj.Towers[gameObj.TowerMapSelectedID].Icon.Width * gameObj.Scaling),
        Convert.ToInt32(gameObj.Towers[gameObj.TowerMapSelectedID].Icon.Height * gameObj.Scaling));
     //Current tower level
     _graphObject.DrawString("Level: " + gameObj.Towers[gameObj.TowerMapSelectedID].Level.ToString(CultureInfo.InvariantCulture),
       new Font("Arial", Settings.ElemSize * gameObj.Scaling, FontStyle.Italic | FontStyle.Bold), new SolidBrush(Color.Black),
       new Point(Convert.ToInt32((Settings.MapAreaSize + gameObj.Towers[gameObj.TowerMapSelectedID].Icon.Width + Settings.DeltaX * 2) * gameObj.Scaling) + 5,
         Convert.ToInt32((375 + gameObj.Towers[gameObj.TowerMapSelectedID].Icon.Height / 2) * gameObj.Scaling)));
     //Upgrading cost
     ShowUpgradeCost(gameObj);
   }
   //Parametrs
   _graphObject.DrawString(strToShow,
       new Font("Arial", 10 * gameObj.Scaling, FontStyle.Italic | FontStyle.Bold), new SolidBrush(Color.Black),
       new Point(xLeftPos, Convert.ToInt32(415 * gameObj.Scaling)));
   //Border line
   _graphObject.DrawRectangle(new Pen(Color.Black), xLeftPos,
     Convert.ToInt32(415 * gameObj.Scaling), Convert.ToInt32((200 - Settings.DeltaX * 2) * gameObj.Scaling),
     Convert.ToInt32((184) * gameObj.Scaling));
 }
    /// <summary>
    /// Renders map area
    /// </summary>
    /// <param name="gameObj">The game obj.</param>
    private void MapAreaShowing(Game gameObj)
    {
      //If needs to change constant map image
      if (RepaintConstImage)
      {
        gameObj.Map.GetConstantBitmap(_constantMapImage, Convert.ToInt32(Settings.MapAreaSize * gameObj.Scaling),
                                      Convert.ToInt32(Settings.MapAreaSize * gameObj.Scaling));
        RepaintConstImage = false;
        //Memory leak fix
        GC.Collect();
      }
      //Limitation of the area for drawing
      _graphObject.Clip = new Region(new Rectangle(Settings.DeltaX, Settings.DeltaY,
                                            Convert.ToInt32((gameObj.Map.VisibleXFinish - gameObj.Map.VisibleXStart) *
                                                            Settings.ElemSize * gameObj.Scaling),
                                            Convert.ToInt32((gameObj.Map.VisibleYFinish - gameObj.Map.VisibleYStart) *
                                                            Settings.ElemSize * gameObj.Scaling)));
      //Map showing
      _graphObject.DrawImage(_constantMapImage, Settings.DeltaX, Settings.DeltaY, _constantMapImage.Width,
                      _constantMapImage.Height);
      Point visibleStart = new Point(gameObj.Map.VisibleXStart, gameObj.Map.VisibleYStart);
      Point visibleFinish = new Point(gameObj.Map.VisibleXFinish, gameObj.Map.VisibleYFinish);
      //Towers showing
      foreach (Tower tower in gameObj.Towers)
        tower.ShowTower(_graphObject, visibleStart, visibleFinish);
      //Monsters showing
      foreach (Monster monster in gameObj.Monsters.Where(monster => !monster.DestroyMe))
        monster.ShowMonster(_graphObject, visibleStart, visibleFinish);

      #region Showing square and circle around selected tower or around the tower, which player want to stand

      if (gameObj.ArrayPosForTowerStanding.X != -1)
      {
        ShowSquareAndCircleAtTower(gameObj,
          gameObj.ArrayPosForTowerStanding,
          gameObj.TowerParamsForBuilding[gameObj.TowerConfSelectedID].UpgradeParams[0].AttackRadius,
          gameObj.Check(gameObj.ArrayPosForTowerStanding) ? Color.White : Color.Red);
      }
      else if (gameObj.TowerMapSelectedID != -1)
      {
        ShowSquareAndCircleAtTower(gameObj,
          new Point(gameObj.Towers[gameObj.TowerMapSelectedID].ArrayPos.X - gameObj.Map.VisibleXStart,
          gameObj.Towers[gameObj.TowerMapSelectedID].ArrayPos.Y - gameObj.Map.VisibleYStart),
          gameObj.Towers[gameObj.TowerMapSelectedID].CurrentTowerParams.AttackRadius, Color.White);
      }

      #endregion Showing square and circle around selected tower or around the tower, which player want to stand

      //Missle showing
      foreach (Missle missle in gameObj.Missels.Where(missle => !missle.DestroyMe))
        missle.Show(_graphObject, visibleStart, visibleFinish, gameObj.Monsters);
      _graphObject.Clip = new Region();
    }
 /// <summary>
 /// Factories the game objects
 /// </summary>
 /// <param name="filename">The filename of game configuration or save file</param>
 /// <param name="act">What factory should to do: Create or Load</param>
 /// <param name="graphicObject">IGraphic object </param>
 /// <returns>Game object or null if error</returns>
 public static Game Factory(string filename, FactoryAct act, IGraphic graphicObject)
 {
   Game result;
   try
   {
     switch (act)
     {
       case FactoryAct.Create:
         result = new Game(filename, graphicObject);
         break;
       case FactoryAct.Load:
         using (BinaryReader loadGameInfo = new BinaryReader(new FileStream(Environment.CurrentDirectory + "\\Data\\SavedGames\\" + filename + ".tdsg", FileMode.Open, FileAccess.Read)))
         {
           result = new Game(loadGameInfo.ReadString(), graphicObject);
           result.Load(loadGameInfo);
         }
         break;
       default:
         throw new ArgumentOutOfRangeException("act");
     }
   }
   catch (Exception exc)
   {
     MessageBox.Show("Game files damadged: " + exc.Message + "\n" + exc.StackTrace, "Fatal error");
     throw;
   }
   return result;
 }
 protected override void OnUpdateFrame(FrameEventArgs e)
 {
   base.OnUpdateFrame(e);
   if(_game == null)
   {
     return;
   }
   _game.Tick(new Point(this.Mouse.X, this.Mouse.Y));
   if(!_game.Lose && !_game.Won)
   {
     return;
   }
   MessageBox.Show(_game.Lose
                     ? GameCoClassLibrary.Properties.Resources.Looser_message
                     : GameCoClassLibrary.Properties.Resources.Winner_message);
   float scaling = _game.Scaling;
   _game = null;
   _gameMenu = new MainMenu(_graphObject);
   GameResize(scaling);
 }