Hi,
I want to addObjects to the world that are acting seperatly of each other.
I have a class that checks if the enemy is in range and that the sight isn't blocked. The other class is the enemy class that reduces the health.
When I add the first enemy it works fine, but when I add another enemy the second one only targets you if the first one targets you too.
So how can I let them act for them selves?
Do I have to add a subclass to the class that checks the line of sight?
Line of Sight:
Enemy:
public SichtS(){
setImage(new GreenfootImage(1,1));
}
/**
* Act - do whatever the SichtS wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(!Quest.pause){
sehenSchwert1(-100, 100, -100, 100);
//GameOver();
}
}
public static boolean SichtS = true;
public void sehenSchwert1(int SYNe, int SYPo, int SXNe, int SXPo){
//AYNe = Negativ Y-Value of max distance to Character
//AYPo = Positive Y-Value of max distance to Character
//AXNe = Negativ X-Value of max distance to Character
//AXPo = Positive X-Value of max distance to Character
W1 w1 = (W1) getWorld();
Schwert schwert = new Schwert();
Charakter1 charakter1 = new Charakter1();
Wände wände = (Wände) getOneIntersectingObject(Wände.class);
int EntfX = CharX-SchwertX; //Calculation of distance on x axe
int EntfY = CharY-SchwertY; //Calculation of distance on y axe
boolean EntfX1 = false;
if(SXNe <= EntfX && SXPo >= EntfX ){ //checks if x distance is ok
EntfX1 = true;
}else{
EntfX1 = false;
}
boolean EntfY1 = true;
if(SYNe <= EntfY && SYPo >= EntfY){ //checks if y distance is ok
EntfY1 = true;
}else{
EntfY1 = false;
}
boolean EntfS = false;
if(EntfY1 == true && EntfX1 == true){ //checks if y&x distance is ok
EntfS = true;
}else{
EntfS = false;
}
if(wände !=null || !EntfS){ // checks if line of sight is blocked
SichtS = false;
SchwertX = schwert.X2;
SchwertY = schwert.Y2;
CharX = charakter1.X1;
CharY = charakter1.Y1;
int myX = (SchwertX + CharX) / 2; //calculates X coordinate
int myY = (SchwertY + CharY) / 2; //calculates y coordinate
setLocation(myX, myY);
turnTowards(CharX, CharY); //turning to character
int d = (int)Math.sqrt(Math.pow((SchwertX-CharX),2)+Math.pow((SchwertY-CharY),2)); //calculation of d for size of the line of sight
if(d<=0){
d = 1;
}
setImage(new GreenfootImage(d, 1));
//ImgS = getImage();
// ImgS.setColor(Color.GREEN);
// ImgS.drawLine(0, 0, d,0);
}else{
SichtS = true;
SchwertX = schwert.X2;
SchwertY = schwert.Y2;
CharX = charakter1.X1;
CharY = charakter1.Y1;
int myX = (SchwertX + CharX) / 2;
int myY = (SchwertY + CharY) / 2;
setLocation(myX, myY);
turnTowards(CharX, CharY);
int d = (int)Math.sqrt(Math.pow((SchwertX-CharX),2)+Math.pow((SchwertY-CharY),2));
if(d<=0){
d = 1;
}
setImage(new GreenfootImage(d, 1));
//ImgS = getImage();
// ImgS.setColor(Color.RED);
//ImgS.drawLine(0, 0, d,0);
}
}/**
* Act - do whatever the Schwert wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(!Quest.pause){
getX2();
getY2();
ausrichten();
treffen(10, 1000);
Rot();
//bewegen();
//GameOver();
}
}
public static int X2 = 0;
public void getX2(){
X2 = getX();
}
public static int Y2 = 0;
public void getY2(){
Y2 = getY();
}
/**
* Ausrichtn in Richtung Charakter, wenn SichtS (wird in Klasse SichtA berechnet) eintrifft
*
*/public void ausrichten(){
if(SichtS.SichtS){
//turnTowards(Charakter1.X1, Charakter1.Y1);
bewegen();
}
}
public void bewegen(){
GreenfootImage img = getImage();
int xChar = Charakter1.X1;
int yChar = Charakter1.Y1;
if(xChar < getX() && getOneObjectAtOffset(img.getWidth()/2, 0, Wände.class)==null && getOneObjectAtOffset(img.getWidth()/2, 0, Tor.class)==null && getOneObjectAtOffset(img.getWidth()/2, 0, Fenster.class)==null){
setLocation(X2 - 1, Y2);
setImage("G6/G6Links.png");
}else if(xChar > getX() && getOneObjectAtOffset(img.getWidth()/-2, 0, Wände.class)==null && getOneObjectAtOffset(img.getWidth()/-2, 0, Tor.class)==null && getOneObjectAtOffset(img.getWidth()/-2, 0, Fenster.class)==null){
setLocation(X2 + 1, Y2);
setImage("G6/G6Rechts.png");
}
if(yChar < getY() && getOneObjectAtOffset(0, img.getHeight()/-2, Wände.class)==null && getOneObjectAtOffset(0, img.getHeight()/-2, Tor.class)==null && getOneObjectAtOffset(0, img.getHeight()/-2, Fenster.class)==null){
setLocation(X2, Y2 - 1);
setImage("G6/G6Hinten.png");
}else if(yChar > getY() && getOneObjectAtOffset(0, img.getHeight()/2, Wände.class)==null && getOneObjectAtOffset(0, img.getHeight()/2, Tor.class)==null && getOneObjectAtOffset(0, img.getHeight()/2, Fenster.class)==null){
setLocation(X2, Y2 + 1);
setImage("G6/G6Front.png");
}
}

