Download

Seriously?

Never expected a simple 8 value direction struct’s code to be this long XD

BTW feel free to use this code in your projects


USTRUCT()
struct FDirection8
{
	GENERATED_USTRUCT_BODY();

	UPROPERTY()
		EDirection8 Direction;

	FDirection8()
	{

	}

	FDirection8(EDirection8 NewDirection)
	{
		Direction = NewDirection;
	}

	FDirection8(EDirection4 NewDirection)
	{
		FromEDirection4(NewDirection);
	}

	void FromEDirection4(EDirection4 NewDirection)
	{
		if (NewDirection == EDirection4::Forward) Direction = EDirection8::Forward;
		else if (NewDirection == EDirection4::Right) Direction = EDirection8::Right;
		else if (NewDirection == EDirection4::Backward) Direction = EDirection8::Backward;
		else if (NewDirection == EDirection4::Left) Direction = EDirection8::Left;
		else Direction = EDirection8::None;
	}

	void AddDirection(EDirection4 Value)
	{
		if (Direction != EDirection8::None)
		{
			if (Value == EDirection4::Forward)
			{
				if (Direction == EDirection8::Right) Direction = EDirection8::ForwardRight;
				else if (Direction == EDirection8::Left) Direction = EDirection8::ForwardLeft;
			}
			else if (Value == EDirection4::Right)
			{
				if (Direction == EDirection8::Forward) Direction = EDirection8::ForwardRight;
				else if (Direction == EDirection8::Backward) Direction = EDirection8::BackwardRight;

			}
			else if (Value == EDirection4::Backward)
			{
				if (Direction == EDirection8::Right) Direction = EDirection8::BackwardRight;
				else if (Direction == EDirection8::Left) Direction = EDirection8::BackwardLeft;
			}
			else if (Value == EDirection4::Left)
			{
				if (Direction == EDirection8::Forward) Direction = EDirection8::ForwardLeft;
				else if (Direction == EDirection8::Backward) Direction = EDirection8::BackwardLeft;
			}
		}
		else FromEDirection4(Value);
	}

	void AddDirection(EDirection4 Value, bool bOverrideOpposing)
	{
		if (bOverrideOpposing == NULL) bOverrideOpposing = false;

		if (Direction != EDirection8::None)
		{
			if (Value == EDirection4::Forward)
			{
				if (Direction == EDirection8::Right) Direction = EDirection8::ForwardRight;
				else if (Direction == EDirection8::Left) Direction = EDirection8::ForwardLeft;
				else if (Direction == EDirection8::Backward && bOverrideOpposing) Direction = EDirection8::Forward;
			}
			else if (Value == EDirection4::Right)
			{
				if (Direction == EDirection8::Forward) Direction = EDirection8::ForwardRight;
				else if (Direction == EDirection8::Backward) Direction = EDirection8::BackwardRight;
				else if (Direction == EDirection8::Left && bOverrideOpposing) Direction = EDirection8::Right;

			}
			else if (Value == EDirection4::Backward)
			{
				if (Direction == EDirection8::Right) Direction = EDirection8::BackwardRight;
				else if (Direction == EDirection8::Left) Direction = EDirection8::BackwardLeft;
				else if (Direction == EDirection8::Forward && bOverrideOpposing) Direction = EDirection8::Backward;
			}
			else if (Value == EDirection4::Left)
			{
				if (Direction == EDirection8::Forward) Direction = EDirection8::ForwardLeft;
				else if (Direction == EDirection8::Backward) Direction = EDirection8::BackwardLeft;
				else if (Direction == EDirection8::Right && bOverrideOpposing) Direction = EDirection8::Left;
			}
		}
		else FromEDirection4(Value);
	}

	// Do not use EAxis::Z
	void ZeroOutAxis(EAxis::Type Value)
	{
		if (Value == EAxis::X)
		{
			if (Direction == EDirection8::Forward || Direction == EDirection8::Backward) Direction = EDirection8::None;
			else if (Direction == EDirection8::ForwardRight || Direction == EDirection8::BackwardRight) Direction = EDirection8::Right;
			else if (Direction == EDirection8::ForwardLeft || Direction == EDirection8::BackwardLeft) Direction = EDirection8::Left;
			// Else direction does not need to be modified
		}
		else if (Value == EAxis::Y)
		{
			if (Direction == EDirection8::Right || Direction == EDirection8::Left) Direction = EDirection8::None;
			else if (Direction == EDirection8::ForwardRight || Direction == EDirection8::ForwardLeft) Direction = EDirection8::Forward;
			else if (Direction == EDirection8::BackwardRight || Direction == EDirection8::BackwardLeft) Direction = EDirection8::Backward;
			// Else direction does not need to be modified
		}
	}
};