Java-Gaming.org Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (612)
Games in Android Showcase (172)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2 3 ... 11
1  Game Development / Newbie & Debugging Questions / Re: Digging tiles out on: 2015-07-16 14:06:44
Hi,

Thanks guys.

For input listener do you mean implement InputListener and override the methods in there?

Thanks
2  Game Development / Newbie & Debugging Questions / Re: Digging tiles out on: 2015-07-16 13:46:18
Thanks for the advice...Opiop, yes, I should be doing as you say.

I can write clean code, this has just been put together rather quickly as don't have much time to 'design' it with having full-time job, I'm well aware of SOLID principles, design patterns etc, just not got time to do.





3  Game Development / Newbie & Debugging Questions / Re: How to render multiple map layers and make the map rendering more efficient on: 2015-07-16 10:08:54
Using Swing...

IMO need to change to using a good gfx lib such as LibGDX for your tile rendering.

4  Game Development / Newbie & Debugging Questions / Re: How to render multiple map layers and make the map rendering more efficient on: 2015-07-16 09:48:40
Are you drawing just the tiles that can be seen on the screen, or the whole map?
5  Game Development / Newbie & Debugging Questions / Re: Digging tiles out on: 2015-07-16 08:51:47
I use a top level class in my multidimensional array:

BlankEntity which has quite a few abstract methods in there of which other classes derive from, for instance WaterEntity, GrassEntity, CaveEntity - polymorphism used
here to make my code simple imo.  By having map made up of BlankEntities and derived ones makes it more workable and allows other things to be done going forward.

Think the problem in the code is the bRight / bLeft not being set correctly, they seem to go back to false...
6  Game Development / Newbie & Debugging Questions / Re: Digging tiles out on: 2015-07-16 08:47:41
Hi,

getBlock is just a simple get from the map array:

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  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
   public BlankEntity getBlock(int x, int y)
   {
      return worldMap[x][y];
   }
[code]

and here is the code for render which does the keyboard input:

   @Override
   public void render() {

      Vector3 poss = new Vector3(camera.position);
      poss.y-=8;

      // Need to check if not jumping as we need to jump Y amount before any downward force happens
       if(!bLeft && !bRight)  // this needs fixing as doesn't happen when moving right
      {
          if(checkCollision(poss))
         {
            camera.position.y -= 3;  // scroll map down
         }
      }
     
      int fps = Gdx.graphics.getFramesPerSecond();
           
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
       
      cloudBackground.render(0.05f, ambientShader);   // render clouds

      batch.setProjectionMatrix(camera.combined);

      if (Gdx.input.isKeyPressed(Input.Keys.W))
      {
         removeBlock(camera.position, bDown, bLeft, bRight);
      }
         
      if (Gdx.input.isKeyPressed(Input.Keys.R))
         camera.zoom = 1;
      if (Gdx.input.isKeyPressed(Input.Keys.MINUS))
         camera.zoom += 3.0f;
      if (Gdx.input.isKeyPressed(Input.Keys.I))
         camera.zoom -= 3.0f;
   
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
         bRight = false;
         bDown = false;
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.x-=2;//TILEWIDTH;
         po.y-=4;//TILEHEIGHT/2;
         if(checkCollision(po)) {
            camera.position.x -= 1;  // scroll map left
            bLeft = false;  
         }
         else // collision, move up
         {
            if(displayBlockDiagAbove(false,camera.position))
            {
               bLeft = true;
               camera.position.y += 2;
            } else
            {
               bLeft = false;
            }
         }
      }
     
      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
     
         bLeft = false;
         bDown = false;
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.y-=4;//TILEHEIGHT/2;
         
         if(checkCollision(po)) {        
            camera.position.x += 1; // scroll map right
            bRight = false;
         }
         else // collision - how do we go up - go up until no collision
         {  
            // is it clear above block? (water, cave, blank)
            if(displayBlockDiagAbove(true,camera.position))
            {
               bRight = true;            
               camera.position.y += 2; // scroll map up
            }
            else
            {
               bRight = false;
            }
         }
      }
     
      // We need to do a jump - TO DO
      if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
      }
      if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
         bDown = true;
         bRight = false; bLeft = false;
      }
      if (Gdx.input.isKeyPressed(Input.Keys.PAGE_UP)) {
         camera.position.y += 200; // jump map up
      }
      if (Gdx.input.isKeyPressed(Input.Keys.PAGE_DOWN)) {
         camera.position.y -= 200; // jump map down
      }
      if (Gdx.input.isKeyPressed(Input.Keys.D)) {
         debug = true; // turn debug on - basically draw the whole map
      }
      if (Gdx.input.isKeyPressed(Input.Keys.F)) {
         debug = false; // turn debug off
      }

      camera.position.set(MathUtils.roundPositive(camera.position.x),
            MathUtils.roundPositive(camera.position.y),0);
     
      camera.update();

      // Calculate what we should draw
      int camX = (int) camera.position.x - SCREENWIDTH / 2;
      camX /= TILEWIDTH;
      int endX = SCREENWIDTH / TILEHEIGHT + camX;

      int camY = (int) camera.position.y - SCREENHEIGHT / 2;
      camY /= TILEHEIGHT;
      int endY = SCREENHEIGHT / TILEHEIGHT + camY;

      batch.setShader(ambientShader);

      batch.begin();

     
      // Draw the map - only draw what camera sees
      worldMap.drawMap(debug, batch, regions, camX - 2, camY, endX + 2, endY,
            WORLDWIDTH, WORLDHEIGHT);

      batch.end();

      // HUD - Draw hud (in our case some text!)
      Matrix4 uiMatrix = hudCamera.combined.cpy();
      uiMatrix.setToOrtho2D(0, 0, SCREENWIDTH, SCREENHEIGHT);
      batch.setProjectionMatrix(uiMatrix);
      hudCamera.update();

     
      // Draw player after we have drawn the world
      player.render(0);

       
      batch.begin();
      drawCameraPosition(batch, camera.position);
      String pos = String.format("SteRraria V1.0");
      font.draw(batch, pos, 10, SCREENHEIGHT);

      this.drawFps(batch, fps);
     
      batch.end();
   }


