1
\$\begingroup\$

I trying to create a turn-based strategy game where you move a game object from one tile of a grid to another tile near it. I have created the grid using sprites and game objects but can't sort out the movement part.

How can I highlight adjacent tiles given the 'current' tile, and then allow a game object to move to any of those highlighted tiles?

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Dota 2 characters :D Just make sure you don't use break any copyright laws! \$\endgroup\$ – Kroltan Dec 19 '14 at 19:21
  • 1
    \$\begingroup\$ @Kroltan just for testing >.< \$\endgroup\$ – vongolashu Dec 19 '14 at 20:00
  • \$\begingroup\$ Just kidding too. Valve is quite relax about using their chars too. :) \$\endgroup\$ – Kroltan Dec 19 '14 at 20:03
  • \$\begingroup\$ In other words, you want to highlight possible paths when you select a unit? Or just the tiles directly next to the selected tile? \$\endgroup\$ – jzx Dec 20 '14 at 7:28
  • \$\begingroup\$ @jzx it would be possible paths but since you can only move path-to-path, possible paths will till be next to selected tile. \$\endgroup\$ – vongolashu Dec 20 '14 at 21:02
1
\$\begingroup\$

From your screenshot it looks like you've made your grid by placing a bunch of specific sprites or prefabs or whatnot. This will make it difficult to address your needs for adjacency, because each tile is totally distinct.

Instead, consider storing the tiles of your map in an array somewhere. This will allow you to use array indexing to determine tiles adjacent to any other tile, which will let you modify the tile accordingly to highlight it, and know where to move a game object if the tile is clicked on.

For example, you can store a 2D grid of tile sprites like this:

TileSprite[] tiles = new TileSprite[width * height];

This creates width * height tile sprite objects, which you can then initialize accordingly to fill up your map (e.g., tiles[0] = new TileSprite("grass.png") and so on, or whatever is appropriate).

Even though the above is a linear array of tiles you can conceptualize it as a grid. If width and height are both 4, for example, the array (X represents a sprite in the array) looks like this

0 1 2 3 4 5 6 7 8 9 ...
X X X X X X X X X X X X X X X X 

but you can also think of it like this

X X X X
X X X X
X X X X
X X X X

and thus to get the tile at position (x, y) you apply the following indexing math:

var tile = tiles[y * width + x];

This allows you to get the adjacent tiles to (x, y) by adding or subtracting one from both x and y.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

To highlight adjacent tiles from current tile, use a modified floodfill with a depth parameter to get all the nearest tiles within X depth/distance.

To allow a GO to move on any of those adjacent tiles, said floodfill should return a list of tiles, check if clicked tiled is in the list, if so, move GO.

A possible implementation of said floodfill (in Python) from here:

visited = set()
add start to visited
fringes = [[start]]
for each 1 < k ≤ movement:
    fringes[k] = []
    for each cube in fringes[k-1]:
        for each 0 ≤ dir < 6:
            neighbor = cube.add(Cube.direction(dir))
            if neighbor not in visited, not blocked:
                add neighbor to visited
                fringes[k].append(neighbor)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Things I did just now:- Added Box collider to GO (trigger), add RigidBody to GO and add PolygonCollider to grass tile-sprite. This way I got the transform.position for the tile the player is standing on currently. I am reading the TextureFloodFill on UnityWiki but not able to understand it w.r.t. your reply. Can you perhaps provide a example ? \$\endgroup\$ – vongolashu Dec 19 '14 at 20:10
  • \$\begingroup\$ Do you know how to implement a breadth first search? Use that but bail early after it checks n levels of surrounding tiles. \$\endgroup\$ – user55564 Dec 19 '14 at 20:19
  • \$\begingroup\$ am afraid but am somewhere between beginner and intermediate level of knowledge, have not ever worked with breadth first search or floodgate (do know its concept), and the python based example is a bit confusing as working in C#. I am trying to understand it to create a C# counterpart of it but a bit confused as never used python, if you can explain what each line is doing, I might be able to work it out \$\endgroup\$ – vongolashu Dec 19 '14 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.