My game, space admiral, has you be able to control ships. I want these fighters to fly in a v-formation. My current code works and allows a single fighter to head towards a point. What doesn't work is the V-formation. How would I do this?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ships here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ships extends Player
{
public int maxSpeed;
public int fireRate = 20;
public GreenfootImage turnLeftPic;
public GreenfootImage turnRightPic;
public GreenfootImage origPic;
public int Xoffset = 0;
public int Yoffset = 0;
private int i=0;
/**
* Act - do whatever the Ships wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public void Shoot(int x, int y, Ships obj, int rate)
{
x+=Xoffset;
y+=Yoffset;
TurnToPoint(x,y,obj);
i++;
if(i>rate)
{
Bullets b = new Laser_Bolt(obj.getRotation());
getWorld().addObject(b, obj.getX(), obj.getY());
i = 0;
}
}
public void TurnToPoint(int x, int y,Ships obj)
{
int prevRot = obj.getRotation();
obj.turnTowards(x,y);
int newRot = obj.getRotation();
obj.setRotation(prevRot);
if(prevRot<0)
{
prevRot+=360;
}
if(newRot<0)
{
newRot+=360;
}
if(prevRot != newRot)
{
if(whichWay(prevRot,newRot))
{
obj.setRotation(prevRot + 1);
obj.setImage(obj.turnLeftPic);
}
else if(!whichWay(prevRot,newRot))
{
obj.setRotation(prevRot - 1);
obj.setImage(obj.turnRightPic);
}}
if(Math.abs(prevRot - newRot)<10)
{
obj.setImage(obj.origPic);
}
}
public void GoToPoint(int x, int y,Ships obj)
{
x+=Xoffset;
y+=Yoffset;
TurnToPoint(x,y,obj);
double distX = Math.abs(obj.getpX() - x);
double distY = Math.abs(obj.getpY() - y);
double dist = Math.sqrt((distX*distX)+(distY*distY));
if(dist>30)
{
if((dist/100)>obj.maxSpeed)
{
obj.move(obj,obj.maxSpeed, obj.getRotation());
}
else
{
obj.move(obj,(dist/100), obj.getRotation());
}
}
}
public boolean whichWay(int pr, int nr)
{
//if turning right is closer, return true, else, return false
int ab = Math.abs(pr-nr);
if(pr>nr)
{
if(ab>180){return true;} else {return false;}
}
if(nr>pr)
{
if(ab>180){return false;} else {return true;}
}
//if it exactly oppositite, just turn.
return false;
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Fighters here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Fighters extends Ships
{
int turn = 0;
/**
* Act - do whatever the Fighters wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public void FighterFlight(Fighters obj)
{
int i=0;
while(getWorld().getObjects(Fighters.class).size()>i)
{
Fighters o = (Fighters) getWorld().getObjects(Fighters.class).get(i);
if ((i & 1) == 0 ){
o.Xoffset=i*50;
o.Yoffset=i*50;
} else {
o.Xoffset=-i*50;
o.Yoffset=i*50;
}
i++;
}
}
}


