mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 12:57:09 +01:00
47 lines
1.8 KiB
C#
47 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Testing : MonoBehaviour {
|
|
|
|
private Pathfinding pathfinding;
|
|
int originX = 0;
|
|
int originY = 0;
|
|
|
|
private void Start() {
|
|
pathfinding = new Pathfinding(20, 10);
|
|
}
|
|
|
|
private void Update() {
|
|
if (Input.GetMouseButtonDown(0)) {
|
|
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
|
List<PathNode> path = pathfinding.FindPath(originX, originY, x, y);
|
|
if (path != null) {
|
|
float cellSize = pathfinding.GetGrid().GetCellSize();
|
|
for (int i=0; i<path.Count - 1; i++) {
|
|
Debug.DrawLine(new Vector3(path[i].x, path[i].y) * cellSize + Vector3.one * cellSize/2, new Vector3(path[i+1].x, path[i+1].y) * cellSize + Vector3.one * cellSize/2, Color.green, 5f);
|
|
}
|
|
}
|
|
|
|
//characterPathfinding.SetTargetPosition(mouseWorldPosition);
|
|
}
|
|
|
|
if (Input.GetMouseButtonDown(1)) {
|
|
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
|
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
|
|
originX = x;
|
|
originY = y;
|
|
}
|
|
|
|
if (Input.GetMouseButtonDown(2))
|
|
{
|
|
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
|
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
|
|
}
|
|
}
|
|
|
|
}
|