예제 #1
0
        //Use mouse to draw some obstacles
        private void DrawObstacles()
        {
            if (pathfindingManager.visualMode != PathfindingManager.VisualMode.Gizmo)
            {
                float   spacing       = pathfindingManager.gridScale * pathfindingManager.gridSpacing;
                Vector3 mousePosition = GetMousePositionOnXZPlane();
                mousePosition.x = Mathf.Round(mousePosition.x / spacing) * spacing;
                mousePosition.y = Mathf.Round(mousePosition.y / spacing) * spacing;

                Matrix4x4 trs = Matrix4x4.TRS(mousePosition, Quaternion.identity, new Vector3(spacing, spacing, spacing));

                Graphics.DrawMesh(pathfindingManager.obstacleMesh, trs, cursorMaterial, 0);

                trs.m03 = pathfindingManager.start.x * spacing;
                trs.m13 = pathfindingManager.start.y * spacing;
                Graphics.DrawMesh(pathfindingManager.obstacleMesh, trs, startPointMaterial, 0);

                trs.m03 = pathfindingManager.end.x * spacing;
                trs.m13 = pathfindingManager.end.y * spacing;
                Graphics.DrawMesh(pathfindingManager.obstacleMesh, trs, endPointMaterial, 0);


                if (Input.GetMouseButton(0)) //LMB draw
                {
                    if (mousePosition == lastMousePosition)
                    {
                        return;
                    }
                    lastMousePosition = mousePosition;

                    mousePosition /= spacing;
                    pathfindingManager.SetObstacle(new int2((int)mousePosition.x, (int)mousePosition.y));
                    pathfindingManager.UpdateMatrices();
                }

                if (Input.GetMouseButton(1)) //RMB erase
                {
                    if (mousePosition == lastMousePosition)
                    {
                        return;
                    }
                    lastMousePosition = mousePosition;

                    mousePosition /= spacing;
                    pathfindingManager.SetNodeWalkable(new int2((int)mousePosition.x, (int)mousePosition.y));
                    pathfindingManager.UpdateMatrices();
                }

                //Set start&end nodes for custom path.
                if (Input.GetMouseButtonDown(2))
                {
                    lastMousePosition = mousePosition;

                    mousePosition /= spacing;
                    pathfindingManager.SetNodeWalkable(new int2((int)mousePosition.x, (int)mousePosition.y));

                    if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                    {
                        pathfindingManager.start = new Vector2Int((int)mousePosition.x, (int)mousePosition.y);
                    }
                    else
                    {
                        pathfindingManager.end = new Vector2Int((int)mousePosition.x, (int)mousePosition.y);
                    }

                    pathfindingManager.UpdateMatrices();
                }

                //Use start&end node to create custom path.
                if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
                {
                    pathfindingManager.AddPathManually();
                }

                //Clear pathfinding and obstacles map.
                if (Input.GetKeyDown(KeyCode.Delete))
                {
                    if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                    {
                        pathfindingManager.ClearObstaclesMap();
                        pathfindingManager.ClearPathfinders();
                    }
                    else
                    {
                        pathfindingManager.ClearPathfinders();
                    }
                }

                //Generate obstacles with perlin noise.
                if (Input.GetKeyDown(KeyCode.F2))
                {
                    pathfindingManager.GenerateObstaclesWithPerlinNoise();
                }

                //Save
                if (Input.GetKeyDown(KeyCode.F5))
                {
                    pathfindingManager.SaveToFile();
                } //Load
                else if (Input.GetKeyDown(KeyCode.F9))
                {
                    pathfindingManager.LoadFromFile();
                }
            }
        }