[code]
   @Override
   public void render() {

      Vector3 poss = new Vector3(camera.position);
      poss.y-=8;

      // Need to check if not jumping as we need to jump Y amount before any downward force happens
       if(!bLeft && !bRight)  // this needs fixing as doesn't happen when moving right
      {
          if(checkCollision(poss))
         {
            camera.position.y -= 3;  // scroll map down
         }
      }
     
      int fps = Gdx.graphics.getFramesPerSecond();
           
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
       
      cloudBackground.render(0.05f, ambientShader);   // render clouds

      batch.setProjectionMatrix(camera.combined);

      if (Gdx.input.isKeyPressed(Input.Keys.W))
      {
         removeBlock(camera.position, bDown, bLeft, bRight);
      }
         
      if (Gdx.input.isKeyPressed(Input.Keys.R))
         camera.zoom = 1;
      if (Gdx.input.isKeyPressed(Input.Keys.MINUS))
         camera.zoom += 3.0f;
      if (Gdx.input.isKeyPressed(Input.Keys.I))
         camera.zoom -= 3.0f;
   
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
         bRight = false;
         bDown = false;
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.x-=2;//TILEWIDTH;
         po.y-=4;//TILEHEIGHT/2;
         if(checkCollision(po)) {
            camera.position.x -= 1;  // scroll map left
            bLeft = false;  
         }
         else // collision, move up
         {
            if(displayBlockDiagAbove(false,camera.position))
            {
               bLeft = true;
               camera.position.y += 2;
            } else
            {
               bLeft = false;
            }
         }
      }
     
      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
     
         bLeft = false;
         bDown = false;
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.y-=4;//TILEHEIGHT/2;
         
         if(checkCollision(po)) {        
            camera.position.x += 1; // scroll map right
            bRight = false;
         }
         else // collision - how do we go up - go up until no collision
         {  
            // is it clear above block? (water, cave, blank)
            if(displayBlockDiagAbove(true,camera.position))
            {
               bRight = true;            
               camera.position.y += 2; // scroll map up
            }
            else
            {
               bRight = false;
            }
         }
      }
     
      // We need to do a jump - TO DO
      if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
      }
      if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
         bDown = true;
         bRight = false; bLeft = false;
      }
      if (Gdx.input.isKeyPressed(Input.Keys.PAGE_UP)) {
         camera.position.y += 200; // jump map up
      }
      if (Gdx.input.isKeyPressed(Input.Keys.PAGE_DOWN)) {
         camera.position.y -= 200; // jump map down
      }
      if (Gdx.input.isKeyPressed(Input.Keys.D)) {
         debug = true; // turn debug on - basically draw the whole map
      }
      if (Gdx.input.isKeyPressed(Input.Keys.F)) {
         debug = false; // turn debug off
      }

      camera.position.set(MathUtils.roundPositive(camera.position.x),
            MathUtils.roundPositive(camera.position.y),0);
     
      camera.update();

      // Calculate what we should draw
      int camX = (int) camera.position.x - SCREENWIDTH / 2;
      camX /= TILEWIDTH;
      int endX = SCREENWIDTH / TILEHEIGHT + camX;

      int camY = (int) camera.position.y - SCREENHEIGHT / 2;
      camY /= TILEHEIGHT;
      int endY = SCREENHEIGHT / TILEHEIGHT + camY;

      batch.setShader(ambientShader);

      batch.begin();

     
      // Draw the map - only draw what camera sees
      worldMap.drawMap(debug, batch, regions, camX - 2, camY, endX + 2, endY,
            WORLDWIDTH, WORLDHEIGHT);

      batch.end();

      // HUD - Draw hud (in our case some text!)
      Matrix4 uiMatrix = hudCamera.combined.cpy();
      uiMatrix.setToOrtho2D(0, 0, SCREENWIDTH, SCREENHEIGHT);
      batch.setProjectionMatrix(uiMatrix);
      hudCamera.update();

     
      // Draw player after we have drawn the world
      player.render(0);

       
      batch.begin();
      drawCameraPosition(batch, camera.position);
      String pos = String.format("SteRraria V1.0");
      font.draw(batch, pos, 10, SCREENHEIGHT);

      this.drawFps(batch, fps);
     
      batch.end();
   }


