Good Day fellow coders, i have occurred a bump in the road working towards the completion of my major project that requires some expert help. I have created a line of sight actor that draws a line between two actors in the world, this is done so that it can check if any obstacles are in the way.
Army2 code:
As you can see here i have implemented a list system that allows my to get the multiple Army2 actors in the world in an attempt to assign a corresponding Army1 actor for the Army2 actor to turn towards if their is a clear line of sight.
What i want to happen is once the Army2 is in the world it will look for an Army1 and lock onto it then return a Boolean statement to allow for other Army2 actors to know that that specific Army1 is found and they should continue searching for another.
Thanks
Actor Army1;
Actor Army2;
int army1X;
int army1Y;
int army2X;
int army2Y;
GreenfootImage myImage;
public LineOfSight (Actor a1, Actor a2)
{
Army1 = a1;
Army2 = a2;
setImage(new GreenfootImage(1,1));
}
/**
* Act - do whatever the LineOfSight wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Army1.getWorld()!=null && Army2.getWorld()!=null)
{
army1X = Army1.getX();
army1Y = Army1.getY();
army2X = Army2.getX();
army2Y = Army2.getY();
int myX = (army2X + army1X)/2;
int myY = (army2Y + army1Y)/2;
setLocation(myX, myY);
turnTowards(army1X, army1Y);
int d =(int)Math.sqrt(Math.pow((army2X-army1X),2)+Math.pow((army2Y-army1Y),2));
setImage(new GreenfootImage(d,1));
myImage = getImage();
myImage.setColor(Color.BLACK);
myImage.drawLine(0,0,d,0);
}
}
public boolean clearLineOfSight()
{
return(getOneIntersectingObject(Barriers.class)==null);
}
}public class Army2 extends West
{
Army1 army1OnScreen;
LineOfSight los;
boolean found = false;
public List<Army1> army1List;
public Army1 targetedArmyObject;
public void addedToWorld(World myWorld)
{
if(myWorld instanceof BattleGround)
{
BattleGround battleground = (BattleGround)myWorld;
los = battleground.getLOS();
army1OnScreen = battleground.getArmy1();
}
}
/**
* Act - do whatever the Army2 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Army1Found();
if(los!=null)
{
if(los.clearLineOfSight())
{
if(army1OnScreen.getWorld()!=null && found ==true)
{
turnTowards(army1OnScreen.getX(), army1OnScreen.getY());
move(3);
}
}
}
if( isTouching(Army1.class))
{
removeTouching(Army1.class);
}
}
public void Army1Found()
{
army1List = getWorld().getObjects(Army1.class);
int i = 0;
while (i < army1List.size() && !found)
{
if (army1List.get(i).targeted)
{
i ++;
}
else
{
found = true;
army1List.get(i).targeted = true;
targetedArmyObject = army1List.get(i);
}
}
}
public Army2()
{
}
}public class Army1 extends East
{ private int frameCounterMove = 0;
private int reload;
int damage;
public boolean targeted;
private String[] womanleft = {"eastarmywomen3.png", "eastarmywomen4.png"};
/**
* Act - do whatever the Army1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
animate();
// move(-2);
// if (Research = 1)
// {
// damage = 1;
// }
}
public Army1()
{
}
public void animate()
{
reload++;
if(reload==7)
{
setImage(womanleft[frameCounterMove]);
frameCounterMove++;
if(frameCounterMove >= womanleft.length)
frameCounterMove=0;
reload = 0;
}
}
public void turnTowards()
{
}
}
