public Node(Node _parentNode, float _f, Vector2 _pos) { this._parentNode = _parentNode; this._f = _f; this._pos = _pos; }
// position sur les tiles isos public List<Vector2> GetPath(Vector2 _positionDepart, Vector2 _positionFinale) { Console.WriteLine("set path :"); _positionFinale.X = (int)_positionFinale.X; _positionFinale.Y = (int)_positionFinale.Y; //Console.WriteLine("Depart : " + _positionDepart); //Console.WriteLine("Arrivé : " + _positionFinale); Node _currentNode = new Node(null, GetF(_positionDepart, _positionFinale, _positionDepart, 0), _positionDepart); List<Node> _openList = new List<Node>(); List<Node> _closedList = new List<Node>(); bool _retour = false; // vaut vrai si la position a deja ete parcouru (evite de retourner en arriere) List<Vector2> _path = new List<Vector2>(); ; Vector2[] _adjacentNodePos = new Vector2[8]; _openList.Add(_currentNode); int x = 0; while (_currentNode.Pos != _positionFinale && !_retour) //for (int j = 0; j < 5; j++) { _adjacentNodePos[0] = new Vector2(_currentNode.Pos.X + 1, _currentNode.Pos.Y); _adjacentNodePos[1] = new Vector2(_currentNode.Pos.X - 1, _currentNode.Pos.Y); _adjacentNodePos[2] = new Vector2(_currentNode.Pos.X, _currentNode.Pos.Y + 1); _adjacentNodePos[3] = new Vector2(_currentNode.Pos.X, _currentNode.Pos.Y - 1); _adjacentNodePos[4] = new Vector2(_currentNode.Pos.X + 1, _currentNode.Pos.Y + 1); _adjacentNodePos[5] = new Vector2(_currentNode.Pos.X - 1, _currentNode.Pos.Y - 1); _adjacentNodePos[6] = new Vector2(_currentNode.Pos.X + 1, _currentNode.Pos.Y - 1); _adjacentNodePos[7] = new Vector2(_currentNode.Pos.X - 1, _currentNode.Pos.Y + 1); for (int i = 0; i < 8; i++) { //Console.WriteLine("X : " + (int)_adjacentNodePos[i].X + " Y : " + (int)_adjacentNodePos[i].Y + " coef : " + _collisionTableau[(int)_adjacentNodePos[i].X, (int)_adjacentNodePos[i].Y]); /*foreach(Node n in _closedList) if (_adjacentNodePos[i] == n.Pos) _retour = true; if (!_retour)*/ _openList.Add(new Node(_currentNode, GetF(_positionDepart, _positionFinale, _adjacentNodePos[i], _collisionTableau[(int)_adjacentNodePos[i].X, (int)_adjacentNodePos[i].Y]), _adjacentNodePos[i])); //new Node(_currentNode, GetF(_positionDepart, _positionFinale, _adjacentNodePos[i], _collisionTableau[(int)_adjacentNodePos[i].X, (int)_adjacentNodePos[i].Y]), _adjacentNodePos[i]).display(); } if (!isRetour(_openList[0], _closedList)) _closedList.Add(_openList[0]); else { Console.WriteLine("occurence de retour !"); _retour = true; }// on deplace le noeud parent dans la closedList Console.WriteLine("nouvelle position : " + _openList[0].Pos + ", sa value : " + _collisionTableau[(int)_openList[0].Pos.X, (int)_openList[0].Pos.Y]); _openList.RemoveAt(0); for (int i = 0; i < 7; i++) { if (_openList[0].F < _openList[1].F) // garde le f le plus petit _openList.RemoveAt(1); else _openList.RemoveAt(0); } _currentNode = _openList[0]; x++; } _closedList.Add(_openList[0]); foreach (Node n in _closedList) _path.Add(n.Pos); return _path; }
public bool isRetour(Node _newNode, List<Node> _closedList) { Console.WriteLine("test isRetour"); foreach (Node n in _closedList) { Console.WriteLine("newNode = " + _newNode.Pos + ", n = " + n.Pos); if (n.Pos == _newNode.Pos) return true; } return false; }