Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] Determining which type of wall to draw...  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
Offline Z-Man
« Posted 2011-06-07 08:59:43 »

Hello. I'm working on a 2D tile based game. Right now I'm trying to get it so that walls draw so that they connect correctly, i.e. I have corner images, vertical and horizontal images, end piece images, and so on. The problem arises when I try to determine which image to draw. I have it working but I have the feeling that there is a better way to do it, and I just can't seem to find it. So help, suggestions, tips, etc. are greatly welcome. This isn't really a problem so I understand if nobody wants to answer, or if I posted in the wrong area, or something.

The level, is loaded from a text file into a char array containing chars that represent different map elements, like U for user, W for wall (The position of the walls don't change during the level). When the game is drawn the type of wall is checked by this method. Would getting the types at start time and storing them be better so I don't have to check every time the walls are drawn?
[CODE]
public int getWallType(int row,int col)
{
   boolean r = getType(row,col + 1) == WALL, // Space to the right is a wall
      l = getType(row,col - 1) == WALL, // Space to the left is a wall
      a = getType(row - 1,col) == WALL, // Space above is a wall
      b = getType(row + 1,col) == WALL; // Space below is a wall

   if(r && l && !a && !b)
      return WH; // Horizontal wall piece -

   else if(a && b && !r && !l)
      return WV; // Vertical wall piece |

   else if(b && !a && !r && !l)
      return WTE; // Top end of a wall ^

   else if(l && !r && !a && !b)
      return WRE; // Right end of a wall >

   else if(a && !b && !r && !l)
      return WBE; // Bottom end of a wall V

   else if(r && !l && !a && !b)
      return WLE; // Left end of a wall <

   else if(r && b && !l && !a)
      return WUL; // Upper left corner of a wall

   else if(l && b && !r && !a)
      return WUR; // Upper right corner of a wall

   else if(r && a && !l && !b)
      return WLL; // Lower left corner of a wall

   else if(l && a && !r && !b)
      return WLR; // Lower right corner of a wall

   return WM; // Middle wall piece +
}
[/CODE]
Offline Roquen
« Reply #1 - Posted 2011-06-07 09:38:10 »

Think of your four values as being one bit each instead of booleans. Combine them and you have a 4-bit integer.  Look up the result or simply have your result constants the same as the packing.
Offline Z-Man
« Reply #2 - Posted 2011-06-07 19:22:59 »

I'm not quite sure I understand what you mean, do you mean add one to a value if "true" and zero if "false" or construct an integer like "1111" meaning there are walls on all sides? Then check that?

EDIT: I think I found a better way than my original method. I did what I think you suggested and then just adjusted my constants to work with it. So A top left corner piece would be 0110. First position represents above 1 if there is a wall 0 if there isn't, second position is to the right, third is below, and fourth is to the left.

[CODE]
public int getWallType(int row,int col)
{
   String type = "";

   if(getType(row - 1,col) == WALL) // Above
      type += 1;
   else
      type += 0;

   if(getType(row,col + 1) == WALL) // To the right
      type += 1;
   else
      type += 0;

   if(getType(row + 1,col) == WALL) // Below
      type += 1;
   else
      type += 0;

   if(getType(row,col - 1) == WALL) // To the left
      type += 1;
   else
      type += 0;

   type = type.trim(); // Remove spaces

   // Make sure there are 4 digits in the integer
   return type.length() == 4 ? Integer.parseInt(type) : 0000;
}
[/CODE]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2011-06-08 13:29:04 »

I was thinking more something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
  private static final int WALL_BIT_R = 1<<0;
  private static final int WALL_BIT_U = 1<<1;
  private static final int WALL_BIT_L = 1<<2;
  private static final int WALL_BIT_D = 1<<3;
 
  public int getWallBits(int row, int col)
  {
    int r = 0;
   
    if (getType(row,  col+1) == WALL) r |= WALL_BIT_R;
    if (getType(row,  col-1) == WALL) r |= WALL_BIT_L;
    if (getType(row-1,col)   == WALL) r |= WALL_BIT_U;
    if (getType(row+1,col)   == WALL) r |= WALL_BIT_D;
   
    return r;
  }
 
  private static final int[] WALL_TYPE =
  {
    0 // FILL-ME IN, I'M TOO LAZY
 };
 
  public int getWallType(int row, int col)
  {
    return WALL_TYPE[getWallBits(row, col)];
  }


Your second code is pretty much the same, except that you're building a string to put each bit into position.  I'm simply predefining the bit positions.  Maybe you should read-up on bit-shifting operations.  Have fun!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!