Maybe I should introduce keys where you can move a rectangle over the block you want to remove?

Sorry, not got all the gfx etc in a repo.

Thanks
[/code][/code]
7  Game Development / Newbie & Debugging Questions / Re: Digging tiles out on: 2015-07-15 09:14:25
Thanks for that, but still pretty much the same.  When click right, block I remove is still removed from under player.
8  Game Development / Newbie & Debugging Questions / Digging tiles out on: 2015-07-14 20:26:17
Hi,

I have a 2d array containing all my map - made up of tiles.  I can dig down but struggling to dig left and right as using left and right keys just moves my
player.  I use W key at the moment to dig (remove a tile), but would like to be able to dig in the left right directions also...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   private void removeBlock(Vector3 position, boolean bDown, boolean bLeft, boolean bRight)
   {
      Vector3 screenSpace = toScreenSpace(position);
      int x = Math.round(screenSpace.x);
      int y = Math.round(screenSpace.y);
      if(bDown) y--;
      if(bRight) x+=1;
      if(bLeft) x-=1;
      BlankEntity entity = this.worldMap.getBlock(x, y);
      if(entity !=null && !entity.toString().equals("CAVE")
            && !entity.toString().equals("WATER")) {
         this.worldMap.removeEntity(x, y);
      }
   }


The method above is meant to know if you pressed the left, right or down key, these are stored in some booleans I have set up, but they are always false.

1  
2  
3  
4  
      if (Gdx.input.isKeyPressed(Input.Keys.W))
      {
         removeBlock(camera.position, bDown, bLeft, bRight);
      }


The code above just calls the removeBlock method.  What is the best way about going about removing blocks where the player 'digs'?

