How to avoid calling instances?

I defined this simple method:

public static boolean isBorder(int x, int y) throws CollisionDetectionException {
        try {
            if ( (levelItems[x][y] instanceof StaticGameObject && levelItems[x][y].isVisible()) ||
                (levelItems[x-1][y] instanceof StaticGameObject && levelItems[x-1][y].isVisible()) ||
                (levelItems[x][y+1] instanceof StaticGameObject && levelItems[x][y+1].isVisible()) ||
                (levelItems[x][y-1] instanceof StaticGameObject && levelItems[x][y-1].isVisible()) ||
                (levelItems[x-1][y-1] instanceof StaticGameObject && levelItems[x-1][y-1].isVisible()) ||
                (levelItems[x-1][y+1] instanceof StaticGameObject &&levelItems[x-1][y+1].isVisible()) ||
                (levelItems[x+1][y] instanceof StaticGameObject && levelItems[x+1][y].isVisible()) ||
                (levelItems[x+1][y+1] instanceof StaticGameObject && levelItems[x+1][y+1].isVisible()) ||
                (levelItems[x+1][y-1] instanceof StaticGameObject && levelItems[x+1][y-1].isVisible()) ) {
                return true;
            } else {
                return false;
            } 
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new CollisionDetectionException("Collision couldn't be checked because checking position " + x + "/" + y + " caluclated values below (0/0)");
        }
    }

As you can see, I have a 2 dimensional array. Now I want to check a specific position ((x / y) -> method arguments) if there is a visible StaticGameObject in the adjacent fields of the 2-dimensional array.

The levelItems array consists of the so-called GameObject. StaticGameObject is a direct subclass of GameObject.

Any tips on improving this method?

+3
source share
7 answers

Add Method to GameObject

bool isBorderObject() { return false; }

Then override in StaticGameObject

bool isBorderObject() { return true; }

change test to

(levelItems[x][y].isBorderObject() && levelItems[x][y].isVisible())

In addition, if can be nested for

for (int i = x-1; i <= x+1; ++i) {
   for (int j = y-1; j <= y+1; ++j) {
       GameObject item = levelItems[i][j];
       if (item.isBorderObject() && item.isVisible()) 
           return true;
   }
}
return false;
+13
source

(Disclaimer: I worked on Java games running on many mobile devices)

, if. , CollisionDetectionException, , .

Java OO: , , - Java, . Java- , Spring. , , , , , ( : " " OO, , OOA/OOD OOP ).

: ArrayIndexOutOfBoundException . , , HUGE no-no.

, isBorder... , , . , , . , ""? , .

, , , static - - OO. , . , isCollidingWith (...): , "", , , , isCollidingWith (...).

: isCollidingWith (...) resolveCollisionWith (...).

public class Wall implements MySuperAbstraction {

    int wallHitPoints = 42;

    boolean isWallStillUp = true;

    void resolveCollisionWith( SomeObject o ) {
        if ( o.isStrongEnoughToHarmWall ) {
           wallHitPoints--;
           isWallStillUp = wallHitPoints > 0;
        }
    }

}

, , SomeObject, , , .., : OO , . , , .

OO, , , , Java static, instanceof . , - OO:) ]

+3

if , :

for(int col = x-1; col <= x+1; col++)
{
    for(int row = y-1; row <= y+1; row++)
    {
        if(levelItems[row][col] instanceof StaticGameObject && levelItems[row][col].isVisible())
            return true;
    }
}

( if, instanceof, )

, , , .

+2

@Lou, .

for (int i = 0; i < 9; i++) 
   if (levelItems[x-1 + i/3][y-1 + i%3].isBorderObjectVisible())  
       return true; 
return false; 
+2

" ".

, GameObject:

public boolean
isBorder()
{
    return false;
}

StaticGameObject:

public boolean
isBorder()
{
    return self.isVisible();
}

?

+1

, . , (, , ) . ; , , .

  public Set<GameObject> adjacentItems(int x, int y) {
    Set<GameObject> set = new HashSet<GameObject>();
    for (int i = -1; i < 2; ++i)
      for (int j = -1; j < 2; ++j)
        set.add(levelItems[x+i][y+j]);
    return set;
  }

  public boolean isBorder(int x, int y) {
    for (GameObject item : adjacentItems(x, y))
      if (item.isBorder() && item.isVisible())
        return true;
    return false;
  }
+1

. , GameObject isVisible. , .

, :

  • isVisible -

  • isMovable -

instanceof.

[ , , StaticGameObject GameObject isMovable() false.]

0
source

Source: https://habr.com/ru/post/1759196/


All Articles