Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 334 times)
0 Members and 1 Guest are viewing this topic.
Online GabrielBailey74

JGO Knight


Medals: 8
Projects: 6


<3 Java


« Posted 2013-02-03 00: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 Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


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

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

Try using the modulus operator.

Online GabrielBailey74

JGO Knight


Medals: 8
Projects: 6


<3 Java


« Reply #2 - Posted 2013-02-03 00: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: 438
Projects: 4


Hand over your head.


« Reply #3 - Posted 2013-02-03 00: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #4 - Posted 2013-02-03 00: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));
}

Online GabrielBailey74

JGO Knight


Medals: 8
Projects: 6


<3 Java


« Reply #5 - Posted 2013-02-03 00: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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (104 views)
2013-05-17 21:29:12

alaslipknot (113 views)
2013-05-16 21:24:48

gouessej (141 views)
2013-05-16 00:53:38

gouessej (137 views)
2013-05-16 00:17:58

theagentd (149 views)
2013-05-15 15:01:13

theagentd (134 views)
2013-05-15 15:00:54

StreetDoggy (177 views)
2013-05-14 15:56:26

kutucuk (200 views)
2013-05-12 17:10:36

kutucuk (201 views)
2013-05-12 15:36:09

UnluckyDevil (207 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.273 seconds with 20 queries.