I want to make a Lobster, which chases the Crab that you control with your mouse .To do that, i make the lobster always turn to the coordinates of the Crab.
This worked but you couldn´t run from the lobster because it always turned to you instandly. To make it harder for the lobster I tried to delay the turning. Here the Code that doesn´t work:
public class Lobster extends Animal
{
private int counter;
private int crabX;
private int crabY;
private int crabX0;
private int crabY0;
private int crabX1;
private int crabY1;
private int crabX2;
private int crabY2;
private int crabX3;
private int crabY3;
private int crabX4;
private int crabY4;
/**
* Act - do whatever the Lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Lobster(){
crabX0 = 0;
crabY0 = 0;
crabX1 = 0;
crabY1 = 0;
crabX2 = 0;
crabY2 = 0;
crabX3 = 0;
crabY3 = 0;
crabX4 = 0;
crabY4 = 0;
}
public void act()
{
move();
int newCrabX = ((Crab) getWorld().getObjects(Crab.class).get(0)).getX();
int newCrabY = ((Crab) getWorld().getObjects(Crab.class).get(0)).getY();
crabX = newCrabX;
crabY = newCrabY;
turnLobster();
counter++;
}
public void turnLobster(){
switch(counter){
case(0):
crabX0 = crabX;
crabY0 = crabY;
turnTowards(crabX1, crabX1);
case(1):
crabX1 = crabX;
crabY1 = crabY;
turnTowards(crabX2, crabX2);
case(2):
crabX2 = crabX;
crabY2 = crabY;
turnTowards(crabX3, crabX3);
case(3):
crabX3 = crabX;
crabY3 = crabY;
turnTowards(crabX4, crabX4);
case(4):
crabX4 = crabX;
crabY4 = crabY;
turnTowards(crabX0, crabX0);
counter = -1;
break;
}
}
}
