Then, instead of downActions
being an IEnumerable<GameActions>
and using LINQ IEnumearble<T>.Contains
extension, you can add the values of all "down actions", and pass ActionRequested
an enum value that represents the sum of all pressed directions, be it Left+Top
, Down+Right
or whatever. The [Flags] attribute merely serves as a visual cue to remind you that these values can be combined and, using bitwise AND operations bitwise AND operations or simply, the .HasFlag()
method, the handler can determine whether the received value 3 contains Direction.Left
:
Then, instead of downActions
being an IEnumerable<GameActions>
and using LINQ IEnumearble<T>.Contains
extension, you can add the values of all "down actions", and pass ActionRequested
an enum value that represents the sum of all pressed directions, be it Left+Top
, Down+Right
or whatever. The [Flags] attribute merely serves as a visual cue to remind you that these values can be combined and, using bitwise AND operations or simply, the .HasFlag()
method, the handler can determine whether the received value 3 contains Direction.Left
:
Then, instead of downActions
being an IEnumerable<GameActions>
and using LINQ IEnumearble<T>.Contains
extension, you can add the values of all "down actions", and pass ActionRequested
an enum value that represents the sum of all pressed directions, be it Left+Top
, Down+Right
or whatever. The [Flags] attribute merely serves as a visual cue to remind you that these values can be combined and, using bitwise AND operations or simply, the .HasFlag()
method, the handler can determine whether the received value 3 contains Direction.Left
:
This is what's painted you into the corner you're in. You're not showing your GameActions
enum (a better name would be GameAction
, or even better, MoveDirection
if..actually, Direction
suffices, since all there is to it is a bunch of values that each represent a direction - the point is, enum type names should not be plural), but I presume it looks something like this:
[Flags]
public enum MoveDirectionDirection
{
Left = 1,
Top = 2,
Right = 4,
Down = 8
}
Then, instead of downActions
being an IEnumerable<GameActions>
and using LINQ IEnumearble<T>.Contains
extension, you can add the values of all "down actions", and pass ActionRequested
an enum value that represents the sum of all pressed directions, be it Left+Top
, Down+Right
or whatever. The [Flags] attribute merely serves as a visual cue to remind you that these values can be combined and, using bitwise AND operations or simply, the.HasFlag()
method, the handler can determine whether the received value 3 contains MoveDirectionDirection.Left
:
void ActionRequested(MoveDirectionDirection direction)
{
if ((direction & MoveDirection.Left) == MoveDirectionHasFlag(Direction.Left))
{
// direction contains MoveDirectionDirection.Left
}
if ((direction & MoveDirection.Right) == MoveDirectionHasFlag(Direction.Right))
{
// direction contains MoveDirectionDirection.Right
}
if ((direction & MoveDirection.Top) == MoveDirectionHasFlag(Direction.Top))
{
// direction contains MoveDirectionDirection.Top
}
if ((direction & MoveDirection.Bottom) == MoveDirectionHasFlag(Direction.Bottom))
{
// direction contains MoveDirectionDirection.Bottom
}
}
class Program
{
[Flags]
public enum Direction
{
Left = 1,
Top = 2,
Right = 4,
Bottom = 8
}
static void Main(string[] args)
{
var direction = (Direction)3;
Console.WriteLine("Direction: {0}", direction);
Console.WriteLine("Left: {0}", (Direction.Left & direction) == .HasFlag(Direction.Left);
Console.WriteLine("Top: {0}", (Direction.Top & direction) == .HasFlag(Direction.Top);
Console.WriteLine("Right: {0}", (Direction.Right & direction) == .HasFlag(Direction.Right);
Console.WriteLine("Bottom: {0}", (Direction.Bottom & direction) == .HasFlag(Direction.Bottom);
Console.ReadLine();
}
}
This is what's painted you into the corner you're in. You're not showing your GameActions
enum (a better name would be GameAction
, or even better, MoveDirection
if all there is to it is a bunch of values that each represent a direction - the point is, enum type names should not be plural), but I presume it looks something like this:
[Flags]
public enum MoveDirection
{
Left = 1,
Top = 2,
Right = 4,
Down = 8
}
Then, instead of downActions
being an IEnumerable<GameActions>
and using LINQ IEnumearble<T>.Contains
extension, you can add the values of all "down actions", and pass ActionRequested
an enum value that represents the sum of all pressed directions, be it Left+Top
, Down+Right
or whatever. The [Flags] attribute merely serves as a visual cue to remind you that these values can be combined and, using bitwise AND operations, the handler can determine whether the received value 3 contains MoveDirection.Left
:
void ActionRequested(MoveDirection direction)
{
if ((direction & MoveDirection.Left) == MoveDirection.Left)
{
// direction contains MoveDirection.Left
}
if ((direction & MoveDirection.Right) == MoveDirection.Right)
{
// direction contains MoveDirection.Right
}
if ((direction & MoveDirection.Top) == MoveDirection.Top)
{
// direction contains MoveDirection.Top
}
if ((direction & MoveDirection.Bottom) == MoveDirection.Bottom)
{
// direction contains MoveDirection.Bottom
}
}
class Program
{
[Flags]
public enum Direction
{
Left = 1,
Top = 2,
Right = 4,
Bottom = 8
}
static void Main(string[] args)
{
var direction = (Direction)3;
Console.WriteLine("Direction: {0}", direction);
Console.WriteLine("Left: {0}", (Direction.Left & direction) == Direction.Left);
Console.WriteLine("Top: {0}", (Direction.Top & direction) == Direction.Top);
Console.WriteLine("Right: {0}", (Direction.Right & direction) == Direction.Right);
Console.WriteLine("Bottom: {0}", (Direction.Bottom & direction) == Direction.Bottom);
Console.ReadLine();
}
}
This is what's painted you into the corner you're in. You're not showing your GameActions
enum (a better name would be GameAction
, or even better, MoveDirection
..actually, Direction
suffices, since all there is to it is a bunch of values that each represent a direction - the point is, enum type names should not be plural), but I presume it looks something like this:
[Flags]
public enum Direction
{
Left = 1,
Top = 2,
Right = 4,
Down = 8
}
Then, instead of downActions
being an IEnumerable<GameActions>
and using LINQ IEnumearble<T>.Contains
extension, you can add the values of all "down actions", and pass ActionRequested
an enum value that represents the sum of all pressed directions, be it Left+Top
, Down+Right
or whatever. The [Flags] attribute merely serves as a visual cue to remind you that these values can be combined and, using bitwise AND operations or simply, the.HasFlag()
method, the handler can determine whether the received value 3 contains Direction.Left
:
void ActionRequested(Direction direction)
{
if (direction.HasFlag(Direction.Left))
{
// direction contains Direction.Left
}
if (direction.HasFlag(Direction.Right))
{
// direction contains Direction.Right
}
if (direction.HasFlag(Direction.Top))
{
// direction contains Direction.Top
}
if (direction.HasFlag(Direction.Bottom))
{
// direction contains Direction.Bottom
}
}
class Program
{
[Flags]
public enum Direction
{
Left = 1,
Top = 2,
Right = 4,
Bottom = 8
}
static void Main(string[] args)
{
var direction = (Direction)3;
Console.WriteLine("Direction: {0}", direction);
Console.WriteLine("Left: {0}", direction.HasFlag(Direction.Left);
Console.WriteLine("Top: {0}", direction.HasFlag(Direction.Top);
Console.WriteLine("Right: {0}", direction.HasFlag(Direction.Right);
Console.WriteLine("Bottom: {0}", direction.HasFlag(Direction.Bottom);
Console.ReadLine();
}
}
var pressedKeys = keyboardState.PressedKeys(); var inputActions = pressedKeys.Select(GetInputAction).ToList(); var downActions = inputActions.Select(ia => ia.Down);
This is what's painted you into the corner you're in. You're not showing your GameActions
enum (a better name would be GameAction
, or even better, MoveDirection
if all there is to it is a bunch of values that each represent a direction - the point is, enum type names should not be plural), but I presume it looks something like this:
public enum GameActions
{
MoveLeft,
MoveUpLeft,
MoveUp,
MoveUpRight,
MoveRight,
MoveDownRight,
MoveDown,
MoveDownLeft
}
The problem is that MoveUpLeft
is a combination of MoveUp
and MoveLeft
, and since the enum type itself doesn't account for it, your code has to.
I'd suggest using a [Flags]
attribute:
[Flags]
public enum MoveDirection
{
Left = 1,
Top = 2,
Right = 4,
Down = 8
}
Then, instead of downActions
being an IEnumerable<GameActions>
and using LINQ IEnumearble<T>.Contains
extension, you can add the values of all "down actions", and pass ActionRequested
an enum value that represents the sum of all pressed directions, be it Left+Top
, Down+Right
or whatever. The [Flags] attribute merely serves as a visual cue to remind you that these values can be combined and, using bitwise AND operations, the handler can determine whether the received value 3 contains MoveDirection.Left
:
void ActionRequested(MoveDirection direction)
{
if ((direction & MoveDirection.Left) == MoveDirection.Left)
{
// direction contains MoveDirection.Left
}
if ((direction & MoveDirection.Right) == MoveDirection.Right)
{
// direction contains MoveDirection.Right
}
if ((direction & MoveDirection.Top) == MoveDirection.Top)
{
// direction contains MoveDirection.Top
}
if ((direction & MoveDirection.Bottom) == MoveDirection.Bottom)
{
// direction contains MoveDirection.Bottom
}
}
It's not clear from your post exactly what GetInputAction
does and what downActions
means, so it's hard to recommend a change that will fit into your code, but here's some inspiration:
class Program
{
[Flags]
public enum Direction
{
Left = 1,
Top = 2,
Right = 4,
Bottom = 8
}
static void Main(string[] args)
{
var direction = (Direction)3;
Console.WriteLine("Direction: {0}", direction);
Console.WriteLine("Left: {0}", (Direction.Left & direction) == Direction.Left);
Console.WriteLine("Top: {0}", (Direction.Top & direction) == Direction.Top);
Console.WriteLine("Right: {0}", (Direction.Right & direction) == Direction.Right);
Console.WriteLine("Bottom: {0}", (Direction.Bottom & direction) == Direction.Bottom);
Console.ReadLine();
}
}
Output:
enter image description here