Thanks
9  Game Development / Newbie & Debugging Questions / Re: Tile Collision on: 2015-07-14 13:46:55
Got this working now :-)

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  
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
         bRight = false;

         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.x-=TILEWIDTH/2;
         po.y-=4;//TILEHEIGHT/2;
         if(checkCollision(po)) {
            camera.position.x -= 2;  // scroll map left
            bLeft = false;  
         }
         else // collision, move up
         {
            if(displayBlockDiagAbove(false,camera.position))
            {
               bLeft = true;
               camera.position.y += 3;
            } else
            {
               bLeft = false;
            }
         }
      }
     
      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
     
         bLeft = false;
         
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.y-=4;//TILEHEIGHT/2;
         
         if(checkCollision(po)) {        
            camera.position.x += 2; // scroll map right
            bRight = false;
         }
         else // collision - how do we go up - go up until no collision
         {  
            // is it clear above block? (water, cave, blank)
            if(displayBlockDiagAbove(true,camera.position))
            {
               bRight = true;            
               camera.position.y += 3; // scroll map up
            }
            else
            {
               bRight = false;
            }
         }
      }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   private boolean displayBlockDiagAbove(boolean dir, Vector3 position)
   {
      int val = dir == true ? 0 : -1;
      Vector3 screenSpace = toScreenSpace(position);
      int x = Math.round(screenSpace.x)+val;
      int y = Math.round(screenSpace.y)+1;
      BlankEntity entity = this.worldMap.getBlock(x, y);

      if(entity!=null)
      {  
         if(entity.toString().equals(".") || entity.toString().equals("CAVE")
               || entity.toString().equals("WATER") )
               
         {
            return true;
         }
      }
      else // entity is null
         return true;
           
      return false;
   }


The method displayBlockDiagAbove did the trick - as Springrbua suggested, check block diagnally above.

Thanks
10  Game Development / Newbie & Debugging Questions / Re: Tile Collision on: 2015-07-14 09:41:20
Thought it was working well, but now well collide say with a big wall, moves all the way up it, just want it to move up to a maximum amount...


       T
       BBBBBBBBBB
       BBBBBBBBBB
     PBBBBBBBBBB
BBBBBBBBBBBBBB

Above P (player) when pressing right would go all the way up the wall and end up at T, now what I want, just want to go up one block...
Would this require me to save the players position when collsion occured on right key?

Thanks
11  Game Development / Newbie & Debugging Questions / Re: Tile Collision on: 2015-07-13 20:12:43
Got this working with the code below (snippet):

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  
      Vector3 poss = new Vector3(camera.position);
      poss.y-=8;
      // Need to check if not jumping as we need to jump Y amount before any downward force happens
       if(!bRight)  // pressing right key, if so, don't check for collision downwards
      {
          if(checkCollision(poss))
         {
            camera.position.y -= 3;  // scroll map down
         }
      }
     

      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
         
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.y-=TILEHEIGHT/2;
         
         if(checkCollision(po)) {        
            camera.position.x += 2; // scroll map right
            bRight = false;
         }
         else // collision - how do we go up - go up until no collision
         {  
               bRight = true;            
               camera.position.y += 3; // scroll map up
         }
      }
   


and collision code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   private boolean checkCollision(Vector3 position)
   {
      Vector3 screenSpace = toScreenSpace(position);
      int x = Math.round(screenSpace.x);
      int y = Math.round(screenSpace.y);
      BlankEntity entity = this.worldMap.getBlock(x, y);
      if(entity ==null) return true;
         return false;
     
   }
   // Some helper methods
   private Vector3 toScreenSpace(Vector3 position) {
      Vector3 v = new Vector3(0, 0, 0);
      v.x = position.x / TILEWIDTH;
      v.y = position.y / TILEHEIGHT;
      return v;
   }


This now works :-)  Still to put jumping in then digging and some lighting...
12  Game Development / Newbie & Debugging Questions / Tile Collision on: 2015-07-13 16:16:25
Hi,

I got my tile collision working in my sandbox game, but when a player is say moving right and hits a block/platform, I'd like him to automatically move up this block like in Terraria.

                            BBBBB
                     PBBBB
BBBBBBBBBBBBB

B is block, P is player, player has hit block, and they continue pressing right key so now want them to go up the block onto the platform without having to jump, hope this makes sense?

Thanks
13  Game Development / Newbie & Debugging Questions / Re: Where to start? on: 2015-07-07 20:32:08
Hi,

