I have code that is responsible for determining move position in a hex grid when the user moves the stick on the gamepad.
cameraAdjustedMovement
is aVector3
._xThreshold
and_yThreshold
arefloat
s._isOdd
is abool
.
The code is very simple. We just check input from cameraAdjustedMovement
and determine where to move. The _isOdd
case is basically when where we want to move is a pointy hex, so if you constantly input up, then you will go in a straight line in a hex grid, switching between left and right.
Is there a way to improve it without making it too complicated or is this code as good as it can get?
if (cameraAdjustedMovement.x < -_xThreshold)
{
if (cameraAdjustedMovement.z > _yThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.ETopLeft;
}
else if (cameraAdjustedMovement.z < -_yThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.EBottomLeft;
}
else
{
movePosition = ScenarioManager.EAdjacentPosition.ELeft;
}
}
else if (cameraAdjustedMovement.x > _xThreshold)
{
if (cameraAdjustedMovement.z > _yThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.ETopRight;
}
else if (cameraAdjustedMovement.z < -_xThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.EBottomRight;
}
else
{
movePosition = ScenarioManager.EAdjacentPosition.ERight;
}
}
else
{
if (_isOdd)
{
if (cameraAdjustedMovement.z > _yThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.ETopRight;
_isOdd = !_isOdd;
}
else if (cameraAdjustedMovement.z < -_yThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.EBottomRight;
_isOdd = !_isOdd;
}
}
else
{
if (cameraAdjustedMovement.z > _yThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.ETopLeft;
_isOdd = !_isOdd;
}
else if (cameraAdjustedMovement.z < -_yThreshold)
{
movePosition = ScenarioManager.EAdjacentPosition.EBottomLeft;
_isOdd = !_isOdd;
}
}
}