vendredi 25 septembre 2015

Have enum value be equivalent to many others

I need to use an enum of various values, in this case various building pieces. Most of these are unique, but there a few that I'd like to be equivalent. I mean as follows:

enum class EPiece: uint8 {
    Ceiling,
    Table,
    Door,
    WestWall,
    NorthWall,
    SouthWall,
    EastWall,
    Wall,
    Floor
};

And I'd like to Wall == WestWall to be true, as well as Wall == NorthWall, etc. However, WestWall == NorthWall is false.

Why I am doing this is because I am making a game where various pieces have a definition based off of what they are/where they are. The player has to place various pieces in a predefined order. The player first has to place a NorthWall piece. They will have available various pieces, and will have to select a Wall piece, and have to attempt to place it on a NorthWall piece. The game checks if the two are equivalent (in this case true), and if the current piece to place is NorthWall. If they attempt to place it on a WestWall piece it should fail since it's not that stage yet.

I thought of doing this through flags, doing something like

WestWall = 0x01,
NorthWall = 0x02,
SouthWall = 0x04,
EastWall = 0x08,
Wall = WestWall | NorthWall | SouthWall | EastWall

and checking by doing something like:

// SelectedPiece is the Piece the Player selected and is attempting to place
// PlacedOnPiece is the Piece that we are attempting to place on top of
// CurrentPieceToPlace is what Piece we are supposed to place at this stage
if ((CurrentPieceToPlace == PlacedOnPiece) && (SelectedPiece & PlacedOnPiece != 0)) {
}

The thing is, I have a lot of pieces and my understanding is to make the flags work I have to use powers of two. That means if I use uint32 I could have a max of 32 Pieces, and I don't want to be limited by that. I might only need around 20, but I don't want to get stuck.

Any suggestions? At this point I need to use an enum, so I can't try a different type.

Aucun commentaire:

Enregistrer un commentaire