content: /*
   * FXGL - JavaFX Game Library. The MIT License (MIT).
   * Copyright (c) AlmasB (almaslvl@gmail.com).
   * See LICENSE for details.
   */
  
  package sandbox;
  
  import com.almasb.fxgl.app.GameApplication;
  import com.almasb.fxgl.app.GameSettings;
  import com.almasb.fxgl.dsl.components.ExpireCleanComponent;
  import com.almasb.fxgl.entity.components.CollidableComponent;
  import com.almasb.fxgl.input.UserAction;
  import com.almasb.fxgl.physics.BoundingShape;
  import com.almasb.fxgl.physics.HitBox;
  import com.almasb.fxgl.physics.PhysicsComponent;
  import com.almasb.fxgl.physics.box2d.dynamics.BodyType;
  import com.almasb.fxgl.physics.box2d.dynamics.FixtureDef;
  import javafx.scene.input.MouseButton;
  import javafx.scene.paint.Color;
  import javafx.util.Duration;
  
  import static com.almasb.fxgl.dsl.FXGL.*;
  
  /**
   * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
   */
  public class PhysicsBounceSample extends GameApplication {
  
      private enum TType {
          BULLET, WALL
      }
  
      @Override
      protected void initSettings(GameSettings settings) {
          settings.setWidth(1280);
          settings.setHeight(720);
      }
  
      @Override
      protected void initInput() {
          getInput().addAction(new UserAction("LMB") {
              private double x;
              private double y;
  
              @Override
              protected void onActionBegin() {
                  x = getInput().getMouseXWorld();
                  y = getInput().getMouseYWorld();
              }
  
              @Override
              protected void onActionEnd() {
                  var endx = getInput().getMouseXWorld();
                  var endy = getInput().getMouseYWorld();
  
                  spawnBullet(x, y, endx - x, endy - y);
              }
          }, MouseButton.PRIMARY);
      }
  
      @Override
      protected void initGame() {
          getGameScene().setBackgroundColor(Color.LIGHTGRAY);
  
          var screenBounds = entityBuilder().buildScreenBoundsAndAttach(50);
  
          screenBounds.addComponent(new CollidableComponent(true));
          screenBounds.setType(TType.WALL);
      }
  
      @Override
      protected void initPhysics() {
          onCollisionBegin(TType.WALL, TType.BULLET, (wall, bullet) -> {
              var vx = bullet.getComponent(PhysicsComponent.class).getVelocityX();
              var vy = bullet.getComponent(PhysicsComponent.class).getVelocityY();
  
              // limit proximity spawns to 10
              if (getGameWorld().getEntitiesInRange(bullet.getBoundingBoxComponent().range(100, 100)).size() < 10) {
                  spawnBullet(bullet.getX() + 30 * -Math.signum(vx), bullet.getY() + 30 * -Math.signum(vy), -Math.signum(vx) * 20, -Math.signum(vy) * 20);
              }
          });
      }
  
      private void spawnBullet(double x, double y, double vx, double vy) {
          var physics = new PhysicsComponent();
          physics.setFixtureDef(new FixtureDef().density(25.5f).restitution(0.36f));
          physics.setBodyType(BodyType.DYNAMIC);
  
          physics.setOnPhysicsInitialized(() -> {
              physics.setLinearVelocity(vx * 10, vy * 10);
          });
  
          entityBuilder()
                  .at(x, y)
                  .type(TType.BULLET)
                  .bbox(new HitBox(BoundingShape.circle(450 / 15.0 / 2.0)))
                  .view(texture("ball.png", 450 / 15.0, 449 / 15.0))
                  .with(physics)
                  .with(new ExpireCleanComponent(Duration.seconds(5)).animateOpacity())
                  .collidable()
                  .buildAndAttach();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  