I'd look at one of the libGdx books - libGdx sits 'above' OpenGL so to speak (hides the complexity of direct calls to OpenGL) and makes for fast rendering needed in games unlike Java2D/Swing/JavaFx.

If you are competent at Java then libGdx is a good way to go and the books on this will help you tremendously as well as the JGO forums.

Good luck.
14  Game Development / Newbie & Debugging Questions / Re: Animation on: 2015-07-07 19:53:24
Hi,

No, when you click key to move left, just do walk animation for left.

Yes, what you had done previously looks like what I'm looking for.  I'm taking it the Animation objects are made up from
the texture regions, like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
 playerTextures = new Texture(textureFile);
 TextureRegion[][] tmp = TextureRegion.split(playerTextures, playerTextures.getWidth()/FRAMECOLS,   playerTextures.getHeight()/FRAMEROWS);              
        walkFrames = new TextureRegion[FRAMECOLS * FRAMEROWS];
     
        int index = 0;
        for (int i = 0; i < FRAMEROWS; i++) {
            for (int j = 0; j < FRAMECOLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }
        walkAnimation = new Animation(TIMER, walkFrames);


Thanks
15  Game Development / Newbie & Debugging Questions / Re: Animation on: 2015-07-07 15:50:53
Hi,

Thanks for that.  What I was meaning is, when player presses right key for instance, the player moves right and walk animation is done :-)

16  Game Development / Newbie & Debugging Questions / Re: Animation on: 2015-07-07 15:13:46
Thanks for that,


Exactly what I was doing without having the atlas :-) 

What is best way to update the frame when user presses a key - I've been doing this:

1  
2  
3  
4  
   currentFrame = walkAnimation.getKeyFrame(TIMER, true);
      if(TIMER > AMOUNTFRAMES) TIMER= 0.0f;
      else
         TIMER+=0.1f;


Thanks
17  Game Development / Newbie & Debugging Questions / Re: Animation on: 2015-07-07 14:25:47
Just going to use a spritesheet unpacker then feed these into texturepacker to create an atlas file, failing that
I could just type the atlas file up myself...

You mean, load sprite sheet and use the split method?  Or manually cut the sprites up and store in a List?

Something like:

1  
2  
3  
   playerTextures = new Texture(textureFile);
   
   TextureRegion[][] tmp = TextureRegion.split(playerTextures, playerTextures.getWidth()/FRAMECOLS, playerTextures.getHeight()/FRAMEROWS);              

18  Game Development / Newbie & Debugging Questions / Animation on: 2015-07-07 11:58:49
Hi,
I have a sprite sheet already but no atlas file - can you create an atlas file from a sprite sheet?

Just wondering what is the simplest way of implementing walking animation when player presses a key.

Thanks
19  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-30 08:56:45
Cheers Jesse,

Couldn't have done it without your help!

Looking to do some lighting next...not sure of which way to go with this though, looked at using a shader.

Thanks
20  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-29 13:58:49
Iterative flood fill working a treat and so much faster to render the world with!

Please see here for latest screen shot:

https://sites.google.com/site/sterrialand/development/news/iterativefloodfillimplemented

The code:

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  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
package com.mygdx.game;

