private void OnDrawGizmos() { if (grid == null) { return; } var _nodes = grid._nodes; for (int i = 0; i < _nodes.Count; i++) { DJ_Node _node = _nodes[i]; Vector3 _position = _node.Position; /*var _neighbour = _node.Neighbours; * for (int j = 0; j < _neighbour.elements; j++) * { * Gizmos.color = Color.red; * Gizmos.DrawLine(_position, _neighbour[j].Position); * } * Handles.Label(_position + Vector3.up, $"Node : {i}"); * Handles.DrawDottedLine(_position, _position + Vector3.up, 0.1f);*/ Gizmos.color = Color.yellow; Gizmos.DrawSphere(_nodes[i].Position, 0.1f); } Gizmos.color = Color.white; if (path != null) { for (int i = 1; i < path.Count; i++) { Gizmos.color = Color.green; Gizmos.DrawLine(path[i - 1].Position, path[i].Position); } } }
public void Compute(int _id, int _target = -1) { previous.Clear(); list.Clear(); distance.Clear(); deleted.Clear(); for (int i = 0; i < vertices.Count; i++) { DJ_Node _node = vertices[i]; if (_node.Enabled) { distance.Add(i, Mathf.Infinity); previous.Add(i, null); list.Add(i, vertices[i]); } } distance[_id] = 0; while (list.Count != 0) { int _vertex = FindMin(); if (_vertex == -1) { return; } if (_target != -1) { if (_target == _vertex) { return; } } list.Remove(_vertex); DJ_Node _node = vertices[_vertex]; List <DJ_Node> _neighbours = _node.Neighbours; for (int i = 0; i < _neighbours.Count; i++) { DJ_Node _neighbour = _neighbours[i]; int _neightbourId = _neighbour.Id; if (list.ContainsKey(_neightbourId)) { float _weight = distance[_vertex] + Vector3.Distance(_node.Position, _neighbour.Position); //DISTANCE TO NEIGHBOUR SHOULD BE MODIFIED WHEN WORKING if (_weight < distance[_neightbourId]) { distance[_neightbourId] = _weight; previous[_neightbourId] = _node; } } } } }
public DJ_Grid(int _length, int _depth, Transform _container) { for (int y = 0; y < _depth + 1; y++) { int _yIndex = y * (_depth + 1); for (int x = 0; x < _length + 1; x++) { List <DJ_Node> _neighbours = new List <DJ_Node>(); if (x != 0) { _neighbours.Add(_nodes[(x - 1) + _yIndex]); } if (y != 0) { _neighbours.Add(_nodes[(x) + (y - 1) * (_depth + 1)]); if (x != _depth) { _neighbours.Add(_nodes[(x + 1) + (y - 1) * (_depth + 1)]); } if (x != 0) { _neighbours.Add(_nodes[(x - 1) + (y - 1) * (_depth + 1)]); } } GameObject _nodeObject = new GameObject(); DJ_Node _node = _nodeObject.AddComponent <DJ_Node>(); if (_container) { _node.transform.parent = _container; } _node.Init(new Vector3(x, 0, y), _neighbours, x + _yIndex); if (x != 0) { _nodes[(x - 1) + _yIndex].AddNeighbor(_node); } if (y != 0) { _nodes[(x) + (y - 1) * (_depth + 1)].AddNeighbor(_node); if (x != _depth) { _nodes[(x + 1) + (y - 1) * (_depth + 1)].AddNeighbor(_node); } if (x != 0) { _nodes[(x - 1) + (y - 1) * (_depth + 1)].AddNeighbor(_node); } } _nodes.Add(_node); } } }
public List <DJ_Node> GetPath(int _target) { DJ_Node _pathNode = vertices[_target]; if (!previous.ContainsKey(_pathNode.Id)) { return(null); } path.Clear(); while (_pathNode != null) { path.Insert(0, _pathNode); _pathNode = previous[_pathNode.Id]; } return(path); }
void MapVsStrechy() { System.Diagnostics.Stopwatch _clock = new System.Diagnostics.Stopwatch(); _clock.Start(); List <DJ_Node> _nodes = new List <DJ_Node>(); DJ_Node _test = gameObject.AddComponent <DJ_Node>(); _test.Init(Vector3.zero, null, 10); _nodes.Add(_test); _nodes.Add(gameObject.AddComponent <DJ_Node>()); if (_nodes.Contains(_test)) { } _clock.Stop(); TimeSpan ts = _clock.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Debug.Log($"{ts} -- Streachy buffer time"); _clock = new System.Diagnostics.Stopwatch(); _clock.Start(); Dictionary <int, DJ_Node> _mapNode = new Dictionary <int, DJ_Node>(); _test = gameObject.AddComponent <DJ_Node>(); _test.Init(Vector3.zero, null, 10); _mapNode.Add(0, _test); _mapNode.Add(1, gameObject.AddComponent <DJ_Node>()); if (_mapNode.ContainsKey(_test.Id)) { } _clock.Stop(); ts = _clock.Elapsed; elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Debug.Log($"{ts} -- Dictionarry time"); }
private void UpdateNodes() { if (path.Grid == null) { return; } for (int i = 0; i < nodes.Count; i++) { DJ_Node _node = nodes[i]; _position = transform.InverseTransformPoint(_node.Position); float _nodeX = _position.x, _nodeY = _position.y, _nodeZ = _position.z; float _scale = 0.5f; float _scaleX = _scale * (1 / transform.localScale.x); float _scaleY = _scale * (1 / transform.localScale.y); float _scaleZ = _scale * (1 / transform.localScale.z); _nodeX = _nodeX < 0 ? _nodeX += _scaleX : _nodeX -= _scaleX; _nodeY = _nodeY < 0 ? _nodeY += _scaleY : _nodeY -= _scaleY; _nodeZ = _nodeZ < 0 ? _nodeZ += _scaleZ : _nodeZ -= _scaleZ; float _xCheck = 0.5f; float _yCheck = 0.5f; float _zCheck = 0.5f; bool _contains = visited.ContainsValue(_node); if (_nodeX < _xCheck && _nodeX > -_xCheck && _nodeZ < _yCheck && _nodeZ > -_yCheck && _nodeY < _zCheck && _nodeY > -_zCheck) { _node.Enabled = false; if (!_contains) { visited.Add(_node.Id, _node); } } else if (_contains) { _node.Enabled = true; visited.Remove(_node.Id); } } }
public List <DJ_Node> GetPath(DJ_Node _target) => GetPath(_target.Id);
public void AddNeighbor(DJ_Node _node) { Neighbours.Add(_node); }