0

In the game i'm making I need to constantly find objects from a list and then check if it's coordinates are adjacent, there is hundreds of these objects and only 4 of them can be right, is there a better way to do this?

The list is at getMap.getTerrain(x,y);

It's a 2 Dimension int array that holds the dataValue of the object.

    int[] r = new int[4];
    int tX = (int)(getX()/32);
    int tY = (int)(getY()/32);
    if((tY > 1) && (tX > 1) && (tY < 39) && (tX < 39))
    {
    r[0] = getMap().getTerrain(tX-1, tY);
    r[1] = getMap().getTerrain(tX+1, tY);
    r[2] = getMap().getTerrain(tX, tY-1);
    r[3] = getMap().getTerrain(tX, tY+1);
    //}
    }
    int resource = 0;
    for(int i : r) if(i != 0) resource = i; 
    if(resource != 0)
    {
        System.out.println("R isnt 0, We are next to a "+resource);
    }
Ryan Ramsden
  • 21
  • 1
  • 3

1 Answers1

0

Hmm... if getTerrain looks up a value in a 40x40 two dimensional array, I don't think you're going to get much faster. The only issue might be if you have a sparse array and resources are scattered throughout the map. In that case you have 1,600 array elements with most filled with 0. If it is sparse, and you want large maps with 100 x 100 or 1,000 x 1,000, then you should look at a Point class and a HashMap where the point is the key. Get terrain can then return 0 for any elements not in the hash map. Otherwise it doesn't look horrible, if you are going to stick with smallish 2-d arrays.

ipaul
  • 374
  • 1
  • 10