public Agent(Game1 game, Texture2D texture, String name, Color color, Position startPosition, Position goalPosition)
 {
     this.game = game;
     this.texture = texture;
     this.name = name;
     this.color = color;
     this.startPosition = startPosition;
     this.goalPosition = goalPosition;
     this.speed = 2;
 }
 public bool Intersect(Position position)
 {
     double centerX1 = this.leftUpCorner.X + ((this.size.X) / 2);
     double centerY1 = this.leftUpCorner.Y + ((this.size.Y) / 2);
     double centerX2 = position.leftUpCorner.X + ((position.size.X) / 2);
     double centerY2 = position.leftUpCorner.Y + ((position.size.Y) / 2);
     if ((Math.Abs(centerX1 - centerX2) < (Math.Abs(this.size.X + position.size.X) / 2)) && ((Math.Abs(centerY1 - centerY2) < (Math.Abs(this.size.Y + position.size.Y) / 2))))
         return true;
     else
         return false;
 }
 public void move()
 {
     if (position.isSamePosition(goalPosition))
     {
         System.Threading.Thread.Sleep(2000);
         this.startPositionLocated = false;
         this.goalPositionLocated = false;
         isMove = false;
         return;
     }
     if (stations.Count != 0)
     {
         setDirection();
         this.position.leftUpCorner += direction * speed;
         if ((Math.Abs(position.leftUpCorner.X - stations.ElementAt(0).leftUpCorner.X) <= (Math.Abs(direction.X * speed)) && (Math.Abs(position.leftUpCorner.Y - stations.ElementAt(0).leftUpCorner.Y) <= (Math.Abs(direction.Y * speed)))))
         {
             position = stations.ElementAt(0);
             stations.Dequeue();
         }
     }
 }
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
            else if (Keyboard.GetState().IsKeyDown(Keys.PrintScreen))
                screenShot();

            if (agent1.isMove)
            {
                agent1.move();
            }
            else
            {
                roadmap.clearDijikstraRecords();
                mauseState = Mouse.GetState();
                if (!agent1.startPositionLocated)
                {
                    // Baþlangýç konumu belirlenmediyse burada seçtirilecek.
                    if (mauseState.LeftButton == ButtonState.Pressed)
                    {
                        mausePosition = new Position(new Vector2(mauseState.X, mauseState.Y), new Vector2(agent1.startPosition.size.X, agent1.startPosition.size.Y));
                        if (mausePosition.isOnObstacles(gameMap))
                            return;
                        agent1.startPosition = mausePosition;
                        agent1.startPositionLocated = true;
                    }
                }
                else if (agent1.startPositionLocated && !agent1.goalPositionLocated)
                {
                    if (mauseState.RightButton == ButtonState.Pressed)
                    {
                        mausePosition = new Position(new Vector2(mauseState.X, mauseState.Y), new Vector2(agent1.startPosition.size.X, agent1.startPosition.size.Y));
                        if (mausePosition.isOnObstacles(gameMap))
                            return;
                        agent1.goalPosition = mausePosition;
                        agent1.goalPositionLocated = true;

                        agent1.position = new Position(agent1.startPosition.leftUpCorner, agent1.startPosition.size);
                        if (roadmap.findAgentsRoad() == true)
                            agent1.isMove = true;
                        else
                        {
                            agent1.startPositionLocated = false;
                            agent1.goalPositionLocated = false;
                        }
                    }
                }
            }
            base.Update(gameTime);
        }
 protected override void LoadContent()
 {
     spriteBatch = new SpriteBatch(GraphicsDevice);
     obstacleTexture = Content.Load<Texture2D>("obstacleImage");
     Texture2D tempTexture = Content.Load<Texture2D>("ball");
     goalPositionImageTexture = Content.Load<Texture2D>("goalPositionImage");
     Position agent1StartPosition = new Position(new Vector2(10, 10), new Vector2(tempTexture.Width, tempTexture.Height));
     Position agent1FinishPosition = new Position(new Vector2(800, 500), new Vector2(tempTexture.Width, tempTexture.Height));
     agent1 = new Agent(this, tempTexture, "Agent1", Color.DarkBlue, agent1StartPosition, agent1FinishPosition);
     roadmap = new Roadmap(gameMap, agent1, sampleSize, depth);
 }
        private Position getFreePosition()
        {
            int displayWidth = agent.game.graphics.PreferredBackBufferWidth;
            int displayHeight = agent.game.graphics.PreferredBackBufferHeight;

            bool continueSearch = true;
            Position position;
            do
            {
                position = new Position(new Vector2(rand.Next(0, displayWidth - agent.texture.Width), rand.Next(0, displayHeight - agent.texture.Height)), new Vector2(agent.texture.Width, agent.texture.Height));
                continueSearch = false;
                foreach (Obstacle obs in map.obstacleList)
                {
                    if (position.Intersect(obs.position))
                    {
                        continueSearch = true;
                        break;
                    }
                }
            } while (continueSearch);
            return position;
        }
        public void setMap(string name)
        {
            Obstacle obstacle1;
            Position p;
            switch (name)
            {
                case "Map1":
                    p = new Position(new Vector2(80, 30), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(200, 400), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(500, 500), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(400, 150), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(400, 250), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(550, 350), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(150, 200), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    break;
                case "Map2":
                    p = new Position(new Vector2(80, 80), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.DarkRed, p);
                    obstacleList.Add(obstacle1);
                    break;
                case "Map3":
                    p = new Position(new Vector2(150, 200), new Vector2(100, 60));
                    obstacle1 = new Obstacle(game, Color.Yellow, p);
                    obstacleList.Add(obstacle1);
                    break;
                case "Map4":
                    p = new Position(new Vector2(675, 20), new Vector2(10, 300));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(675, 450), new Vector2(10, 300));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(20, 375), new Vector2(585, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(750, 375), new Vector2(600, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    break;
                case "Map5":
                    p = new Position(new Vector2(0, 80), new Vector2(40, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(150, 80), new Vector2(1400, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(0, 200), new Vector2(500, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(600, 200), new Vector2(800, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(0, 320), new Vector2(10, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(150, 320), new Vector2(1000, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(50, 350), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(150, 400), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(250, 350), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(350, 400), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(450, 350), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(550, 400), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(650, 350), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(750, 400), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(850, 350), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(950, 400), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(1050, 350), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(1150, 400), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    p = new Position(new Vector2(1250, 350), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    break;
                case "Map6":
                    //Obstacle1
                    p = new Position(new Vector2(0, 80), new Vector2(120, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle2
                    p = new Position(new Vector2(120, 80), new Vector2(10, 100));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle3
                    p = new Position(new Vector2(80, 180), new Vector2(50, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle4
                    p = new Position(new Vector2(300, 80), new Vector2(10, 250));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle5
                    p = new Position(new Vector2(120, 330), new Vector2(190, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle6
                    p = new Position(new Vector2(450, 0), new Vector2(10, 150));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle7
                    p = new Position(new Vector2(450, 150), new Vector2(450, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle8
                    p = new Position(new Vector2(550, 150), new Vector2(10, 150));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle9
                    p = new Position(new Vector2(550, 300), new Vector2(200, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle10
                    p = new Position(new Vector2(425, 300), new Vector2(10, 200));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle11
                    p = new Position(new Vector2(600, 425), new Vector2(300, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle12
                    p = new Position(new Vector2(900, 425), new Vector2(10, 275));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle13
                    p = new Position(new Vector2(600, 700), new Vector2(310, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle14
                    p = new Position(new Vector2(600, 520), new Vector2(10, 180));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle15
                    p = new Position(new Vector2(600, 520), new Vector2(200, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle16
                    p = new Position(new Vector2(800, 520), new Vector2(10, 100));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle17
                    p = new Position(new Vector2(700, 620), new Vector2(110, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle18
                    p = new Position(new Vector2(1000, 80), new Vector2(10, 590));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle19
                    p = new Position(new Vector2(1000, 100), new Vector2(250, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle20
                    p = new Position(new Vector2(1150, 250), new Vector2(250, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle21
                    p = new Position(new Vector2(1000, 400), new Vector2(250, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle22
                    p = new Position(new Vector2(1150, 650), new Vector2(250, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle23
                    p = new Position(new Vector2(70, 700), new Vector2(100, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle24
                    p = new Position(new Vector2(100, 600), new Vector2(150, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    break;
                case "Map7":
                    //Obstacle1
                    p = new Position(new Vector2(50, 50), new Vector2(1250, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle2
                    p = new Position(new Vector2(1300, 50), new Vector2(10, 650));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle3
                    p = new Position(new Vector2(90, 700), new Vector2(1220, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle4
                    p = new Position(new Vector2(90, 120), new Vector2(10, 580));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);

                    //Obstacle5
                    p = new Position(new Vector2(90, 120), new Vector2(1150, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle6
                    p = new Position(new Vector2(1240, 120), new Vector2(10, 500));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle6
                    p = new Position(new Vector2(170, 620), new Vector2(1080, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle7
                    p = new Position(new Vector2(170, 200), new Vector2(10, 420));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);

                    //Obstacle8
                    p = new Position(new Vector2(170, 200), new Vector2(1000, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle9
                    p = new Position(new Vector2(1170, 200), new Vector2(10, 350));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle10
                    p = new Position(new Vector2(250, 550), new Vector2(930, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle11
                    p = new Position(new Vector2(250, 280), new Vector2(10, 270));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);

                    //Obstacle12
                    p = new Position(new Vector2(250, 280), new Vector2(850, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle13
                    p = new Position(new Vector2(1100, 280), new Vector2(10, 200));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle14
                    p = new Position(new Vector2(330, 480), new Vector2(780, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle15
                    p = new Position(new Vector2(330, 360), new Vector2(10, 120));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);

                    //Obstacle16
                    p = new Position(new Vector2(330, 360), new Vector2(700, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle17
                    p = new Position(new Vector2(1030, 360), new Vector2(10, 70));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    //Obstacle18
                    p = new Position(new Vector2(400, 430), new Vector2(640, 10));
                    obstacle1 = new Obstacle(game, Color.Red, p);
                    obstacleList.Add(obstacle1);
                    break;

            }
        }
 public Obstacle(Game1 game, Color color, Position position)
 {
     this.game = game;
     this.color = color;
     this.position = position;
 }
 /// <summary>
 /// Seçilmiş harita içerisinde herhangi bu pozisyona bir serbest bir yolun olup olmadığını hesaplar.
 /// </summary>
 /// <param name="position"></param>
 /// <param name="map"></param>
 /// <returns></returns>
 public bool isConnectable(Position position, Map map)
 {
     Position virtualPosition = new Position(this.leftUpCorner, this.size);
     Vector2 direction;
     bool XisSame = false;
     bool YisSame = false;
     while (true)
     {
         direction = position.leftUpCorner - virtualPosition.leftUpCorner;  // Yön belirlenir.
         direction.Normalize();
         virtualPosition.leftUpCorner += direction;
         if (float.IsNaN(virtualPosition.leftUpCorner.X) || float.IsNaN(virtualPosition.leftUpCorner.Y))
             return false;
         foreach (Obstacle obs in map.obstacleList)
         {
             if (virtualPosition.Intersect(obs.position))
                 return false;
         }
         if ((Math.Abs(virtualPosition.leftUpCorner.X - position.leftUpCorner.X) <= (Math.Abs(direction.X * 2))) && !XisSame )
         {
             XisSame = true;
             virtualPosition.leftUpCorner.X = position.leftUpCorner.X;
         }
         if ((Math.Abs(virtualPosition.leftUpCorner.Y - position.leftUpCorner.Y) <= (Math.Abs(direction.Y * 2))) && !YisSame )
         {
             YisSame = true;
             virtualPosition.leftUpCorner.Y = position.leftUpCorner.Y;
         }
         if (XisSame && YisSame)
             return true;
     }
 }
 public bool isSamePosition(Position position)
 {
     if (this.leftUpCorner == position.leftUpCorner)
         return true;
     return false;
 }