Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C#-Quelltext |
|
1 2 3 4 5 6 7 |
public enum MovementDirections { Up, Down, Left, Right } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Cell { public Cell(int posX, int posY, bool isObstacle) { PosX = posX; PosY = posY; IsObstacle = isObstacle; } public int PosX { get; set; } public int PosY { get; set; } public bool IsObstacle { get; set; } } |
C#-Quelltext |
|
1 |
mapCells |
C#-Quelltext |
|
1 |
playerInfo.CurrentCell |
C#-Quelltext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 public Cell GetTargetCell(MovementDirections movementDirection) { List<Cell> cellsOnPath = new List<Cell>(); // Alle Zellen in der Reihe for (int i = 0; i < mapCells.Count; i++) { Cell currentCell = mapCells[i]; switch (movementDirection) { case MovementDirections.Up: if (currentCell.PosX == playerInfo.CurrentCell.PosX && currentCell.PosY > playerInfo.CurrentCell.PosY) // (0,1) { cellsOnPath.Add(currentCell); } break; case MovementDirections.Down: if (currentCell.PosX == playerInfo.CurrentCell.PosX && currentCell.PosY < playerInfo.CurrentCell.PosY) // (0,-1) { cellsOnPath.Add(currentCell); } break; case MovementDirections.Left: if (currentCell.PosX < playerInfo.CurrentCell.PosX && currentCell.PosY == playerInfo.CurrentCell.PosY) // (-1,0) { cellsOnPath.Add(currentCell); } break; case MovementDirections.Right: if (currentCell.PosX > playerInfo.CurrentCell.PosX && currentCell.PosY == playerInfo.CurrentCell.PosY) // (1,0) { cellsOnPath.Add(currentCell); } break; } } int closestObstacleIndex = cellsOnPath.Count; // der Index für das "erste" Hindernis for (int i = 0; i < cellsOnPath.Count; i++) { Cell currentCell = cellsOnPath[i]; switch (movementDirection) { case MovementDirections.Up: if (currentCell.PosY < closestObstacleIndex) { closestObstacleIndex = currentCell.PosY; } break; case MovementDirections.Down: if (currentCell.PosY > closestObstacleIndex) { closestObstacleIndex = currentCell.PosY; } break; case MovementDirections.Left: if (currentCell.PosX > closestObstacleIndex) { closestObstacleIndex = currentCell.PosX; } break; case MovementDirections.Right: if (currentCell.PosX < closestObstacleIndex) { closestObstacleIndex = currentCell.PosX; } break; } } return cellsOnPath[closestObstacleIndex]; }
Administrator
Administrator
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public Cell GetTargetCell(Vector2 movementDirection) { Vector2 currentCellIndex = GetPlayerCellIndex(); // aktuellen Zellindex holen Vector2 targetCellIndex = GetTargetCellIndex(currentCellIndex, movementDirection); // rekursiv die Felder durchsuchen return mapCells[(int)targetCellIndex.x, (int)targetCellIndex.y]; } private Vector2 GetTargetCellIndex(Vector2 currentCellIndex, Vector2 movementDirection) { Vector2 nextCellIndex = currentCellIndex + movementDirection; int maxLengthHorizontal = mapCells.GetLength(0); // Länge einer Reihe int maxLengthVertical = mapCells.GetLength(1); // Länge einer Spalte bool fitsMinIndexHorizontal = nextCellIndex.x >= 0; bool fitsMinIndexVertical = nextCellIndex.y >= 0; bool fitsMaxIndexHorizontal = nextCellIndex.x < maxLengthHorizontal; bool fitsMaxIndexVertical = nextCellIndex.y < maxLengthVertical; if (fitsMinIndexHorizontal && fitsMinIndexVertical && fitsMaxIndexHorizontal && fitsMaxIndexVertical) // Innerhalb des Spielfeldes? { bool nextCellIsObstacle = mapCells[(int)nextCellIndex.x, (int)nextCellIndex.y].IsObstacle; if (!nextCellIsObstacle) { return GetTargetCellIndex(nextCellIndex, movementDirection); } } return currentCellIndex; } |
Administrator
Die Konvertierungen mache ich deshalb, weil Vector2 eine Klasse von Unity ist und die Werte nur floats sein können. Also müsste ich mir entweder eine eigene Koordinaten Klasse schreiben (würde wohl Sinn machen) oder ich muss es zu Beginn umwandeln.
Werbeanzeige