/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package tutorial;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.input.Input;
import com.almasb.fxgl.input.UserAction;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;

import java.util.Map;

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
public class BasicGameApp extends GameApplication {
    @Override
    protected void initSettings(GameSettings settings) {
        settings.setWidth(600);
        settings.setHeight(600);
        settings.setTitle("Basic Game App");
        settings.setVersion("0.1");
    }

    @Override
    protected void initInput() {
        Input input = FXGL.getInput();

        input.addAction(new UserAction("Move Right") {
            @Override
            protected void onAction() {
                player.translateX(5); // move right 5 pixels
                FXGL.getGameState().increment("pixelsMoved", +5);
            }
        }, KeyCode.D);

        input.addAction(new UserAction("Move Left") {
            @Override
            protected void onAction() {
                player.translateX(-5); // move left 5 pixels
                FXGL.getGameState().increment("pixelsMoved", +5);
            }
        }, KeyCode.A);

        input.addAction(new UserAction("Move Up") {
            @Override
            protected void onAction() {
                player.translateY(-5); // move up 5 pixels
                FXGL.getGameState().increment("pixelsMoved", +5);
            }
        }, KeyCode.W);

        input.addAction(new UserAction("Move Down") {
            @Override
            protected void onAction() {
                player.translateY(5); // move down 5 pixels
                FXGL.getGameState().increment("pixelsMoved", +5);
            }
        }, KeyCode.S);

        input.addAction(new UserAction("Play Sound") {
            @Override
            protected void onActionBegin() {
                FXGL.play("drop.wav");
            }
        }, KeyCode.F);
    }

    @Override
    protected void initGameVars(Map<String, Object> vars) {
        vars.put("pixelsMoved", 0);
    }

    private Entity player;

    @Override
    protected void initGame() {
        player = FXGL.entityBuilder()
                .at(300, 300)
                //.view(new Rectangle(25, 25, Color.BLUE))
                .view("brick.png")
                .buildAndAttach();
    }

    @Override
    protected void initUI() {
        Text textPixels = new Text();
        textPixels.setTranslateX(50); // x = 50
        textPixels.setTranslateY(100); // y = 100

        textPixels.textProperty().bind(FXGL.getGameState().intProperty("pixelsMoved").asString());

        FXGL.getGameScene().addUINode(textPixels); // add to the scene graph

        var brickTexture = FXGL.getAssetLoader().loadTexture("brick.png");
        brickTexture.setTranslateX(50);
        brickTexture.setTranslateY(450);

        FXGL.getGameScene().addUINode(brickTexture);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

@@ -304,16 +304,78 @@
 Entity;%0A
+import com.almasb.fxgl.entity.components.CollidableComponent;%0A
 import c
@@ -439,24 +439,73 @@
 UserAction;%0A
+import com.almasb.fxgl.physics.CollisionHandler;%0A
 import javaf
@@ -586,23 +586,20 @@
 e.shape.
-Rectang
+Circ
 le;%0Aimpo
@@ -625,17 +625,16 @@
 t.Text;%0A
-%0A
 import j
@@ -976,32 +976,89 @@
 (%220.1%22);%0A    %7D%0A%0A
+    public enum EntityType %7B%0A        PLAYER, COIN%0A    %7D%0A%0A
     @Override%0A  
@@ -2682,16 +2682,57 @@
 ilder()%0A
+                .type(EntityType.PLAYER)%0A
         
@@ -2773,35 +2773,264 @@
     
-//
 .view
-(new Rectangle(25, 2
+WithBBox(%22brick.png%22)%0A                .with(new CollidableComponent(true))%0A                .buildAndAttach();%0A%0A        FXGL.entityBuilder()%0A                .type(EntityType.COIN)%0A                .at(500, 200)%0A                .viewWithBBox(new Circle(1
 5, C
@@ -3038,12 +3038,14 @@
 lor.
-BLUE
+YELLOW
 ))%0A 
@@ -3064,58 +3064,478 @@
    .
-view(%22brick.png%22)%0A                .buildAndAttach(
+with(new CollidableComponent(true))%0A                .buildAndAttach();%0A    %7D%0A%0A    @Override%0A    protected void initPhysics() %7B%0A        FXGL.getPhysicsWorld().addCollisionHandler(new CollisionHandler(EntityType.PLAYER, EntityType.COIN) %7B%0A%0A            // order of types is the same as passed into the constructor%0A            @Override%0A            protected void onCollisionBegin(Entity player, Entity coin) %7B%0A                coin.removeFromWorld();%0A            %7D%0A        %7D
 );%0A 

update tutorial