package clouds;
import java.util.Random;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import mainPac.BWNode;
import mainPac.World;
public class Clouds extends BWNode {
private static final Image image = new Image(Clouds.class.getResourceAsStream("cloud.png"));
private Random rand = new Random();
public Clouds(World world) {
super(world);
setTranslateZ(1100);
for (int i = -2000; i < 1900; i += 300) {
Node node = createNode(
i + rand.nextInt(200),
getNextY());
getChildren().add(node);
}
}
private int getNextY(){
return rand.nextInt(800)-200;
}
public static Node createNode(double x, double y) {
ImageView imageView = new ImageView(image);
imageView.setTranslateX(x);
imageView.setTranslateY(y);
return imageView;
}
@Override
public void stepSimulation() {
double planeX = world.getPlane().getTranslateX();
for (Node nodeIterator : getChildren()) {
double x = nodeIterator.getTranslateX();
double cloudPosRelativeToPlane = x - planeX;
if (cloudPosRelativeToPlane < -2000) {
x += 4000 + rand.nextInt(100);
nodeIterator.setTranslateX(x);
nodeIterator.setTranslateY(getNextY());
} else if (cloudPosRelativeToPlane > 2000) {
x -= 4000 + rand.nextInt(100);
nodeIterator.setTranslateX(x);
nodeIterator.setTranslateY(getNextY());
}
}
}
}