Slick Forums

Discuss the Slick 2D Library
It is currently Fri Apr 18, 2014 5:14 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Aug 22, 2011 9:54 am 
Offline

Joined: Fri Apr 08, 2011 3:55 pm
Posts: 49
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:
Image Image
Image Image
Image Image

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:
Code:
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2011 11:30 am 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
My competition entry Back to the past does auto tiling (that's the name of your desired implementation).
I only look at the tiles up, down, left and right but that might be already working for you too.

Download the source code from the above link and have a look at Level.java in method prepareBlockedArray(). Basically I check for each wall tile if the 4 interesting other tiles (up, down, left, right) are walls or not. I use power of 2 values for the tiles to calculate a sprite index between 0 (all 4 surrounding tiles are walls) and 15 (none of the 4 tiles is a wall). Up is 1, right is 2, down is 4 and left is 8.

Then I use a sprite sheet (greyblocks40x40.png in res folder) containing 16 images matching the possible sprite index.

Hope that helps,
Tommy

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2011 12:27 pm 
Offline
Regular

Joined: Sat Aug 16, 2008 7:04 pm
Posts: 119
Here is a nice article about this method:

http://www.saltgames.com/?p=184

_________________
Gamedev link-collection (german page)
My Gamedev Blog
Play Cyber Dungeon Quest!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2011 1:15 pm 
Offline

Joined: Fri Apr 08, 2011 3:55 pm
Posts: 49
Thanks both of you!:) Now my only problem is setting the correct number when my tile is a "game corner tile" so I can not check its neighbors in the same way as I normally do. But I'm thinking perhaps I should just hard-code that!
I use the following values:
1 2 4
8 X 16
32 64 128

// Edit:
Hardcoded the edges of the map and it works perfectly now! Thank you so much for your fast help!

// Edit:
Holy crap this will still turn into quite some code, or quite some other work! I mean there's 8 different neighbors to check and I need to check all possible combinations! So I guess there's 2^8 = 258 combinations that need checking / images. Although of course multiple combinations use the same tile and stuff it still is a lot of checks! I might just have to add some limitations on how I allow the user to build walls - such as not allow diagonal walls! Anyway this is working better then any solution I had so I am still happy! Thanks again!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Jon and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group