import java.awt.Point;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class FloodFill {

   BlankEntity[][] map;

   private CaveCell cell;

   public ArrayList<CaveCell> caveCells = new ArrayList<CaveCell>();

   public FloodFill() {
   }

   public FloodFill(BlankEntity[][] map) {
      this.map = map;
   }

   /*
    * Recursive flood fill - CANNOT handle large caves
    */

   public void fill(int x, int y) {

      try {
         if (x < 0)
            return;
         if (y < 0)
            return;
         if (x >= map.length)
            return;
         if (y >= map[x].length)
            return;

         BlankEntity currentTile = map[x][y]; // get current tile
         if (currentTile == null)
            return;

         if (currentTile.visited)
            return;

         if (!(currentTile instanceof CaveEntity))
            return;

         currentTile.visited = true;

         cell = new CaveCell(x, y);
         caveCells.add(cell);

         fill(x - 1, y);
         fill(x + 1, y);
         fill(x, y - 1);
         fill(x, y + 1);
      } catch (Exception ex) {
         System.out.println(":-( Cave too large");
      }
   }

   
    /*
     * Iterative Flood fill - stops stack overflow :-)
     */

   public void iterativeFill(int initialX, int initialY) {
      Queue<Point> points = new LinkedList<Point>();
      points.add(new Point(initialX, initialY));
      caveCells = new ArrayList<CaveCell>();
     
      while (!points.isEmpty()) {
         Point currentPoint = points.remove();
         int x = currentPoint.x;
         int y = currentPoint.y;

         BlankEntity currentTile = map[x][y]; // get current tile
   
         if(currentTile instanceof CaveEntity && !currentTile.visited)
         {
            cell = new CaveCell(x, y);
            caveCells.add(cell);
            currentTile.visited = true;
           
            if (x < map.length - 1 )
               points.add(new Point(x + 1, y));
            if (x > 0)
               points.add(new Point(x - 1, y));
            if (y < map[x].length -1)
               points.add(new Point(x, y + 1));
            if (y > 0)
               points.add(new Point(x, y - 1));
         }
      }
   }
}


class CaveCell {
   public int x, y;

   public CaveCell(int x, int y) {
      this.x = x;
      this.y = y;
   }

   @Override
   public String toString() {
      return "X:" + x + " Y:" + y;
   }
}

class Cave {
   public Cave() {
      cells = new ArrayList<CaveCell>();
   }

   public ArrayList<CaveCell> cells;
}


21  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-25 16:31:53
Thanks Riven,

Makes sense :-)
22  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-24 09:01:46
Hi,

Yeah, seems to work, gives me a list of caves - this is used to allow water to be added to the caves later.

Been on hols for 2 weeks so only just getting back to this!

No caves are the same due to the visited property :-)

Thanks
23  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-23 21:56:25
Well, this is what I have for an iterative flood fill, does this look correct?...

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  
public void iterativeFill(int initialX, int initialY) {
      Queue<Point> points = new LinkedList<Point>();
      points.add(new Point(initialX, initialY));
      caveCells = new ArrayList<CaveCell>();
     
      while (!points.isEmpty()) {
         Point currentPoint = points.remove();
         int x = currentPoint.x;
         int y = currentPoint.y;

         BlankEntity currentTile = mm[x][y]; // get current tile
   
         if(currentTile instanceof CaveEntity && !currentTile.visited)
         {
            cell = new CaveCell(x, y);
            caveCells.add(cell);
            currentTile.visited = true;
           
            if (x < mm.length - 1 )
               points.add(new Point(x + 1, y));
            if (x > 0)
               points.add(new Point(x - 1, y));
            if (y < mm[x].length -1)
               points.add(new Point(x, y + 1));
            if (y > 0)
               points.add(new Point(x, y - 1));
         }
      }

   }


Where caveCells is an ArrayList of cells for particular cave.  The above is called as:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
FloodFill f = new FloodFill();

      for (int x = 0; x < mm.length; x++) {
         for (int y = 0; y < mm[x].length; y++) {
            f.iterativeFill(x, y);
            ArrayList<CaveCell> cavec = FloodFill.caveCells;
            if (caveCells.size() > 0) {
               Cave c = new Cave();
               for (CaveCell cell : caveCells) {
                  c.cells.add(cell);
               }

               caves.add(c);
               System.out.println("Added to caves arraylist.\n");
            }
         }
      }


Thanks
24  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-14 21:30:04
Thanks Jesse,

Yes, using the Point object so can get x,y incremented.  I've got this now:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   public void iterativeFill(int initialX, int initialY, char target, char replacement){
       Queue<Point> points = new LinkedList<Point>();
       points.add(new Point(initialX, initialY));

       while(!points.isEmpty()) {
           Point currentPoint = points.remove();
           int x = currentPoint.x;
           int y = currentPoint.y;

           char current = m[x][y];
           if((current == target)){
               m[x][y] = replacement;
               if(x< m.length - 1)
                  points.add(new Point(x+1, y));
               if(x>0)
                  points.add(new Point(x-1, y));
                    if(y<m[x].length)
                   points.add(new Point(x, y+1));
               if(y>0)
                  points.add(new Point(x, y-1));
           }
       }
   }


