I'm making a game in which the player-class 'Man' can shoot. When you click your mouse, a bullet will go to the position on the moment of clicking,
There also is an Enemy-class which occasionally shoots towards the Man-class.
But there's a problem: because of the inaccuracy of the direction (caused by the fact that the world exists out of pixels), you can't shoot very accurate. There are only a few different directions in which you can shoot, and if your mouse is between one of those, it just choses the most nearby one.
So I want to make the bullets turn to their destination every act. But when they have reached their destination, they should go straight ahead.
So I made it stop turning towards it's destination when it's been near it, but it isn't working:
It still isn't accurate.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Bullet extends Movers
{
private int speed;
private int destinationX;
private int destinationY;
private String myTarget;
private String mySender;
private boolean isTouchingBlock = false;
private boolean hasToRemove = false;
private int margin = 5;
private boolean passedGoTo = false;
public Bullet(int targetX, int targetY, String senderClass, String targetClass)
{
setImage("Bullet" + senderClass + ".png");
getImage().scale(25,5);
speed = 3;
destinationX = targetX;
destinationY = targetY;
mySender = senderClass;
myTarget = targetClass;
}
protected void addedToWorld(World MyWorld)
{
turnTowards(destinationX, destinationY);
}
public void act()
{
move(10);
keepTurningI();
checkTouching();
}
private void checkTouching()
{
if (canGetWorldManEnemy())
{
if ( isTouching(Enemy.class) && myTarget == "Enemy" )
{
Enemy enemy = getWorld().getObjects(Enemy.class).get(0);
enemy.hitEnemy();
hasToRemove = true;
}
else if ( isTouching(Man.class) && myTarget == "Man" )
{
Man man = getWorld().getObjects(Man.class).get(0);
man.hitMan();
hasToRemove = true;
}
else if (getOneIntersectingObject(Block.class) != null || getOneIntersectingObject(Door.class) != null || isAtEdge() || isTouching(Bullet.class) || getOneIntersectingObject(Weapon.class) != null)
{
hasToRemove = true;
}
else if (isTouching(Bullet.class))
{
removeTouching(Bullet.class);
getWorld().removeObject(this);
}
else if (getOneIntersectingObject(ForceField.class) != null && mySender != "Man")
{
hasToRemove = true;
}
else
{
hasToRemove = false;
}
}
if (hasToRemove == true)
{
getWorld().removeObject(this);
}
}
private boolean isNearbyGoToPoint()
{
if ( isNearbyX() && isNearbyY() )
{
return true;
}
else
{
return false;
}
}
private void keepTurningI()
{
if (isNearbyX() && isNearbyY())
{
passedGoTo = true;
}
if (passedGoTo == false)
{
turnTowards(destinationX, destinationY);
}
if (passedGoTo == false)
{
turnTowards(destinationX, destinationY);
}
}
public boolean isNearbyX()
{
if ( getX() < destinationX + margin || getX() > destinationX - margin )
{
return true;
}
else
{
return false;
}
}
public boolean isNearbyY()
{
if ( getY() < destinationY + margin || getY() > destinationY - margin )
{
return true;
}
else
{
return false;
}
}
public String getSender()
{
return mySender;
}
}
