import greenfoot.*;
public class MiniRed extends Actor
{
private int charge = 0;
int removeTimer=0,count=0;
Ray ray=new Ray();
private int shotTime;
public MiniRed(int speed, int rate)
{
charge = rate;
getImage().scale(25, 25);
ray.setImage(new GreenfootImage(1,1));
}
protected void addedToWorld(World world)
{
world.addObject(ray, 0,0);
}
public void act()
{
MyWorld world1 = (MyWorld) getWorld();
if(getWorld()!=null && getWorld().getObjects(B.class).size()>0)
{
B b = getWorld().getObjects(B.class).get(0);
setLocation(b.getX(), b.getY()+75);
}
if(isTouching(MiniRed.class))
setLocation(getX()-40, getY());
if(isTouching(Stone.class) || isTouching(EnemyTank.class) || getWorld().getObjects(B.class).isEmpty() || isTouching(Crate.class))
{
getImage().setTransparency(0);
}
else if(!isTouching(EnemyTank.class) || !isTouching(Crate.class)
|| !isTouching(Stone.class) || !getWorld().getObjects(B.class).isEmpty())
getImage().setTransparency(255);
shotTime=(shotTime+1)%40;
if(shotTime!=0)
return;
if(getWorld()!=null && getWorld().getObjects(EnemyTank.class).size()>0 && getWorld().getObjects(B.class).size()>0)
{
Actor bot=(Actor)getWorld().getObjects(EnemyTank.class).get(0);
B bb = getWorld().getObjects(B.class).get(0);
if(ray.isUnobstructed(this,bot))
{
turnTowards(bot.getX(), bot.getY());
Projectile1 projectile1=new Projectile1();
if(!getWorld().getObjects(B.class).isEmpty())
getWorld().addObject(projectile1, getX(),getY());
projectile1.getImage().scale(17, 7);
projectile1.setRotation(getRotation());
/**
* it doesn't work. it should turn toward the blue bot and shoot it unless
* and untill thier is no obstacle infront of it
* it only works properly when blue bot is not in world
*/
}else setRotation(bb.getRotation());//setRotation is not working(even if the bot is not in the world)...it should rotate as same as B class when their is no obstacle infornt of it
//return;
}
ray.setRotation(0);
ray.setLocation(0, 0);
}
public void Remove()
{
removeTimer++;
if(removeTimer>380 && removeTimer<500)
count++;
if(count<10 && count>0)
getImage().setTransparency(255);
else if(count>10 && count<20)
getImage().setTransparency(0);
else if(count>20) count=0;
if(removeTimer>500 || isTouching(MiniRed.class))
getWorld().removeObject(this);
}
private class Ray extends Actor
{
public boolean isUnobstructed(Actor actorA,Actor actorB)
{
int ax=actorA.getX();
int ay=actorA.getY();
int bx=actorB.getX();
int by=actorB.getY();
int length=(int)Math.hypot(by-ay, bx-ax);
setImage(new GreenfootImage(length,8));
setLocation(ax,ay);
turnTowards(bx,by);
move(length/2);
return getIntersectingObjects(Obstacle.class).size()==0;
}
}
}