Will need to put in the usual visited boolean like had before and then a loop:

1  
2  
3  
      for (int x = 0; x < m.length; x++) 
         for (int y = 0; y < m[x].length; y++)
           fill.IterativeFill(x,y,'c','w');


Think this could work, doing it this way before changing it to the 2d array in my game which you know is of BlankEntity objects.
25  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-14 21:11:29
Did try something like this using Point object:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void BoundaryFill(int initialX, int initialY, char target, char replacement){
       Stack<Point> points = new Stack<Point>();
       points.add(new Point(initialX, initialY));

       while(!points.isEmpty()) {
           Point currentPoint = points.pop();
           int x = currentPoint.x;
           int y = currentPoint.y;

           char current = m[x][y];
           if((current == target)){
               m[x][y] = replacement;
           
               points.push(new Point(x+1, y));
               if(x>0)
                  points.push(new Point(x-1, y));
               points.push(new Point(x, y+1));
               if(y>0)
                  points.push(new Point(x, y-1));
           }
       }
   }


Still only does up to x being 2 then exits
26  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-14 20:59:45
Hey Jesse,

Yes you are correct, the x & y don't change.  I'm taking it you would call this method once and it would do the whole array? 

Thus fill(0,0,'c','w'); 

is all that would be needed?

Thanks
27  Game Development / Newbie & Debugging Questions / Iterative flood fill isn't correct - thought it was working?! on: 2015-06-14 20:40:55
Hi,

I thought my iterative flood fill was working ok - obviously just a red herrin!

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  
private void fill(int x, int y, char target, char replace) {
      if (m[x][y] == replace)
         return; // current is same as node - 1

      Queue<Character> q = new LinkedList();

      q.add(m[x][y]);

      while (!q.isEmpty()) {

         q.remove();

         if (m[x][y] != replace && m[x][y] == target)
         {
            m[x][y] = replace; //

            if (x < m.length - 1)
               if (m[x + 1][y] != replace)   // east
                  q.add(m[x + 1][y]);
            if (x > 0)
               if (m[x - 1][y] != replace)   // west
                  q.add(m[x - 1][y]);

            if (y < m[x].length - 1)
               if (m[x][y + 1] != replace)   // north
                  q.add(m[x][y + 1]);
            if (y > 0)
               if (m[x][y - 1] != replace)   // south
                  q.add(m[x][y - 1]);
         }
      }
   }


The above code can just be:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private void fill(int x, int y, char target, char replace) {
      if (m[x][y] == replace)
         return; // current is same as node - 1

      Queue<Character> q = new LinkedList();

      q.add(m[x][y]);

      while (!q.isEmpty()) {

         q.remove();

         if (m[x][y] != replace && m[x][y] == target)
            m[x][y] = replace;
      }
}



