As the title says, I created a random walk position generator for our dungeon crawler game. But everytime i generate a room, there is still a duplicate position generated even though there is a duplicate checker (located at posGenerator() while loop). Sometimes it generates good (no duplicate positions) and sometimes it generates duplicates.
Any ideas and opinions are welcomed. Thanks!
posGenerator()
public void posGenerator() {
Vector2 currentPosition = transform.position;
positionList.Add(currentPosition);
for(int i = 0; i < roomCount; i++){
int currentDirection = RandomNumberGenerator();
currentPosition = directionalPosition(currentPosition, currentDirection);
while(positionList.Contains(currentPosition)){ // keep generating position until a new position is generated
currentPosition = generateNewPosition(positionList[i], currentDirection);
}
positionList.Add(currentPosition);
if(i+1 == roomCount){
for(int j=1;j<positionList.Count;j++){
Debug.Log("Position " + j + ": " + positionList[j]);
directionList.Add(generateDirectionList(positionList[j-1], positionList[j]));
}
}
}
}
generateNewPosition()
public Vector2 generateNewPosition(Vector2 position, int direction) {
int[] excludeDirections;
switch (direction) {
case 1: excludeDirections = new int[] { 2, 3, 4 }; break;
case 2: excludeDirections = new int[] { 1, 3, 4 }; break;
case 3: excludeDirections = new int[] { 1, 2, 4 }; break;
default: excludeDirections = new int[] { 1, 2, 3 }; break;
}
int randomIndex = Random.Range(0, excludeDirections.Length);
int newPosition = excludeDirections[randomIndex];
return directionalPosition(position, newPosition);
}
I tried changing the structure of while loop, tried using if else and refactoring my code. Still doesnt work. I expect no duplicate positions
Aucun commentaire:
Enregistrer un commentaire