I am new to game development, and I have been building some game with LibGDX engine.
The following function might make you feel bad
The function I am going to show, is accepting the current rotation index of my player (player has 4 rotations, north, south, west, east). The function will return how much to jump in X to get the next tile according to his direction (angle) and the selected move (you can only select left, right or forward per turn):
public int getIncrementXForRotation(int rotationIndex) {
switch(this) {
case MOVE_FORWARD:
switch(rotationIndex) {
case Face.EAST:
return 1;
case Face.SOUTH:
return 0;
case Face.WEST:
return -1;
case Face.NORTH:
return 0;
}
case MOVE_BACKWARD:
switch(rotationIndex) {
case Face.EAST:
return -1;
case Face.SOUTH:
return 0;
case Face.WEST:
return 1;
case Face.NORTH:
return 0;
}
case MOVE_LEFT:
switch (rotationIndex) {
case Face.EAST:
return 0;
case Face.SOUTH:
return 1;
case Face.WEST:
return 0;
case Face.NORTH:
return -1;
}
case MOVE_RIGHT:
switch (rotationIndex) {
case Face.EAST:
return 0;
case Face.SOUTH:
return -1;
case Face.WEST:
return 0;
case Face.NORTH:
return 1;
}
case TURN_LEFT:
switch(rotationIndex) {
case Face.EAST:
case Face.SOUTH:
return 1;
case Face.WEST:
case Face.NORTH:
return -1;
}
case TURN_RIGHT:
switch(rotationIndex) {
case Face.NORTH:
case Face.EAST:
return 1;
case Face.WEST:
case Face.SOUTH:
return -1;
}
}
return 0;
}
Same function for Y
.
Now I looked at this, and i thought to myself.. north can be an 0
, and east can be 90
, and south can be 180
and west can be 270
, am I right?
So there is a way to mathematically do these coordinate increment according to the angle, isn't there?
The reason I classified my player angles by `enums` is because each direction is an image.