brinycann0nade
Member
I'm making a top down, tile-based city builder/rts where buildings have a power resource from power lines like SimCity 2000. They need power to work.
Each powerline tile looks at the surrounding tiles (25x25 pixels) and saves the id of the surrounding instances. You end up with the id's of the tile above, below, and to the left and right of the powerline. Diagonals don't count.
(I'm aware using "all" will get anything near it but that's a problem for another day)
Here is my problem: There are so many combinations of up right down left (at least 11), that I think I have to write at least 11 if statements. This feels wrong, but I don't know a better way. A switch seems like it wouldn't work because there is more than one thing to check. My gut is telling me to use an array or data structure in some way, plus a for loop, but I'm not sure how.
In summary, I would like to be able to loop through the combinations of up right down left, without so many if statements because it feels wrong.
Each powerline tile looks at the surrounding tiles (25x25 pixels) and saves the id of the surrounding instances. You end up with the id's of the tile above, below, and to the left and right of the powerline. Diagonals don't count.
Code:
//obj_humanPowerline STEP event
up = place_meeting(x, y-20, all);
right = place_meeting(x+20, y, all);
down = place_meeting(x, y+20, all);
left = place_meeting(x-20, y, all);
Here is my problem: There are so many combinations of up right down left (at least 11), that I think I have to write at least 11 if statements. This feels wrong, but I don't know a better way. A switch seems like it wouldn't work because there is more than one thing to check. My gut is telling me to use an array or data structure in some way, plus a for loop, but I'm not sure how.
Code:
obj_humanPowerline DRAW event
if up and !right and down and !left
{
image_index = 0;
}
else if !up and right and !down and left
{
image_index = 1;
}
if up and right and down and left
{
image_index = 2;
}
//etc etc