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  
  Tile Maps : how do they works ?  (Read 425 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-05-04 03:54:04 »

Hello,
am currently using lwjgl, but i think my question has nothing to do with it so i didn't mention that in the title
anyway...
am trying to start with a little platformer game and i think a tiled map is something necessary to do that (even if it's not, i want to know how it works ) 
so i was trying to do some codes for the last 2 hours, and i managed to :
    - draw the map grid based on the "block" size and type
    - print out the x and y coordinate of the mouse based on its position on the grid
what am stacked with :
    -draw and KEEP a specific type if "blocks" in the clicked position (it appears in the wrong location)
what i want to know :
    -as a start, is this a good way to do it ?   
         
 codes here :

This class will handle the map creation
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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
public class World {

   private Block[][] blocks;
   private Block block;
   private int width, height;

   public World(int width, int height) {

      this.width = width;
      this.height = height;

      blocks = new Block[width][height];

   }

   public void setWorld() {
      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height; y++) {
            Block block = new Block(0, 0, 32, 0);
            block.setX(x * block.getSize());
            block.setY(y * block.getSize());
            block.draw();
            blocks[x][y]=block;

         }
      }
   }
   
   public void drawInWorld(int x,int y){
      Block block = new Block(0, 0, 32, 1);
      block.setX(x * block.getSize());
      block.setY(y * block.getSize());
      block.draw();
   
   }

   public int getWidth() {
      return width;
   }

   public int getHeight() {
      return height;
   }

}


this class will handle a single block creation
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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
public class Block {

   private int x, y, size, type;
   private Drawing draw = new Drawing();
   public Block(int x, int y, int size, int type) {
      super();
      this.x = x;
      this.y = y;
      this.size = size;
      this.type = type;
   }

   public void draw(){
     
      if(type==0){
         draw.coloring("blue");
      }else{
         draw.coloring("red");
      }
     
      draw.drawRect(x, y, size, size);
   }
   
   public int getX() {
      return x;
   }

   public void setX(int x) {
      this.x = x;
   }

   public int getY() {
      return y;
   }

   public void setY(int y) {
      this.y = y;
   }

   public int getSize() {
      return size;
   }

   public void setSize(int size) {
      this.size = size;
   }

   public int getType() {
      return type;
   }

   public void setType(int type) {
      this.type = type;
   }

}


this is the Board class (logic,calling etc...)
PS :
i removed the player method to make it easiest to follow
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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
 

import java.util.Arrays;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class Board {
   // varibable of other classes
 
   private Loop loop = new Loop(); // the loop class
  private World world;

   int delta;

   public void init() {
      // initialize the loop
     loop.init();
      world = new World(25,19);
     
   }

   // the main game logic "caller"
  public void myBoardLoop() {
      delta = loop.getDelta();
     
      update(delta);
   }

   // the update method
  private void update(int delta) {
      input();
      painting();
     
      loop.updateFPS();

   }

   private void painting() {
      // clear the screen and depth buffer
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
     
     
      world.setWorld();
   }


   private void input(){
      int mouseX = Mouse.getX();
      int mouseY = Mouse.getY();
      boolean mouseClick = Mouse.isButtonDown(0);
      if(mouseClick){
         int mapX = Math.round(mouseX/32);
         int mapY = Math.round(mouseY/32);
         System.out.println(mapX+"||"+mapY);
         world.drawInWorld(mapX, mapY);
      }
   }
}


thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline 65K
« Reply #1 - Posted 2013-05-04 06:33:41 »

Hello,
am currently using lwjgl, but i think my question has nothing to do with it so i didn't mention that in the title
anyway...
how tile maps work
SCNR

You recreate the whole world in each rendering pass. Set up the world once and before.
What is block for int class World ?
setWorld should rather be called createWorld and not draw blocks - and there is drawInWorld
What is Block member drawing, why does each Block has one ?
To check wrong click positions, start with the origin at 0,0, verify output, then move on slowly and see how it behaves.
Instead of determining Block colors by type, set the color directly or derive sub classes with different visual appearance. Keeping modes in classes is not good and not OOP.

