private void GetClosestObstacle() { ModelGame mg = (ModelGame)model; int playerX = mg.player.Location.X; int playerY = mg.player.Location.Y; double difference = 2000; // We maken een array aan zodat we door alle objecten kunnen loopen en ze met elkaar kunnen vergelijken List<GameObject> comparisonArray = new List<GameObject>(mg.GameObjects); foreach (GameObject gameObject in comparisonArray) { if (gameObject is Obstacle) { int obstacleX = gameObject.Location.X; int obstacleY = gameObject.Location.Y; int deltaX = playerX - obstacleX; int deltaY = playerY - obstacleY; if (deltaX < 0) { deltaX *= -1; } if (deltaY < 0) { deltaY *= -1; } int sum = (deltaX * deltaX) + (deltaY * deltaY); double result = Math.Sqrt(sum); if (result < difference) { nextClosestObstacle = closestObstacle; closestObstacle = (Obstacle)gameObject; difference = result; } } } }
private void UpdateObstacleLabels(Obstacle obstacle1, Obstacle obstacle2) { ModelGame mg = (ModelGame)model; if (closestObstacle != null && nextClosestObstacle != null && mg.obstaclePanel != null) { mg.lblObstaclePosX1.Text = obstacle1.Location.X.ToString(); mg.lblObstaclePosX2.Text = obstacle2.Location.X.ToString(); mg.lblObstaclePosY1.Text = obstacle1.Location.Y.ToString(); mg.lblObstaclePosY2.Text = obstacle2.Location.Y.ToString(); mg.lblObstacleDesc1.Text = obstacle1.Description; mg.lblObstacleDesc2.Text = obstacle2.Description; mg.lblObstacleName1.Text = obstacle1.Name; mg.lblObstacleName2.Text = obstacle2.Name; mg.pbObstacle1.BackgroundImage = obstacle1.PanelIcon; mg.pbObstacle2.BackgroundImage = obstacle2.PanelIcon; } }