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-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
IntVec2 Directions[] = {Dir(0,1), Dir(0,-1), Dir(-1,0), Dir(1,0)}; int Dist = 1; while(true) { IntVec2 CurPos = StartPos + Dir[moveDirection] * Dist; if (Map.Countains(CurPos) && Map.Walkable(CurPos)) { Dist++; } else { break; } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public Cell GetTargetCell(Vector2Int movementDirection) { Vector2Int targetCellIndex = new Vector2Int( /* aktuelleZelleMitSpielerX */ , /* aktuelleZelleMitSpielerY */ ); while (targetCellIndex.x >= 0 && targetCellIndex.y >= 0 && targetCellIndex.x < mapCells.GetLength(0) && targetCellIndex.y < mapCells.GetLength(1) && !mapCells[targetCellIndex.x + movementDirection.x, targetCellIndex.y + movementDirection.y].IsObstacle) { targetCellIndex += movementDirection; } return mapCells[targetCellIndex.x, targetCellIndex.y]; } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Garzec« (22.01.2018, 10:15)
C#-Quelltext |
|
1 |
Vector2Int targetCellIndex = new Vector2Int(cellIndexHorizontal, cellIndexVertical); |
C#-Quelltext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int cellIndexHorizontal = 0; int cellIndexVertical = 0; for (int x = 0; x < mapCells.GetLength(0); x++) { for (int y = 0; y < mapCells.GetLength(1); y++) { Cell mapCell = mapCells[x, y]; if (mapCell.Equals(playerInfo.CurrentCell)) { cellIndexHorizontal = x; cellIndexVertical = y; } } } Vector2Int targetCellIndex = new Vector2Int(cellIndexHorizontal, cellIndexVertical);
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 Vector2Int targetCellIndex = new Vector2Int(cellIndexHorizontal, cellIndexVertical); // Startposition int distance = 1; // Startwert while (true) { Vector2Int currentCellIndex = targetCellIndex + movementDirection * distance; // neue Zielposition bool cellExists = currentCellIndex.x >= 0 && currentCellIndex.y >= 0 && currentCellIndex.x < mapCells.GetLength(0) && currentCellIndex.y < mapCells.GetLength(1); // Zelle innerhalb des Spielfeldes? if (cellExists && !mapCells[currentCellIndex.x, currentCellIndex.y].IsObstacle) // Zelle im Spielfeld und kein Hindernis { distance++; } else { break; } } targetCellIndex += movementDirection * distance; // eigentliche Zielposition setzen return mapCells[targetCellIndex.x, targetCellIndex.y];
Administrator
C#-Quelltext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int cellIndexHorizontal = 0; int cellIndexVertical = 0; for (int x = 0; x < mapCells.GetLength(0); x++) { for (int y = 0; y < mapCells.GetLength(1); y++) { Cell mapCell = mapCells[x, y]; if (mapCell.Equals(playerInfo.CurrentCell)) { cellIndexHorizontal = x; cellIndexVertical = y; } } }
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 public Vector2Int GetTargetCellIndex(Vector2Int movementDirection) { Vector2Int targetCellIndex = new Vector2Int(playerInfo.CurrentCellIndex.x, playerInfo.CurrentCellIndex.y); int distance = 1; while (true) { Vector2Int currentCellIndex = targetCellIndex + movementDirection * distance; bool cellExists = currentCellIndex.x >= 0 && currentCellIndex.y >= 0 && currentCellIndex.x < mapCells.GetLength(0) && currentCellIndex.y < mapCells.GetLength(1); if (cellExists && !mapCells[currentCellIndex.x, currentCellIndex.y].IsObstacle) distance++; else break; } if(distance > 1) targetCellIndex += movementDirection * distance; return targetCellIndex; }
Administrator
Zitat
n the above if clause, Condition1() will not be evaluated because whatever the value of Condition1() the whole expression will be false.If we use & instead of &&, it will execute Condition1() also. So always try to stick with short-circuited version.
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 public Vector2Int GetTargetCellIndex(Vector2Int movementDirection) { Vector2Int targetCellIndex = new Vector2Int(playerInfo.CurrentCellIndex.x, playerInfo.CurrentCellIndex.y); int distance = 1; while (true) { Vector2Int currentCellIndex = targetCellIndex + movementDirection * distance; bool cellExists = currentCellIndex.x >= 0 && currentCellIndex.y >= 0 && currentCellIndex.x < mapCells.GetLength(0) && currentCellIndex.y < mapCells.GetLength(1); if (cellExists && !mapCells[currentCellIndex.x, currentCellIndex.y].IsObstacle) { distance++; } else { break; } } distance--; targetCellIndex += movementDirection * distance; return targetCellIndex; }
C#-Quelltext |
|
1 |
bool cellExists = Math.Min(mapCells.GetLength(0) - 1, Math.Abs(x)) == x && Math.Min(mapCells.GetLength(1) - 1, Math.Abs(y)) == y; |
Werbeanzeige