Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  Help on tiles, in my game !  (Read 1162 times)
0 Members and 1 Guest are viewing this topic.
Offline PixelDeBurner

Junior Member


Medals: 1



« Posted 2012-08-10 16:31:58 »

Hi. I'm trying to recreate mojang's Catacomb Snatch, in slick... so I can learn a bit more about game programming. And I have a few problems right now.

I created Tile classes ( Tile, WallTile, FloorTile etc ). Also created Level Class to setTile( initialize tile ).. and other stuff. Right now, in my Play class, I'm creating tiles based on color from level image, and store them in array Tile[][]. Then in floortile class I need to determine does the tile next to it cast shadow, if so, set the floortile to be the one with shadow. It works picking up left and top tile from level, but right tile doesn't. Here is the code :

Play class stuff :
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  
tiles = new Tile[level.getWidth()][level.getHeight()];

      for (int y = 0; y < level.getHeight(); y++) {
         for (int x = 0; x < level.getWidth(); x++) {
            int col;

            col = level.getColor(x, y);

            if (col == 0xFF0000)
               tile = new WallTile();
            else if (col == 0x000000) {
               if (y > 0) {
                  if (tiles[x][y - 1] instanceof FloorTile) {
                     tile = new EdgeTile();
                  } else
                     tile = new HoleTile();
               }
            } else if (col == 0xff7777)
               tile = new TreasureTile();
            else
               tile = new FloorTile();

            tiles[x][y] = tile;
            level.setTile(tiles, x, y);
         }
      }


FloorTile:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void init(Level level, int x, int y) {
      super.init(level, x, y);
      shadowateTile();
   }

   private void shadowateTile() {
      Tile w = level.getTile(x - 1, y);
      Tile n = level.getTile(x, y - 1);
      Tile s = level.getTile(x, y + 1);
      Tile e = level.getTile(x + 1, y);

      tile = randomTile(floortiles, 4, TileSize, TileSize, 0, 0);

      if (w != null && w.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 64, 32);
      if (n != null && n.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 32, 64);
      if(e != null && e.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 96,32);
   }

   public void render() {
      tile.draw(x * 32, y * 32);
   }


Level class :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void setTile(Tile[][] tiles, int x, int y) throws SlickException {
      this.tiles = tiles;
      this.tiles[x][y].init(this, x, y);
   }

   public Tile getTile(int x, int y) {
      if (x < 0 || y < 0 || x > 47 || y > 47)
         return null;
      return tiles[x][y];
   }


I saw mojang team uses different way of setting the tiles, like Tile[x + y * width]; ... I don't know how to set that up unfortunately...
Offline jonjava
« Reply #1 - Posted 2012-08-10 18:43:42 »

I don't know this game you're speaking of or what you're trying to do. But I can explain what the mysterious 'Tile[x + y * width]' is.

1  
Tile[x][y] == Tile[x + y * width]


These are the same thing if we assume they have the same width. Is it better to use one over each other? No, it's all a matter of preference - I think the latter approach just stuck on since in early C (programming language) IIRC you HAD to use it since you couldn't make multi-dimentional arrays straight up but had to juggle it with a long array.

Consider A) and B)

1  
2  
3  
4  
5  
6  
7  
8  
A:
[0][1][2]
[3][4][5]
[6][7][8]

width = 3;
height = 3;
size = 9;


1  
2  
3  
B:
[0][1][2][3][4][5][6][7][8]
size = 9;


If we consider B with the same length as A (I.e 3) then

1  
A[0][1] == [0 + 1 * width] == 3

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #2 - Posted 2012-08-10 21:08:26 »

Oh ok. The thing I want to achieve is to get the north, west and east tile, and see if they cast shadow, if so put the shadow tile next to them. It works for left one, and top one, but the right one ( east - e tile ) doesn't work, and I don't know why...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #3 - Posted 2012-08-10 22:27:35 »

To make it a little bit more clear, in every Tile class, there is method castShadow() which simply returns true or false.
Offline MashedPotatoe

Senior Newbie





« Reply #4 - Posted 2012-08-11 06:02:21 »

Since I don't know how cutTile() works exactly I just take a guess looking at your paramaters:

1  
2  
3  
4  
5  
6  
if (w != null && w.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 64, 32); // last 2 parameters are x and y?
if (n != null && n.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 32, 64);
if(e != null && e.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 96,32); // 96 seems wrong looking at the other parameters, should be 0 if I follow the logic for the other two


Maybe I'm completely off. Hard to tell since I can't see the code for it and it seems to work for the other two tiles.
Offline 65K
« Reply #5 - Posted 2012-08-11 06:33:02 »

Already started a debugger and stepped through the processing of eastern tiles ?

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #6 - Posted 2012-08-11 12:12:35 »

cutTile gets the specific tile from image, last 2 paramateres are x and y, the 64, 32, is not based really logicaly, it's coincidence Smiley

Here you go full project, so you can see the pictures and stuff :

Slick Snatch


The problem is, that it seems to not return me tiles[x+1][y].
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #7 - Posted 2012-08-11 12:36:05 »

Also, I just found out that it doesn't work if I add on the x or y position... so x + 1 or y + 1 doesn't work, but x - 1, or y - 1 work...

This is really weird for me.
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #8 - Posted 2012-08-11 16:33:34 »

Unbelievable !

For some reason level.getTile(x + 1, y - 1) works, but not the way I want it to ( makes tiles below wall also ), but level.getTile(x + 1, y) will not work, not at all, it even says there is no tile at that position...
Offline Best Username Ever

Junior Member





« Reply #9 - Posted 2012-08-11 16:45:37 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
   private void shadowateTile() {
      Tile w = level.getTile(x - 1, y);
      Tile n = level.getTile(x, y - 1);
      Tile s = level.getTile(x, y + 1);
      Tile e = level.getTile(x + 1, y);

      Tile here = level.getTile(x, y);
      System.out.println(x + ", " + y);
      System.out.println(n + " " + e + " " + s + " " + w);
      System.out.println(here == this);
      System.out.println();

      tile = randomTile(floortiles, 4, TileSize, TileSize, 0, 0);

      if (w != null && w.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 64, 32);
      if (n != null && n.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 32, 64);
      if(e != null && e.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 96,32);
   }


What does this print?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #10 - Posted 2012-08-11 17:19:06 »

I think I almost got the error. If I try to use shadowate tile method, in the tile that is just created, then it tries to sample tile from ahead of it, which is still not created !
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #11 - Posted 2012-08-11 17:21:46 »

Yeah, I've just been reading through this, and was about to say just that.
You should do these things separately.

First create the tiles from the image, then initialize them all.

- Jonas
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #12 - Posted 2012-08-11 22:12:26 »

Got it to work ( using some mojang's code ) !
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.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!