godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.13k stars 92 forks source link

Exposing Tilemap as Matrix and multidimensional Tilemaps #2540

Open LordBrkica opened 3 years ago

LordBrkica commented 3 years ago

Describe the project you are working on

Hexgrid turnbased game.

Describe the problem or limitation you are having in your project

A Hex/Square/Tile in a lot of hexgrid games (like Civilization) usually contains several elements, such as River, Road, Improvement etc.

Rigth now I am using the following structure:

Node:
-TileMapRiver
-TileMapRoad
-TileMapImprovment

One cannot use just one tilemap because a tile has River AND/OR Road AND/OR Improvement (its not a OR situation where I could put everything into single TileMap). Each of these 3 tilemaps will always have the same MxN size and each of the cells has the same dimension.

If I want to check the properties of one tile I have to call GetCellV(X,Y) on all 3 of these tilemaps.

This could be done better/more intuitive as these 3 Tilemaps are much like a N-dimensional matrix (having the same number of cells and cell sizes).

Describe the feature / enhancement and how it helps to overcome the problem or limitation

We introduce a MultidimensionalTileMap object which can hold several TileMaps (of course under the condition that they have the same dimension).

Then, when one calls multilayeredTileMap.GetCellV(x,y) we return a Tuple of values (for each sub-TileMap).

We could also introduce some exposing/casting of tilemap as primitive matrix/array. This helps when serializing tilemap data to .json for example and we could then also introduce simple mathematical and logical operations for these tilemaps (as they are in core matrices and now exposed as such).

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

A tilemap is in its core a simple matrix of MxN dimensions. Just like we can store matrices of Red,Green and Blue to form a N-dimensional Image matrix, so we could have a mulidimensional tilemap.

And just like we can (in most languages) make simple mathematical and logical operation on matrices - so we should be able to do the same with tilemaps of same dimensions.

Here is simple example in Python/Numpy (1D but principle is the same for 2D):

a = [True, True, False]
b = [True, False, False]
c =  np.logical_or(a, b) 
print(c)
[ True True False]

Right now, if we want to now the value of element 2 we manually check it across all the tilemaps and make the comparison. And we if we want to create entire tilemap c we have to create it manually.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Of course it can be worked around. One can use several tilemaps and "manually" get the necessary stuff.

Is there a reason why this should be core and not an add-on in the asset library?

I believe that exposing the tilemap as a matrix adds a lot of ease of use to most programmers. After all simple array/matrix operations are common in most programming languages.

mrjustaguy commented 3 years ago

What is Proposed is pointless (If I understood it correctly). You can Just A) Add the data to a custom script or B) Make New tiles with specific Properties and just get cell xy to get what exactly you want on a Tile be represented just by it's ID, so All Tiles of ID 2 for example are River, and all Tiles of ID 1 are Dirt, and so on, thus removing the need to have multiple tilemaps for different behavior

Zireael07 commented 3 years ago

@mrjustaguy: You clearly missed this part:

One cannot use just one tilemap because a tile has River AND/OR Road AND/OR Improvement (its not a OR situation where I could put everything into single TileMap).

It's not River OR road OR improv, it's a situation where a single tile might have one, might have two (either pair) or even all three.

mrjustaguy commented 3 years ago

Ok yea missed that. my bad..