I have the below class which moves the AI towards the given plant, this works well however it feels really messy.
Any input as to a better way to lay out the logic would be really grateful, the logic follows the idea that we will take the x and y positions of the AI from the plants position, if the values are positive we add 30 and if it is negative we take away 30
If you need any more explaining of the logic let me know.
private void movePosistion(Plant p) {
/*
* set which direction to move,the number generated relates to the
* direction as below:
* 1 2 3
* 4 5
* 6 7 8
*/
int xdiff = p.getXpos() - xpos;
int ydiff = p.getYpos() - ypos;
if (xdiff > 0){
if (ydiff > 0){
//8
xpos += 30;
ypos += 30;
}else if(ydiff < 0){
//3
xpos += 30;
ypos -= 30;
}else{
//5
xpos += 30;
}
}else if(xdiff < 0){
if (xdiff > 0){
//6
xpos -= 30;
ypos += 30;
}else if(xdiff < 0){
//1
xpos -= 30;
ypos -= 30;
}else{
//4
xpos -= 30;
}
}else{
if (ydiff < 0){
//7
ypos -= 30;
}else{
//2
ypos += 30;
}
}
if (xpos > 720)
xpos = 1;
if (ypos > 720)
ypos = 1;
if (xpos < 1)
xpos = 720;
if (ypos < 1)
ypos = 720;
}
xpos = xpos % 720
in your last lines. \$\endgroup\$