Thanks guys, It’s not so much that I want to optimize, but that I want to use the best tools for the job.
For the landscape I’m using blocks which are offset from the center (the center of the tile is bottom left or each one). Each block has a physical mesh, and UV which can be moved to create alignments of texture on the physical mesh. I’m using nodes and object color to do the UV scroll rather than actually messing with UV scrolling.
The physical mesh looks like this:
While the texture mask looks like this:
By comparing 3 neighbors to the center tile you can get which tile to use.
Thanks to Blender Artist Nines for reminding me about this, as it’s something I had read but forgotten about.
First I check the square to the right, (1,0) offset from the current square. If it is valid (contains a color or height or feature or whatever) I add one to the base number. Next is the square to the top right (1,1) which will add 2, next the square immediately above the one I’m working on (0,1) which will add 4 to the score, and finally the home square (0,0) which will add 8. Because of the way binary numbers work, these 4 values will add up to a number between 0 and 15. So I just name my tiles “tile_0” to “tile_15”. Saving this data would be as simple as saving an integer.
for x in range(-1,mapsize):
for y in range(-1,mapsize):
main_tile = level_dict.get((x,y),[0,0])
search_array = [(1,0,1),(1,1,2),(0,1,4),(0,0,8)]
tile_number = 0
for n in search_array:
key = (x+n[0],y+n[1])
tile = level_dict.get(key,0)
if tile ==1:
tile_number += n[2]
tile_name = "tile_{}".format(tile_number)
tile_object = own.scene.addObject(tile_name,own,0)
tile_object.worldPosition = [x,y,0.0]
And that’s pretty much the whole of the code for building the map, at least height wise. There is more to it when I start working with the textures too.
But you can see above I’m getting the info (tile) from a dictionary (level_dict). This is fine if I only need it for the level building data, but the game is going to be a grid based, turn based strategy. I’ll also be using the level data as the graph for an A* search algorithm, line of sight calculations, AI strategy maps and much more. If I can use an efficient method of storing/handling that data it will help with all those functions. For instance it may be better to have multiple arrays, each storing one type of data rather than a single array/dictionary with data about pass-ability, height, occupied, damaged, high priority etc… since only one will be used by a particular function at a time.
I know any code written in Python is never going to be as fast as it could be in another language, but making the right choices in python can often result in a function which is twice as fast as it would be otherwise.
I don’t think I am going to save the connectivity data though, since the way the tiles connect visually is different from how they connect for movement or sight calculations.
EDIT:
Here’s a blendfor anyone who is interested.
It’s still just a prototype, so lots of work to do yet, such as making sure there’s only a single physical mesh for each tile and making the base texture non-transparent.