- Home /
Trying to figure out tilemap movement based on dice roll
So I'm making a board game but I'm a bit stuck on how I would go about giving each map tile a specific movement.
I want the player to always start on the Star icon when the game is first run no matter where the star icon may be on the map.
Then the player will role a die and depending on the outcome they will move that many spaces away from their current position, however moving on the grass and dirt tiles will take 1 movement of the dice roll, moving on the forest tiles will take 2 movements of the dice roll, and moving in the ocean will take 3 movements of the dice roll.
I'm stumped as to how to do this so any help would be appreciated. Thank you in advance!
Answer by KoenigX3 · Mar 30, 2021 at 04:13 PM
Sorry, I don't quite understand, but I'll try to help you.
I would create an 2D integer array, where the integers identify the type of the tile. Then I would roll the dice, and get an integer, representing the number of movements I have. On each movement, I would check the type of the target tile, and decrease my movement integer according to the type. If I didn't have enough movement, then I would not move my character.
int movement = Random.Range(1, 7); // The upper limit is exclusive
// This is the logic you can use when making your moves
if(movementCost[targetTileType] <= movement)
{
// Move character to the tile
movement -= movementCost[targetTileType];
}
Here, movementCost is an integer array, that stores the movement cost of each tile type. For example, if you identify dirt with index 0, forest with index 1, and ocean with index 2, it's [1, 2, 3]. You can get the variable targetTileType from the 2D integer array that I've already mentioned.
Now, this would work only if you moved your character step by step. If you wanted to reach a farther tile, a problem would arise. For example, let's say that you want to go up and right - the problem here is that you could also go right and up. Using a different path can make a difference if the tiles are different. This problem can be solved by pathfinding algorithms.
Thank you, I haven't tried it yet because I've been adjusting other aspects of my game, but once I get back to tile movement I will try it out. I just didn't want to leave you hanging!
Your answer
Follow this Question
Related Questions
Add tiles with script 1 Answer
Pathfinding in 2d RPG 0 Answers
Prefab editor (tilemap) doesn't work? 0 Answers
Why is my tilemap disappearing in game mode?,My tiles disappear in play mode 1 Answer