public PathNode(int _index, int _parentIndex, float _g, float _h, MapNav mapNav) { index = _index; parentIndex = _parentIndex; g = _g; h = _h; f = g + h; grid = new MapGrid() { x = index % mapNav.gridXNum, z = index / mapNav.gridXNum }; position = mapNav.GetWorldPosition(grid); }
public void OnSceneGUI() { MapNav mapNav = Target; float y = 0.1f; if (curProcessType == ProcessType.None) { return; } if (curProcessType == ProcessType.Set || curProcessType == ProcessType.Clear) { Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float rayDistance; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 hitPoint = ray.GetPoint(rayDistance); if (hitPoint.x >= 0.0f && hitPoint.x <= mapNav.gridXNum * MapGrid.Width && hitPoint.z >= 0.0f && hitPoint.z <= mapNav.gridZNum * MapGrid.Height) { Handles.color = Color.black; float _radius = radius * 0.5f; Handles.DrawPolyLine(new Vector3[] { new Vector3(hitPoint.x - _radius * MapGrid.Width, y, hitPoint.z - _radius * MapGrid.Height), new Vector3(hitPoint.x - _radius * MapGrid.Width, y, hitPoint.z + _radius * MapGrid.Height), new Vector3(hitPoint.x + _radius * MapGrid.Width, y, hitPoint.z + _radius * MapGrid.Height), new Vector3(hitPoint.x + _radius * MapGrid.Width, y, hitPoint.z - _radius * MapGrid.Height), new Vector3(hitPoint.x - _radius * MapGrid.Width, y, hitPoint.z - _radius * MapGrid.Height) }); if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag) { var grid = new MapGrid(hitPoint); for (int _z = grid.z - Mathf.RoundToInt(_radius); _z <= grid.z + Mathf.RoundToInt(_radius); ++_z) { if (_z < 0) { continue; } if (_z > mapNav.gridZNum - 1) { continue; } for (int _x = grid.x - Mathf.RoundToInt(_radius); _x <= grid.x + Mathf.RoundToInt(_radius); ++_x) { if (_x < 0) { continue; } if (_x > mapNav.gridXNum - 1) { continue; } Vector3 position = mapNav.GetWorldPosition(new MapGrid() { x = _x, z = _z }); if (Mathf.Abs(position.x - hitPoint.x) <= _radius * MapGrid.Width && Mathf.Abs(position.z - hitPoint.z) <= _radius * MapGrid.Height) { switch (curProcessType) { case ProcessType.Set: mapNav[_x, _z] |= curTileType; break; case ProcessType.Clear: mapNav[_x, _z] &= ~curTileType; break; default: throw new System.NotImplementedException(); } } } } } } } } else if (curProcessType == ProcessType.FindPath) { if (Event.current.type == EventType.MouseDown) { Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float rayDistance; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 hitPoint = ray.GetPoint(rayDistance); if (hitPoint.x >= 0.0f && hitPoint.x <= mapNav.gridXNum * MapGrid.Width && hitPoint.z >= 0.0f && hitPoint.z <= mapNav.gridZNum * MapGrid.Height) { if (bSampleStart) { vecStart = hitPoint; } else { vecEnd = hitPoint; } bSampleStart = !bSampleStart; MapGrid gridEnd; if (mapNav.GetNearestValidGrid(new MapGrid(vecStart), new MapGrid(vecEnd), out gridEnd, TileType.TileType_Walk)) { path = mapNav.GetPath(new MapGrid(vecStart), gridEnd, TileType.TileType_Walk); } else { path.Clear(); } } } } Handles.color = Color.red; Handles.CubeCap(0, vecStart, new Quaternion(0, 0, 0, 1), 0.1f); Handles.color = Color.blue; Handles.CubeCap(0, vecEnd, new Quaternion(0, 0, 0, 1), 0.1f); if (path.Count > 0) { Handles.color = Color.black; Vector3 lastPoint = vecStart; lastPoint.y = y; foreach (MapGrid grid in path) { Vector3 curPoint = mapNav.GetWorldPosition(grid); curPoint.y = y; Handles.color = Color.black; Handles.DrawLine(lastPoint, curPoint); lastPoint = curPoint; Handles.color = Color.cyan; Handles.CubeCap(0, curPoint, new Quaternion(0, 0, 0, 1), 0.1f); } } } HandleUtility.Repaint(); switch (Event.current.type) { case EventType.MouseUp: case EventType.MouseDown: case EventType.MouseDrag: case EventType.MouseMove: Event.current.Use(); break; } }
public PathNode(int _index, int _parentIndex, float _g, float _h, MapNav mapNav) { index = _index; parentIndex = _parentIndex; g = _g; h = _h; f = g + h; grid = new MapGrid(){x = index % mapNav.gridXNum, z = index / mapNav.gridXNum}; position = mapNav.GetWorldPosition(grid); }