I am thinking of using a bit mask to make collision design a bit easier. Here is an example, but this could be anything say different types of enemies and only certain ones may collide and hurt the player.
[Flags]
public enum BitMask
{
None = 0,
Ground = 1,
Sand = 2,
Water = 4,
// etc. above could be say zombie, dragon etc for damage collision.
}
Is an enum the right way to go about this? I imagine say my player to have something like the following
class player
{
int collidables = 6; // this is represented as 0110, so can collide with ground, sand but not water, none.
}
The idea then is later in collision checking I can look at all surrounding blocks and they will have their type, say ground so I can do something like:
if (block.Type | player.collidables) return true;
Does anyone have any guidance or resources to use a pattern like this?
Of course I stumble across something 5 minutes after posting. So maybe more like?
// public enum as above, named Collidables
class Player
{
Collidables collidables = Collidables.Ground | Collidables.Sand;
// other stuff then physics calc
if (player.Collidables | block.Type) return true;
}
From looking at your post I take it you have some sort of tile map and the player is a unit on that map and you want to check if he can move in to the adjoining location before they move - maybe with a mouse hover to give it a green/red border?
Yes, I donāt see a problem with using the flags as you say - so each tile on your map would have a āTerrainTypeā flag and you would then have the units flag set to say what terrain they can go over.
HoverCraft = Water | Sand
Tank = Ground | Sand
Infantry = Ground | Sand & Water
etc.
As for putting enemies into the same flags list - possibly not as these would be more of a game object and could possibly move - but does depend on your game.
As well as the FlagsHelper on that stackoverflow page, I have an extension that might be of use, below is the code.
Basically you can put a description attribute on your enums and then use this rather than the ātostringā - I find it useful for things like tool tips etc.
enum TerrainType
{
[Description("Lovely golden sandy beach")]
Sand,
[Description("Icy cold water")]
Water,
}
static public class Extension_Enum
{
public static string ToDescription(this Enum @this)
{
DescriptionAttribute[] descriptionAttribute = (DescriptionAttribute[])(@this.GetType().GetField(@this.ToString())).GetCustomAttributes(typeof(DescriptionAttribute), false);
return descriptionAttribute.Length > 0 ? descriptionAttribute[0].Description : @this.ToString();
}
/// <summary>
/// Multiple ways to check if an enum has description
/// </summary>
/// <param name="this"></param>
/// <returns></returns>
/// <example>
/// if (someColor.HasDescription())
/// {
/// if (someColor.HasDescription("indicates stop", StringComparison.OrdinalIgnoreCase))
/// {
/// Console.WriteLine("Works");
/// }
/// }
/// </example>
public static bool HasDescription(this Enum @this)
{
return (string.IsNullOrWhiteSpace(@this.ToDescription()) == false);
}
/// <summary>
/// Multiple ways to check if an enum has description
/// </summary>
/// <param name="this"></param>
/// <param name="expectedDescription"></param>
/// <returns></returns>
/// <example>
/// if (someColor.HasDescription())
/// {
/// if (someColor.HasDescription("indicates stop", StringComparison.OrdinalIgnoreCase))
/// {
/// Console.WriteLine("Works");
/// }
/// }
/// </example>
public static bool HasDescription(this Enum @this, string expectedDescription)
{
return @this.ToDescription().Equals(expectedDescription);
}
/// <summary>
/// Multiple ways to check if an enum has description
/// </summary>
/// <param name="this"></param>
/// <param name="expectedDescription"></param>
/// <param name="comparisionType"></param>
/// <returns></returns>
/// <example>
/// if (someColor.HasDescription())
/// {
/// if (someColor.HasDescription("indicates stop", StringComparison.OrdinalIgnoreCase))
/// {
/// Console.WriteLine("Works");
/// }
/// }
/// </example>
public static bool HasDescription(this Enum @this, string expectedDescription, StringComparison comparisionType)
{
return @this.ToDescription().Equals(expectedDescription, comparisionType);
}
}
I think this needs to be an & - not done any bit stuff for a while as I use a helper class as in the stackoverflow post - Also note if you are using .net 4.0+ you can use this:
Thanks, I should have said its mainly thinking about 2D platformer games.
Your little unit example did give me some insight about if it were a 2D Dune 2/CC style game
I might still try and go about it, then for example say a player can become a āghostā for a while, I can set a new mask that allows them to no longer collide with certain walls. Things like that I had in mind for this.
I might look at a hitbox/hurtbox implementation for the enemies collision after some reading.
If I were to do this, Iād also use polymorphism to some degree to handle collisions. At least for enemies anyway, so you donāt get a huge if/else if block in your collision code and itās all a bit more manageable
I didnāt want to say that itās glorious, just that, good or bad, we use it as well
Actually weāve found out the hard way that those maps or masks get convoluted and cluttered pretty fast.
1<<15 shifts the columns in the string-map representation quite a few characters to the left
For the decal-system for example (display a tree there and a stone here) where we have many different objects (which would require many different enum-values) we changed the approach to a XML-array of object-descriptors. Not as easy to read but tidier.
The āhero may run hereā or āmay build hereā or āenemy cannot walk hereā is still done in the āoldā way like this: