简体   繁体   中英

How can I replace these for loops with a generic method

I'm making my first game with libGDX and I'm currently working with the object collision methods. I have this code that works well, but is there a way to replace the foreach loops in a generic method? (I need to know if the object is a Solid, Rock, etc..)

MapLayers layers = map.getLayers();
MapLayer layer = layers.get(Game.BORDER_LAYER);
MapObjects objects = layer.getObjects();

//border
for(MapObject object : objects.getByType(RectangleMapObject.class)){
    Rectangle rect = ((RectangleMapObject) object).getRectangle();

    new Border(world, map, rect);
}

layer = layers.get(Game.SOLID_LAYER);
objects = layer.getObjects();

//solid bricks
for(MapObject object : objects.getByType(RectangleMapObject.class)){
    Rectangle rect = ((RectangleMapObject) object).getRectangle();

    new Solid(world, map, rect);
}

layer = layers.get(Game.TREE_LAYER);
objects = layer.getObjects();

//trees
for(MapObject object : objects.getByType(RectangleMapObject.class)){
    Rectangle rect = ((RectangleMapObject) object).getRectangle();

    new Tree(world, map, rect);
}

layer = layers.get(Game.ROCK_LAYER);
objects = layer.getObjects();

//rocks
for(MapObject object : objects.getByType(RectangleMapObject.class)){
    Rectangle rect = ((RectangleMapObject) object).getRectangle();

    new Rock(world, map, rect);
}

Edit: All objects inherit from the same abstract class

public class Rock extends InteractiveTileObject

And the Game.XXXX_LAYER is an integer that indicates the map layer in which those objects are located

No, it will not be possible to do this with generics because you will not be able to instantiate an object by calling new T(world, map, rect) . However, since there is a lot of repeated code in the 4 loops, you can make a separate method that takes the layer number and the Class representation of the object that needs creating. By using reflection, you can call the constructor of the class that is passed in order to instantiate your component.

public void createComponent(int layer, Class toCreate) {
    for(MapObject object : map.getLayers().get(layer).getObjects().getByType(RectangleMapObject.class)){
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        toCreate.getConstructor(World.class, Map.class, Rectangle.class).newInstance(world, map, rect);
    }
}

After making this method, you can call it as follows:

void main() {
    createComponent(Game.BORDER_LAYER, Border.class);
    createComponent(Game.SOLID_LAYER, Solid.class);
    createComponent(Game.TREE_LAYER, Tree.class);
    createComponent(Game.ROCK_LAYER, Rock.class);
}

You can also take a look at this thread to find out more about Reflection and its API's: What is reflection and why is it useful?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM