public void setObstacles() { GameObject[] obstacles = GameObject.FindGameObjectsWithTag("Obstacle"); foreach (GameObject obs in obstacles) { grid.GetXY(IsoMatrix.InvIso(obs.transform.position), out int x, out int y); grid.GetGridObject(x, y).isWalkable = false; } }
public bool haspath = false; //gibt an ob der player gerade wohin moved //sucht einen Pfad von der aktuellen position zu target public void setTarget(Vector3 target) { pathIndex = 0; path = Pathfinding.Instance.FindPathV3(IsoMatrix.InvIso(this.GetPosition()), IsoMatrix.InvIso(target)); if(path != null && path.Count > 1) { path.RemoveAt(0); //entfernt den ersten path da dieser seine eigene position ist haspath = true; Debug.Log("Path set to length: " + path.Count); } else { Debug.Log("Player has no valid path"); } }
public bool debug = true; //aktiviert debug ausgabe public MyGrid(int width, int height, float cellSize, Vector3 origin, Func <MyGrid <MyGridObject>, int, int, MyGridObject> createGridObject) { this.width = width; this.height = height; this.cellSize = cellSize; this.origin = origin; gridArray = new MyGridObject[width, height]; debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) //initialisierung aller gridobjekte { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } //grid wird gezeichnet und beschriftet --debug if (debug) { for (int i = 0; i < gridArray.GetLength(0); i++) { for (int j = 0; j < gridArray.GetLength(1); j++) { debugTextArray[i, j] = UtilsClass.CreateWorldText(gridArray[i, j]?.ToString(), null, IsoMatrix.Iso(GetWorldPosition(i, j) + new Vector3(cellSize, cellSize) * 0.5f), 20, Color.white, TextAnchor.MiddleCenter); Debug.DrawLine(IsoMatrix.Iso(GetWorldPosition(i, j)), IsoMatrix.Iso(GetWorldPosition(i, j + 1)), Color.white, 100f); Debug.DrawLine(IsoMatrix.Iso(GetWorldPosition(i, j)), IsoMatrix.Iso(GetWorldPosition(i + 1, j)), Color.white, 100f); } } Debug.DrawLine(IsoMatrix.Iso(GetWorldPosition(0, height)), IsoMatrix.Iso(GetWorldPosition(width, height)), Color.white, 100f); Debug.DrawLine(IsoMatrix.Iso(GetWorldPosition(width, 0)), IsoMatrix.Iso(GetWorldPosition(width, height)), Color.white, 100f); //triggert mit dem event eine Änderung des texts im debugarray falls sich ein gridobject geändert hat OnGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) => { debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y].ToString(); }; } }
public List <Vector3> FindPathV3(Vector3 startWorld, Vector3 endWorld) { grid.GetXY(startWorld, out int startx, out int starty); Debug.Log("Player =" + startx + "/" + starty); grid.GetXY(endWorld, out int endx, out int endy); Debug.Log("Target =" + endx + "/" + endy); List <PathNode> path = this.FindPath(startx, starty, endx, endy); if (path == null) { return(null); } else { List <Vector3> vectorpath = new List <Vector3>(); foreach (PathNode node in path) { vectorpath.Add(IsoMatrix.Iso(new Vector3(node.x, node.y) * grid.GetCellsize() + Vector3.one * grid.GetCellsize() * 0.5f)); } return(vectorpath); } }
private void Update() { player.handleMovement(); if (Input.GetMouseButtonDown(0)) { /* heatmap * Vector3 mouseP = UtilsClass.GetMouseWorldPosition(); * HeatMapObject currentValue = testGrid.GetGridObject(mouseP); * if (currentValue != null) * { * currentValue.AddValue(5); * } */ } if (Input.GetMouseButtonDown(1)) { Vector3 mouseP = UtilsClass.GetMouseWorldPosition(); if (!player.haspath) //gibt dem player inen pfad wenn dieser gerade keinen hat { player.setTarget(mouseP); } //DEBUG zeichnet player pfad List <Vector3> path = pathfinding.FindPathV3(IsoMatrix.InvIso(player.transform.position), IsoMatrix.InvIso(mouseP)); if (path != null) { for (int i = 0; i < path.Count - 1; i++) { Debug.DrawLine(path[i], path[i + 1], Color.red, 500f); } } } }
private void Update() { player.handleMovement(); if (Input.GetMouseButtonDown(1)) { Vector3 mouseP = UtilsClass.GetMouseWorldPosition(); if (!player.haspath) //pfad wird gesetzt wenn der player nicht bereits einen hat { player.setTarget(mouseP); //DEBUG zeichnet player pfad List <Vector3> path = pathfinding.FindPathV3(IsoMatrix.InvIso(player.transform.position), IsoMatrix.InvIso(mouseP)); if (path != null) { for (int i = 0; i < path.Count - 1; i++) { Debug.DrawLine(path[i], path[i + 1], Color.red, 500f); } } } } }