Enum, Flags and bitwise operators

If you’re a game developer chances are you’re familiar with the need to describe different variations of an attribute. Whether it’s the type of an attack (melee, ice,  fire, poison, …) or the state of an enemy AI (idle, alerted, chasing, attacking, resting, …) you can’t escape this. The most naive way of implementing this is simply by using constants:

public static int NONE = 0;
public static int MELEE = 1;
public static int FIRE = 2;
public static int ICE = 3;
public static int POISON = 4;

public int attackType = NONE;

The downside is that you have no actual control over the values you can assign to attackType: it can be any integer, and you can also do dangerous things such as attackType++.

Continue reading