I am writing a small game to learn path finding(which was quite easy by the way!:D). It is kind of a tower defense game where you get to build the walls yourself, and I’ve run into problems.
I first implemented walls as multiple different classes, each with different images and I then allowed the user to chose what his wall should look like by picking different classes.
Some image examples to make things clearer:
Now my problem is that I instead want to use only one class - Wall. And I want the class constructor to calculate what image is the correct image. I have tried some different solutions but they all come down to me realizing that they will probably work but they will take a week to write and a year to debug because they will be so long and so messy.
Basically what I have had in mind is, when a wall is created it will look around its 8 neighbours: (X beeing the wall itself)
1 2 3
4 X 5
6 7 8
and check which are walls, then select image using a bazillion of if-phrases and then notifying all the walls nearby that “I am new, check your surroundings again and update your images accordingly”.
My problem is that to do this, my only idea is to go like:
if (map[tileY-1][tileX-1] instanceof Wall && map[tileY-1][tileX] instanceof Wall .... && map[tileY+1][tileX+1] instanceof Wall) {
setImage();
} else if (!map[tileY-1][tileX-1] instanceof Wall && map[tileY-1][tileX] instanceof Wall .... && map[tileY+1][tileX+1] instanceof Wall) {
setImage();
} ... {
}
But this would just be so insanely long and complicated to read through it just don’t seem like a very good idea. I mean it would be like 2^8 if statements?
So I decided to ask you guys! How can I solve this?