Offline alaslipknot
« Reply #2 - Posted 2013-05-04 17:51:42 »

alright so i change the code and it gave me this :


the gray and blue squares are token from a string array (check the code below)
they yellow square is the correct position of the mouse when i click in a gray field
the green square is the correct position of the mouse when i click in a blue field 

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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
public class World {

   private Block[][] blocks;
   private Block block;
   private int width, height;
   private int[][] multi = new int[10][10];

   private String[][] stringArray = {
         { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "0", "0", "#", "#", "#", "#", },
         { "#", "#", "#", "#", "0", "0", "#", "#", "#", "#", },
         { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", },

   };

   public World(int width, int height) {

      this.width = width;
      this.height = height;

      blocks = new Block[width][height];

   }

   public void setWorld() {
      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height; y++) {
            Block block = new Block(0, 0, 32, 0);
            block.setX(x * block.getSize());
            block.setY(y * block.getSize());
            if (stringArray[y][x] == "0") {
               drawInWorld(x, y, 0);
            } else {
               drawInWorld(x, y, 1);
            }
         }
      }
   }

   public void drawInWorld(int x, int y, int type) {
      Block block = new Block(0, 0, 32, type);
      block.setX(x * block.getSize());
      block.setY(y * block.getSize());
      block.draw();

   }

   public String[][] getWorld() {

      return stringArray;

   }

}


so now i think the creation part is done(if am using a wrong method please correct it, am doing it all by myself without looking to any example or tutorials so the chance of doing it wrong is VERY high )   

now the hardest part is collision detection with other game elements (the player for now )
i start by doing something like this
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private void heroCollision() {

      int tmpHeroX = (int) heroX;
      int tmpHeroY = (int) (Game.HEIGHT - heroY - 1);

      int heroTx = Math.round(tmpHeroX / 32);
      int heroTy = Math.round(tmpHeroY / 32);
      // this print the correct position of the player in the grid
     //System.out.println(heroTx + "|" + heroTy);
     // if it's on the ground
     if (myWorld[heroTy + 1][heroTx] == "0") // +1 to make it stop before get
                                   // into the grid case
     {
         System.out.println("you are on the ground ");
      }
   }

and it works, it can tell that the player is on the ground or not
but the problem is this isn't a correct collision detection, or at least not the way i learned how to do it,
so what i want to ask now, is if there is a way to use the AABB collision detection (i already build a method that have in parameter (x1,y1,width1,height1,x2,y2,width2,height2) with what i achieved till now ??
so in fact what i clearly want is access the x and y value of each block from the grid .

in case you needed the Block class
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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
public class Block {

   private int x, y, size, type;
   private Drawing draw = new Drawing();

   public Block(int x, int y, int size, int type) {
      super();
      this.x = x;
      this.y = y;
      this.size = size;
      this.type = type;
   }

   public void draw() {

      if (type == 0) {
         draw.coloring("blue");
      } else if (type == 1) {
         draw.setColor(0.5f, 0.5f, 0.5f);
      } else if (type == 2) {
         draw.setColor(0.5f, 0.9f, 0.2f);
      }

      else {
         draw.setColor(0.9f, 0.9f, 0.5f);
      }

      draw.drawRect(x, y, size, size);
   }

   public int getX() {
      return x;
   }

   public void setX(int x) {
      this.x = x;
   }

   public int getY() {
      return y;
   }

   public void setY(int y) {
      this.y = y;
   }

   public int getSize() {
      return size;
   }

   public void setSize(int size) {
      this.size = size;
   }

   public int getType() {
      return type;
   }

   public void setType(int type) {
      this.type = type;
   }

}



thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
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 (12 views)
2014-07-29 18:09:19

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

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

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

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

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

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

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

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

ctomni231 (59 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!