Esempio n. 1
0
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mouseWorldPos = UtilitsClass.GetMouseWorldPosition(background.position.z);
            pathfinding.GetGrid().GetXY(characterPathfinding.GetPosition(), out int startX, out int startY);
            pathfinding.GetGrid().GetXY(mouseWorldPos, out int endX, out int endY);
            List <PathNode> path = pathfinding.FindPath(startX, startY, endX, endY);
            if (path != null)
            {
                Debug.Log("Path exist");
                for (int i = 0; i < path.Count - 1; i++)
                {
                    Debug.DrawLine(new Vector3(path[i].x, path[i].y) * 10f + Vector3.one * 5f, new Vector3(path[i + 1].x, path[i + 1].y) * 10f + Vector3.one * 5f, Color.green, 2.5f);
                }
            }
            characterPathfinding.SetTargetPosition(mouseWorldPos);
        }

        if (Input.GetMouseButtonDown(1))
        {
            Vector3 mouseWorldPos = UtilitsClass.GetMouseWorldPosition(background.position.z);
            pathfinding.GetGrid().GetXY(mouseWorldPos, out int x, out int y);
            PathNode pathNode = pathfinding.GetGrid().GetGridObject(x, y);
            if (pathNode != default && pathNode.isWalkable)
            {
                obstacle.CreateObstacle(pathfinding, mouseWorldPos);
            }
        }
    }
Esempio n. 2
0
    public void ShowGrid()
    {
        TextMesh[,] debugTextArray = new TextMesh[width, height];
        GameObject parent = new GameObject("GridText");

        for (int x = 0; x < gridArray.GetLength(0); x++)
        {
            for (int y = 0; y < gridArray.GetLength(1); y++)
            {
                debugTextArray[x, y] = UtilitsClass.CreateWorldText(gridArray[x, y]?.ToString(), parent.transform, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * 0.5f);
                CreateGridLine(x, y);
            }
        }
        Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
        Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);

        onGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) => debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString();
    }