content: package com.almasb.fxglgames.flappy;
  
  import com.almasb.fxgl.dsl.FXGL;
  import com.almasb.fxgl.entity.component.Component;
  import com.almasb.fxgl.entity.components.CollidableComponent;
  import javafx.scene.shape.Rectangle;
  
  import static com.almasb.fxgl.dsl.FXGL.entityBuilder;
  
  /**
   * @author Almas Baimagambetov (almaslvl@gmail.com)
   */
  public class WallBuildingComponent extends Component {
  
      private double lastWall = 1000;
  
      @Override
      public void onUpdate(double tpf) {
          if (lastWall - entity.getX() < FXGL.getAppWidth()) {
              buildWalls();
          }
      }
  
      private Rectangle wallView(double width, double height) {
          Rectangle wall = new Rectangle(width, height);
          wall.setArcWidth(25);
          wall.setArcHeight(25);
          wall.fillProperty().bind(FXGL.getWorldProperties().objectProperty("stageColor"));
          return wall;
      }
  
      private void buildWalls() {
          double height = FXGL.getAppHeight();
          double distance = height / 2;
  
          for (int i = 1; i <= 10; i++) {
              double topHeight = Math.random() * (height - distance);
  
              entityBuilder()
                      .at(lastWall + i * 500, 0 - 25)
                      .type(EntityType.WALL)
                      .viewWithBBox(wallView(50, topHeight))
                      .with(new CollidableComponent(true))
                      .buildAndAttach();
  
              entityBuilder()
                      .at(lastWall + i * 500, 0 + topHeight + distance + 25)
                      .type(EntityType.WALL)
                      .viewWithBBox(wallView(50, height - distance - topHeight))
                      .with(new CollidableComponent(true))
                      .buildAndAttach();
          }
  
          lastWall += 10 * 500;
      }
  }
  