Skip to content
Snippets Groups Projects
Commit c264f2ba authored by Julian Thijssen's avatar Julian Thijssen
Browse files

Abstract bounds-checking code

parent 00d66d3e
No related branches found
No related tags found
No related merge requests found
......@@ -81,8 +81,8 @@ public class CoastlineAgent
for (int y = -1; y <= 1; y++)
{
Vector2 currPos = new Vector2((int)pos.x + x, (int)pos.y + y);
if (currPos.x < 0 || currPos.x >= coastlineGenerator.TerrainSize) continue;
if (currPos.y < 0 || currPos.y >= coastlineGenerator.TerrainSize) continue;
if (!PositionInBounds(currPos)) continue;
int currIndex = (int)(currPos.x + currPos.y * coastlineGenerator.TerrainSize);
if (coastlineGenerator.landBitField[currIndex]) continue;
......@@ -102,16 +102,8 @@ public class CoastlineAgent
tokensRemaining--;
return true;
}
//Debug.Log(assigned);
if(highestPos.x < 0 || highestPos.x >= coastlineGenerator.TerrainSize)
{
Debug.Log("whoops");
}
else if (highestPos.y < 0 || highestPos.y >= coastlineGenerator.TerrainSize)
{
Debug.Log("whoops");
}
else
if (PositionInBounds(highestPos))
{
coastlineGenerator.coastText.SetPixel((int)highestPos.x, (int)highestPos.y, Color.grey);
int highestIndex = (int)highestPos.x + (int)highestPos.y * coastlineGenerator.TerrainSize;
......@@ -122,8 +114,8 @@ public class CoastlineAgent
if (!borderPart.Contains(highestPos)) borderPart.Add(highestPos);
coastlineGenerator.RemoveNonBoundaryPixels(highestPos);
}
else Debug.Log("Whoops");
tokensRemaining--;
return true;
}
......@@ -138,6 +130,16 @@ public class CoastlineAgent
return ((repulsor - pos).sqrMagnitude * 1f - (attractor - pos).sqrMagnitude * 1f) + 3 * mapDist * mapDist;
}
private bool PositionInBounds(Vector2 pos)
{
return pos.x >= 0 || pos.y >= 0 || pos.x <= (coastlineGenerator.TerrainSize - 1) || pos.y <= (coastlineGenerator.TerrainSize - 1);
}
private bool PositionOnEdge(Vector2 pos)
{
return pos.x == 0 || pos.y == 0 || pos.x == (coastlineGenerator.TerrainSize - 1) || pos.y == (coastlineGenerator.TerrainSize - 1);
}
public Vector2 GetRandomPosition()
{
if (borderPart.Count == 0)
......@@ -148,14 +150,7 @@ public class CoastlineAgent
int randIndex = Random.Range(0, borderPart.Count);
Vector2 pos = borderPart[randIndex];
if(pos.x == 0 || pos.x == (coastlineGenerator.TerrainSize - 1))
{
return Vector2.zero;
}
if(pos.y == 0 || pos.y == (coastlineGenerator.TerrainSize - 1))
{
return Vector2.zero;
}
if (PositionOnEdge(pos)) return Vector2.zero;
bool inland = true;
......@@ -165,8 +160,7 @@ public class CoastlineAgent
{
if (x == 0 && y == 0) continue;
if ((pos.x + x) < 0 || (pos.x + x) >= coastlineGenerator.TerrainSize) continue;
if ((pos.y + y) < 0 || (pos.y + y) >= coastlineGenerator.TerrainSize) continue;
if (!PositionInBounds(pos + new Vector2(x, y))) continue;
int index = (int)((pos.x + x) + (pos.y + y) * coastlineGenerator.TerrainSize);
if (!coastlineGenerator.landBitField[index])
......@@ -206,8 +200,7 @@ public class CoastlineAgent
{
Vector2 pos = new Vector2(-1, -1);
if (startPos.x == 0 || startPos.x == (coastlineGenerator.TerrainSize - 1)) return pos;
if (startPos.y == 0 || startPos.y == (coastlineGenerator.TerrainSize - 1)) return pos;
if (PositionOnEdge(startPos)) return pos;
Vector2 prevPos = pos;
bool foundCoastPoint = false;
......@@ -217,8 +210,8 @@ public class CoastlineAgent
do
{
Vector2Int newPos = new Vector2Int((int) (oldPos.x + prefDirection.x), (int) (oldPos.y + prefDirection.y));
if (newPos.x < 0 || newPos.x >= coastlineGenerator.TerrainSize) return pos;
if (newPos.y < 0 || newPos.y >= coastlineGenerator.TerrainSize) return pos;
if (!PositionInBounds(pos)) return pos;
int idx = coastlineGenerator.TerrainSize * newPos.y + newPos.x;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment