Search code examples
javacollectionssetblockflip

Flipping a set of blocks in Java


In my game that I am creating, I am using blocks. To simplify my life, I created a class that accepts an array of blocks, and adds them to the block set for me.

Block set (in World class):

Set<Block> blocks = new HashSet<Block>();

BlockCollection class:

public class BlockCollection
{
    private World world;
    Set<Block> blocks = new HashSet<Block>();

    public BlockCollection(World world, Vector2 position, Block[] blocks)
    {
        this.world = world;

        for (Block block : blocks)
            this.blocks.add(new Block(new Vector2(block.position.x + position.x, block.position.y + position.y)));
    }

    public void flip()
    {
        //Flip the set
    }

    public void add()
    {
        world.blocks.addAll(blocks);
    }
}

(The library that I am using is libgdx, the Vector2 class is basically a vector with an X float and a Y float)

I added an array of blocks that looks like this:

Block[] spike = {
        new Block(new Vector2(0, 0)),
        new Block(new Vector2(1, 0)),
        new Block(new Vector2(2, 0)),
        new Block(new Vector2(3, 0)),
        new Block(new Vector2(1, 1)),
        new Block(new Vector2(2, 1)),
        new Block(new Vector2(3, 1)),
        new Block(new Vector2(1, 2)),
        new Block(new Vector2(2, 2)),
        new Block(new Vector2(1, 3)),
        new Block(new Vector2(2, 3)),
        new Block(new Vector2(1, 4)),
};

It works, but now I'm trying to add functionality to flip the set of blocks in the flip() method of the BlockCollection class. It would help me not need to hardcode every single variation of every single object I make with blocks.

Can anyone share any insight, maybe an equation, of how to do this? Any help is much appriciated.

EDIT: By flip, I mean flip the shape along the x axis, similarly to flipping an image. Example:

Original vs. Flipped
00100 | 11111
01110 | 01110
11111 | 00100


Solution

  • You probably want to change your data structure and use the coordinates as keys for the Blocks. If you know the size of your image at construction, you could use a two-dimensional array to store all your blocks, with the Y coordinate as first index and the X coordinate as second index:

    this.blocks[y][x] = block;
    

    Then, flipping along the X axis can be accomplished by simply reversing the order of the rows:

    public void flip()
    {
        List<Block[]> blocksAsList = Arrays.asList(blocks);
        Collections.reverse(blocksAsList);
        blocks = blocksAsList.toArray();
    }
    

    You didn't elaborate much as to what Block and BlockCollection actually represent. In your example, Block seems to have its own Vector2. Does it own and control that position? Can a Block be owned by none, one or multiple BlockCollections? If a Block controls its position, what should happen to a BlockCollection containing a Block which has changed its position? Can and/or should the BlockCollection update a Block's position when flipping? Does Block really need its own position? ...