(sorry for my english I’m french) Hello everyone, I need your help for a problem that I have since really long time without finding a solution (6/7 months) …
So, I would like my program to change the animation of my objects “road” once it is created, but with certain conditions, such as the location of one or more adjacent roads.
(each square corresponds to a “road” object)
(here each object “road” changes animation and become what they should be logically)
Do you have any idea how I could proceed please ?
I have never tried anything like this so I have no experience how to do it, but my first try would be is to setup a grid system and check for each grid one by one what road is display there.
So lat say we have a grid like this
Where 0 is empty 1 is road. So what I would do is check the neighbour of each tile and say if A2 display a road, set animation at B2 to T junction if A2 and C2 display a road set animation at B2 to cross road… And pretty much setup an algorithm inside an external event and whenever I use the paint road tool, run it to update all the grids.
To make it more advanced, each grid tile could also store information in object variables regarding what road is it, is it a motorway or highway and add that too regarding what animations to display.
There is definitely lot of place for errors, it won’t be easy.
A grid system is really well thought out, but not so easy. I’ll try what you explained to me and then if other people give me a simpler solution, I’ll take x)
Thank you very much ddabrahim !
Well, if you want to paint roads like in simcity anywhere on the map, and want the pieces to magically fit together and form a road there is no other way around it imo except, you can make your life easier and add different road tiles the player can choose from and let the players design the roads however they want. If they make a mistake it obviously leads to errors if you also want traffic on the roads, but let the player deal with it. This is the most simple way I can think of.
Yes, it’s the system already present, if you think it’s not bad, I’ll give it time to set up a grid system. I would share a playable version and you will tell me what you think
It can be done, but it’s a bit tedious, this is my idea: Each time you create a tile, check the neighbors through a helper object to collide or through position checking, and then do the machine work, checking all the possible combinations of neighbors to set the right animation. Also you should modify the animation of each neighbor, because there is a new tile.
It surely can be optimized with external events…
Not necessarily, I guess you need some sort of grid system for this kind of games anyway to position roads, rails, houses, grass…etc. So the only thing need to be done is add object variables to each grid tile to store it row and column number and simply when you place a road, get the R and C value of the tile and setup a formula to find it neighbours using the RC values. For example if placed a road at R2C2 you can simply check if R2C(2+1), R2C(2-1) and R(2-1)C2, R(2+1)C2 displaying road or not… You can even create the RC values on the fly at the beginning just go through each tile and add the values systematically and when you place a road go through again and get the RC values from the variables to find the neighbours.
But yeah, I guess it would be a lot easier to simply check the colliding tiles and their X and Y position to find the neighbours
Ok, the row/colum system is a great idea, and will be easier for future checks. Thinking about it a bit, I’ve a more optimized and almost working idea (I can’t test it though).
Each tile needs 6 variables: row, column, up, down, left, right.
The column and row are obviously calculated from the position (x/grid_width, y/grid_height), the other 4 variables are 0 or 1 depending if there is a neighbor in that direction.
Now you need an external event that assumes there is only one tile selected, and checks the variables up, down, left and right to set the tile animation (there are 2⁴ checks to do at most).
And this would be the logic to apply when a new tile is created:
Replace mouse release with your conditions to create a new tile. I’ve written two of the four directions (it isn’t easy from a phone).
Finally, the external event linked should do something like:
If Tile variables up, down, left and right = 0 >> set animation 0
If variable up = 1 and the others are = 0 >> set animation 1
Brilliant! With some binary magic you don’t have to do the 16 tedious checks, you have to do only 0
When you are checking the neighbors, for example if there is a tile up, instead set the variable “up” = 1, do + 1 to a variable “animation”. If there is a tile at right do + 2 to “animation”. + 4 if there is a neighbor downward, and + 8 for a left neighbor (you can switch the values for the directions, for example left = 1 and up = 4).
This way the variable “animation” will have an unique value per tile combination, now you have to set sprite animation = variable animation, and order your sprite animations in the same way:
0 - No neighbors
1 - Up
2 - Right
3 - Right + Up
4 - Down
5 - Down + Up
6 - Down + Right
7 - Down + Right + Up