在我的一个类中,我有一个整数来存储一组枚举标志,如下所示:
enum AttackFlags
{
Contact = 1, //Move connects with target
Projectile = 2, //Attack is projectile based
Unblockable = 4, //Attack can not be blocked
UncounterableLv1 = 8, //Attack can't be countered except by extreme counter attack skills/status effects
UncounterableLv2 = 16, //Attack can not be countered
Flinches = 32, //Has a chance to stun the enemy, pushing back their next turn
Unreflectable = 64, //Attack penetrates reflect. Only checked for Magic attacks
IgnoreDefenderStatusEffects = 128, //Ignores active status effects on the defender
IgnoreAttackerStatusEffects = 256, //Ignores active status effects on the attacker
IgnoreDefenderAbilities = 512, //Ignore the defenders abilities
IgnoreAttackerAbilities = 1024, //Ignore the attackers abilities
IgnoreArmorRating = 2048, //Ignore the defensive boosts of armor
IgnoreWeaponRating = 4096, //Ignore the attack boost from weapons
HighCritical = 8192, //The move has an increased chance to crit
CausesStatus = 16384, //Can the move cause status effects?
Elemental = 32768, //Is the move elemental based?
Unimplemented = 65536, //has the move been implemented yet?
ModsTimer = 131072, //Does it have an effect on the target or users timer?
Heals = 262144, //Does the move heal?
SecondaryEffects = 524288, //Attack has additional effects besides basic attack
PhysicalAttackFlag = 1048576, //Is the Class Physically based? I.E. blocked by Protect and Shield
MagicAttackFlag = 2097152, //Is the move Magically Based? I.E. is it affected by things like Shell
MultiHit = 4194304, //Does it enxcapsulate more then 1 hit
SingleUse = 8388608, //Attack can only be used once per battle
DoesNotCauseDamage = 16777216
};
class Attack
{
int AtkFlags; //Stores AttackFlags |'d together
}
我想使用以下签名向我的 Attack 类添加一个方法
bool HasAttackFlags(int flags);
flags 将是许多 AttackFlags |'d 在一起。如果我只想检查一个标志,我可以将 & AtkFlags 和标志放在一起,但是因为我必须检查多个可能的标志,所以这行不通。如何正确检查多个标志?我想避免传递一个 vector/标志集来检查,因为简单地 |'ing 一组标志在一起比构造一个 vector/集更简单
提前致谢
编辑:
为了阐明我的意思,我可能有以下内容
Attack atk;
atk.AtkFlags = Contact | Projectile | Heals | MagicAttackFlag;
然后,我想像这样检查 atk 上的标志:
bool res = atk.HasAttackFlags(Contact | Projectile);
res应该为真反之
bool res = atk.HasAttackFlags(Contact | Unreflectable);
应该为 false,因为 AtkFlags 不包含 Contact 和 Unreflectable。
最佳答案
我不确定我是否在关注您的问题,因为您似乎提到了显而易见的解决方案。那么这个解决方案有什么问题,显然添加你想要的任何额外标志:
bool HasAttackFlags(int flags) {
return (flags&(contact|projectile))!=0;
}
编辑:哦......我想我刚刚弄明白了,你想检查是否有 2 个或更多标志作为一个集合?在这种情况下,您只需将方法修改为:
bool HasAttackFlags(int flags) {
return (flags&(contact|projectile))==(contact|projectile);
}
关于C++ 检查枚举标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7366458/