1  
2  
3  
4  
5  
6  
7  
   public static void main(String[] args) {
      FloodFill f = new FloodFill();
      for (int x = 0; x < m.length; x++)
         for (int y = 0; y < m[x].length; y++)
            f.fill(x, y, 'c', 'w');
                       
               


Where m is a 2d array of chars:

1  
2  
3  
4  
5  
6  
7  
   static char[][] m = { 
          { 'c', 'x', 'x', 'c', 'c' },
         { 'c', 'x', 'x', 'c', 'x' },
         { 'x', 'c', 'c', 'c', 'x' },
         { 'x', 'c', 'x', 'x', 'c' },
         { 'x', 'c', 'c', 'c', 'c' },
   };


The queue is doing nothing...anything obvious wrong?  All the code I've written is just replacing the character c with W.

Thanks.
28  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-12 12:15:47
Got iterative flood fill to work with some test 2d char array but in my game code, only ever produces 3 cells for each cave?

Here is the fill method:

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 void iterativeFill(int x, int y) {
      BlankEntity currentTile = map[x][y]; // get current tile
      if (currentTile == null)
         return;

      if (currentTile.visited)
         return;

      if (!(currentTile instanceof CaveEntity))
         return;

      currentTile.visited = true;

      Queue<BlankEntity> q = new LinkedList();

      q.add(map[x][y]);

      while (!q.isEmpty()) {

         BlankEntity e = q.remove();

         cell = new CaveCell(e.x, e.y);

         caveCells.add(cell);

         if (x < map.length - 1)
            if (map[x + 1][y] instanceof CaveEntity) {
               if (!map[x + 1][y].visited) {
                  q.add(map[x + 1][y]);
                  map[x + 1][y].visited = true;
               }
            }

         if (x > 0)
            if (map[x - 1][y] instanceof CaveEntity) {

               if (!map[x - 1][y].visited) {
                  q.add(map[x - 1][y]);
                  map[x - 1][y].visited = true;
               }
            }

         if (y < map[x].length - 1)
            if (map[x][y + 1] instanceof CaveEntity) {
               if (!map[x][y + 1].visited) {
                  q.add(map[x][y + 1]);
                  map[x][y + 1].visited = true;
               }
            }

         if (y > 0)
            if (map[x][y - 1] instanceof CaveEntity) {
               if (!map[x][y - 1].visited) {
                  q.add(map[x][y - 1]);
                  map[x][y - 1].visited = true;
               }
            }
      }

   }


Called up:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
for(int x=0; x<worldMap.length;x++)
      {
         for(int y=0;y<worldMap[x].length;y++)
         {
            fFill = new FloodFill(worldMap);
            fFill.iterativeFill(x,y);
            ArrayList<CaveCell> cavec = fFill.caveCells;
            if(cavec.size() > 0)
            {
               Cave c = new Cave();
               for(CaveCell cell : cavec)
               {
                  c.cells.add(cell);
               }
               
               this.caves.add(c);
            }
         }
      }


I'm sure x and y need incrementing or something in the iterative fill method?

Any ideas?

Thanks
29  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-12 10:02:43
Now works, see modifications below :-)

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  
package com.mygdx.game;

import java.util.LinkedList;
import java.util.Queue;

public class IterativeFloodFill {

   static char[][] m = {
         {'c','x','x','c','c' },
         {'c','x','x','c','x' },
         {'x','c','x','c','x' },
         {'x','c','x','c','c' },
         {'x','c','c','c','c'},      
   };
   
   
   private static void fill(int x, int y, char target, char replace)
   {
      if(m[x][y] == replace) return;  // current is same as node - 1
     
      Queue<Character> q = new LinkedList();
         
      q.add(m[x][y]);  
   
      while(!q.isEmpty()) {  
         
         q.remove();
         
         if( m[x][y]!= replace && m[x][y]==target) // 7
         {
            m[x][y] = replace;  //
            if(y<m[x].length-1)
            q.add(m[x][y+1]);
            if(y>0)
               q.add(m[x][y-1]);
            if(x<m.length-1)
               q.add(m[x+1][y]);            
            if(x>0)
               q.add(m[x-1][y]);
         }
      }
     
   }
   
   
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      for(int x=0; x<m.length;x++)
         for(int y=0;y<m[x].length;y++)
            fill(x,y,'c','w');

   
      for(int x=0; x<m.length;x++,System.out.println())
         for(int y=0;y<m[x].length;y++)
            System.out.print(m[x][y]);

   }

}

30  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-12 09:57:53
Hi Riven,

Modified it, this look ok (see previous post)?  But as stated, cannnot start from 0 and cannot go to max width, height of array, has to be minus 1...
Pages: [1] 2 3 ... 11
 
Andrew_3ds (19 views)
2015-09-01 19:08:10

afikri (15 views)
2015-08-31 09:30:22

afikri (23 views)
2015-08-31 09:30:07

afikri (11 views)
2015-08-31 09:27:24

afikri (15 views)
2015-08-31 09:26:40

Roquen (20 views)
2015-08-29 11:30:54

GamerC4 (33 views)
2015-08-22 20:38:50

GamerC4 (29 views)
2015-08-22 20:37:18

GamerC4 (34 views)
2015-08-22 20:37:01

Kefwar (43 views)
2015-08-22 18:07:24
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30
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!