Loop back to beginning of list in monopoly style game
Hey all,
I am making a Monopoly clone and I am having a problem with going around the board. I am using tiles and storing their positions in a List which moves the player depending on the dice roll.
My problem is when I go around the board and get to the last tile I am not sure how to loop it back to the beginning of the list so the player will continue pass go and start moving around the board again.
My current code is:
IEnumerator movePlayer(Token currentPlayerNumber, int rollValue)
{
isPlayerMoving = true;
//Get tile player is currently on
int currentTile = currentPlayerNumber.CurrentTileID;
//Move player according to dice value
for(int i = 0; i < rollValue; i++)
{
float t = 0;
//Makes the player move one tile every roll until it has reached the end position
Vector3 startPosition = tile[(currentTile + i)].transform.position + playerPosition[currentPlayerNumber.ID];
Vector3 endPosition = tile[(currentTile + i + 1)].transform.position + playerPosition[currentPlayerNumber.ID];
while (t < 1f)
{
t += Time.deltaTime * 4f;
currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
yield return null;
}
//Reset current tile position
currentPlayerNumber.CurrentTileID = currentTile + rollValue;
isPlayerMoving = false;
StartCoroutine(tileActions());
}
Any one have any ideas on how to achieve this?
Hi @beefyt123 din you finish your work?? i also creating monopoly game with a twist do you have some sample project that i can use as a guide? thanks in advance
Answer by smnerat · Aug 01, 2016 at 04:49 AM
Instead of using currentTile, you can use variable for the startTile and endTile index. Then you just have to check to see if the endTile value is greater than the last index of your tile array or not. This is untested code, but it should be enough to get the idea.
IEnumerator movePlayer(Token currentPlayerNumber, int rollValue) {
isPlayerMoving = true;
//Get tile player is currently on
int currentTile = currentPlayerNumber.CurrentTileID;
int sTile = currentTile;
int eTile;
for(int i = 0; i < rollValue; i++) {
float t = 0;
eTile++;
if(eTile > tile.length)
eTile = 0;
//Makes the player move one tile every roll until it has reached the end position
Vector3 startPosition = tile[sTile].transform.position + playerPosition[currentPlayerNumber.ID];
Vector3 endPosition = tile[eTile].transform.position + playerPosition[currentPlayerNumber.ID];
while (t < 1f) {
t += Time.deltaTime * 4f;
currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
sTile = eTile;
yield return null;
}
//Reset current tile position
currentPlayerNumber.CurrentTileID = currentTile + rollValue;
isPlayerMoving = false;
StartCoroutine(tileActions());
}
Thanks for the quick reply. I will try implementing what you suggested now.
EDIT I have tried what you suggested and now my player moves around the board past go as I want but only for that current roll. As soon as either player comes to a stop on the second lap around the board then I receive the error message below.
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[monopoly.Tile].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) GameController+c_Iterator2.$$anonymous$$oveNext () (at Assets/Scripts/GameController.cs:138) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(IEnumerator) c_Iterator1:$$anonymous$$oveNext() (at Assets/Scripts/GameController.cs:131)
Any thoughts?
Like I mentioned, this is untested code. $$anonymous$$y first guess is that it when I wrote this, once everything is done, the players currentTile is never set to the adjusted value.
At line 31, currentPlayerNumber.CurrentTileID is set to current + roll. So when this is called again, sTile is set to current + roll, which is out of range, and only eTile is adjusted in the loop. $$anonymous$$aybe try setting currentPlayerNumber.CurrentTileID to eTile inside of the loop right after line 27. That way sTile will start out as an adjusted value.
Thanks again I had a thought about what you said and I changed line 31 to currentPlayerNumber.CurrentTileID = eTile and everything seems to be working. I was able to move around the board multiple times so thanks for your help.
If anyone else is interested my final code is:
IEnumerator movePlayer(Token currentPlayerNumber, int rollValue)
{
isPlayer$$anonymous$$oving = true;
//Get tile player is currently on
int currentTile = currentPlayerNumber.CurrentTileID;
int sTile = currentTile;
int eTile = currentTile;
for (int i = 0; i < rollValue; i++)
{
float t = 0;
eTile++;
if (eTile >= tile.Count)
{
eTile = 0;
passGo();
}
//$$anonymous$$akes the player move one tile every roll until it has reached the end position
Vector3 startPosition = tile[sTile].transform.position + playerPosition[currentPlayerNumber.ID];
Vector3 endPosition = tile[eTile].transform.position + playerPosition[currentPlayerNumber.ID];
while (t < 1f)
{
t += Time.deltaTime * 4f;
currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
sTile = eTile;
yield return null;
}
//Reset current tile position
currentPlayerNumber.CurrentTileID = eTile;
StartCoroutine(tileActions());
}