Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Converting ugly method to a for loop *solved*  (Read 704 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2013-02-02 23:23:56 »

Hey JGO. Cheesy
A few months back I spent a decent amount of time in JMonkeyEngine, now after trying a few things in LWJGL via code I've written I've decided it would be easier for me to produce a quality game via JMonkeyEngine.

While the issue I'm having doesn't have any relevance to JME, it is relevant to Java, that's why I figured I'd post this question here instead of the 3rd party engines board.

What I'm doing
:
Rendering a grid of 25x20 (Volume: 1) cubes, same height level for all just different depth and x axis offsets.

Notes:
The variables x1, x2, x3 etc are just used so after 25 cubes are looped through, their offsets starting value is reset to zero.
(So when it's incremented by the width / depth of the cube, the cube doesn't start with a improperly offset position)
It works Grin Grin, but I'd like to throw it into a simple for loop, I just don't know how I'd write it at the moment.
I'm stumped, hoping somebody could help.

How it currently looks Clueless:
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  
      /*
       * Add a new Box to the list of floorBoxes.
       * Add a new Geometry to the list of Geometry's for the box.
       * Set the floorBoxes material.
       * Attach the floorBoxes geometry the terrain's static geometry node.
       * Position each Box properly.
       */

      for (int i = 0; i < floorCubes; i++) {
         floorBoxes.add(new Box(Vector3f.ZERO, new Vector3f(floorCubeWidth, floorCubeHeight, floorCubeDepth)));
         floorGeoms.add(new Geometry("Box " + i, floorBoxes.get(i)));
         floorGeoms.get(i).setMaterial(floorMaterial);
         floorNode.attachChild(floorGeoms.get(i));
         if (i < 25) {
            floorGeoms.get(i).setLocalTranslation(i, 0, 0);
         } else if (i > 24 && i < 50) {
            floorGeoms.get(i).setLocalTranslation(x1++, 0, -floorCubeDepth);
         } else if (i > 49 && i < 75) {
            floorGeoms.get(i).setLocalTranslation(x2++, 0, -floorCubeDepth * 2);
         } else if (i > 74 && i < 100) {
            floorGeoms.get(i).setLocalTranslation(x3++, 0, -floorCubeDepth * 3);
         } else if (i > 99 && i < 125) {
            floorGeoms.get(i).setLocalTranslation(x4++, 0, -floorCubeDepth * 4);
         } else if (i > 124 && i < 150) {
            floorGeoms.get(i).setLocalTranslation(x5++, 0, -floorCubeDepth * 5);
         } else if (i > 149 && i < 175) {
            floorGeoms.get(i).setLocalTranslation(x6++, 0, -floorCubeDepth * 6);
         } else if (i > 174 && i < 200) {
            floorGeoms.get(i).setLocalTranslation(x7++, 0, -floorCubeDepth * 7);
         } else if (i > 199 && i < 225) {
            floorGeoms.get(i).setLocalTranslation(x8++, 0, -floorCubeDepth * 8);
         } else if (i > 224 && i < 250) {
            floorGeoms.get(i).setLocalTranslation(x9++, 0, -floorCubeDepth * 9);
         } else if (i > 249 && i < 275) {
            floorGeoms.get(i).setLocalTranslation(x10++, 0, -floorCubeDepth * 10);
         } else if (i > 274 && i < 300) {
            floorGeoms.get(i).setLocalTranslation(x11++, 0, -floorCubeDepth * 11);
         } else if (i > 299 && i < 325) {
            floorGeoms.get(i).setLocalTranslation(x12++, 0, -floorCubeDepth * 12);
         } else if (i > 324 && i < 350) {
            floorGeoms.get(i).setLocalTranslation(x13++, 0, -floorCubeDepth * 13);
         } else if (i > 349 && i < 375) {
            floorGeoms.get(i).setLocalTranslation(x14++, 0, -floorCubeDepth * 14);
         } else if (i > 374 && i < 400) {
            floorGeoms.get(i).setLocalTranslation(x15++, 0, -floorCubeDepth * 15);
         } else if (i > 399 && i < 425) {
            floorGeoms.get(i).setLocalTranslation(x16++, 0, -floorCubeDepth * 16);
         } else if (i > 424 && i < 450) {
            floorGeoms.get(i).setLocalTranslation(x17++, 0, -floorCubeDepth * 17);
         } else if (i > 449 && i < 475) {
            floorGeoms.get(i).setLocalTranslation(x18++, 0, -floorCubeDepth * 18);
         } else if (i > 474 && i < 500) {
            floorGeoms.get(i).setLocalTranslation(x19++, 0, -floorCubeDepth * 19);
         }
      }


Looking forward to any feedback / hints on how I can convert this / clean it up, thanks JGO.

Offline HeroesGraveDev

JGO Kernel


Medals: 259
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-02-02 23:28:11 »

I can't look at that properly. It hurts my eyes.

Try using the modulus operator.

Offline GabrielBailey74
« Reply #2 - Posted 2013-02-02 23:30:28 »

XD, I was in pain while writing it O.O 500 cubes lol.
I'll go look into implementing the Modulus Operator in here somehow (Never really used it to be honest Shocked Shocked Shocked), in the meantime feedback's still welcome guys.
Thanks.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2013-02-02 23:38:28 »

The usage of 19 variables (x1..x19) makes it impossible to turn into a loop (without reflection).

just-use-arrays

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2013-02-02 23:38:52 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
int[] xValues = new int[19];

for(int i = 0; i < floorCubes; i++) {
    floorBoxes.add(new Box(Vector3f.ZERO, new Vector3f(floorCubeWidth, floorCubeHeight, floorCubeDepth)));
    floorGeoms.add(new Geometry("Box " + i, floorBoxes.get(i)));
    floorGeoms.get(i).setMaterial(floorMaterial);
    floorNode.attachChild(floorGeoms.get(i));
   
    if(i < 25)
        floorGeoms.get(i).setLocalTranslation(i, 0, 0);
    else
        floorGeoms.get(i).setLocalTranslation(xValues[i/25 - 1]++, 0, -floorCubeDepth * (i/25));
}

Offline GabrielBailey74
« Reply #5 - Posted 2013-02-02 23:42:18 »

This guy... XD!!
Thanks ra4king, 46 lines of code turned into 10 works perfect.

Now to do this for the walls and a few static boxes Pointing

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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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!