Anonymous Anonymous - 2 months ago
136 0

blaböa

ActionScript

setOnKey(DO NOTHING)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package discaptive.view;

import discaptive.controller.InterfaceController;
import discaptive.model.InterfaceModel;
import discaptive.model.Moveable;
import discaptive.model.Tile;
import discaptive.controller.Movement;
import javafx.geometry.HPos;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.StrokeType;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.util.Duration;

/**
 *
 * @author Jonas
 */
public class View extends Region implements InterfaceView {

    public InterfaceModel model;
    public InterfaceController controller;
    public Node playerGraphic;
    public int east = 0;
    public int south = 90;
    public int west = 180;
    public int north = 270;
    public GridPane grid = new GridPane();

    public View(InterfaceModel<Tile, Moveable> model, InterfaceController controller) {
        this.model = model;
        this.controller = controller;
        setOnMouseClicked(new MouseClickHandler());
        setOnKeyPressed(new KeyPressHandler());

        for (int i = 0; i < InterfaceModel.MAXCOLLUMS; i++) {
            for (int j = 0; j < InterfaceModel.MAXROWS; j++) {
                Tile tileM = model.getTileAt(j, i);
                Node tile = null;
                if (tileM != null) {
                    if (tileM.isWall()) {
                        tile = makeWallGraphic();
                    } else if (tileM.isEmptyPassage()) {
                        tile = makeFloorGraphic();
                    } else if (tileM.isPlayerGoal()) {
                        tile = makePlayerGoalGraphic();
                    } else if (tileM.isPitfall()) {
                        if (tileM.isClosed()) {
                            tile = makePitfallCloseGraphic();
                        } else {
                            tile = makePitfallOpenGraphic();
                        }
                    } else if (tileM.isGuardTurn()) {
                        if (tileM.isLeft()) {
                            tile = makeGuardTurnLeftGraphic();
                        } else {
                            tile = makeGuardTurnRightGraphic();
                        }
                    } else if (tileM.isSensor()) {
                        tile = makeSensorGraphic(tileM.getCounter());
                    }
                    if (tile != null) {
                        GridPane.setConstraints(tile, i, j);
                        grid.getChildren().addAll(tile);
                    }
                }
            }
        }

        for (int i = 0; i < InterfaceModel.MAXCOLLUMS; i++) {
            for (int j = 0; j < InterfaceModel.MAXROWS; j++) {
                Moveable moveM = model.getMoveableAt(j, i);

                Node move = null;

                if (moveM != null) {

                    if (moveM.isPlayer()) {
                        move = makePlayerGraphic();
                        playerGraphic = move;
                    } else if (moveM.isBox()) {
                        move = makeBoxGraphic();
                    } else if (moveM.isGuard()) {
                        int richtung = moveM.blickRichtung();
                        if (!(richtung == 0)) {
                            switch (richtung) {
                                case 1:
                                    move = makeGuardGraphic(east);
                                    break;
                                case 2:
                                    move = makeGuardGraphic(south);
                                    break;
                                case 3:
                                    move = makeGuardGraphic(west);
                                    break;
                                case 4:
                                    move = makeGuardGraphic(north);
                                    break;
                                default:
                                    break;
                            }
                        }

                    }
                }

                if (move != null) {
                    /*Halignment : Zentriert die Moveables 
                    in die mitte des GridPaneKasten*/
                    GridPane.setHalignment(move, HPos.CENTER);
                    GridPane.setConstraints(move, i, j);
                    grid.getChildren().addAll(move);
                }

            }
        }

        this.getChildren().add(grid);
        model.registerView(this);

    }

    @Override
    public void updateTile(int x, int y) {
        Tile tileM = (Tile) model.getTileAt(x, y);
        Node tile = null;
        if (tileM != null) {
            if (tileM.isWall()) {
                tile = makeWallGraphic();
            } else if (tileM.isEmptyPassage()) {
                tile = makeFloorGraphic();
            } else if (tileM.isPlayerGoal()) {
                tile = makePlayerGoalGraphic();
            } else if (tileM.isPitfall()) {
                if (tileM.isClosed()) {
                    tile = makePitfallCloseGraphic();
                } else {
                    tile = makePitfallOpenGraphic();
                }
            } else if (tileM.isGuardTurn()) {
                if (tileM.isLeft()) {
                    tile = makeGuardTurnLeftGraphic();
                } else {
                    tile = makeGuardTurnRightGraphic();
                }
            } else if (tileM.isSensor()) {
                tile = makeSensorGraphic(tileM.getCounter());
            }

            if (tile != null) {
                GridPane.setConstraints(tile, y, x);
                grid.getChildren().addAll(tile);
            }
        }
    }

    @Override
    public void updatePlayer(int x, int y) {
        Moveable moveM = (Moveable) model.getMoveableAt(x, y);
        Node move = null;
        if (moveM != null) {
            if (moveM.isPlayer()) {
                /*Alte Grafik löschen & neue erzeugen*/
                grid.getChildren().remove(playerGraphic);
                move = makePlayerGraphic();
                playerGraphic = move;
            }
        }
        if (move != null) {
            GridPane.setConstraints(move, y, x);
            GridPane.setHalignment(move, HPos.CENTER);
            grid.getChildren().addAll(move);
        }
    }

    @Override
    public void updateGuard(int x, int y) {
        Moveable moveM = (Moveable) model.getMoveableAt(x, y);

        Node move = null;
        if (moveM != null) {
            if (moveM.isGuard()) {
                int richtung = moveM.blickRichtung();
                if (!(richtung == 0)) {
                    grid.getChildren().remove(move);
                    switch (richtung) {
                        case 1:
                            move = makeGuardGraphic(east);
                            break;
                        case 2:
                            move = makeGuardGraphic(south);
                            break;
                        case 3:
                            move = makeGuardGraphic(west);
                            break;
                        case 4:
                            move = makeGuardGraphic(north);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        if (move != null) {
            GridPane.setConstraints(move, y, x);
            GridPane.setHalignment(move, HPos.CENTER);
            grid.getChildren().addAll(move);
        }
    }

    @Override
    public void updateBox(int x, int y) {
        Moveable moveM = (Moveable) model.getMoveableAt(x, y);

        Node move = null;
        if (moveM != null) {
            if (moveM.isBox()) {
                if (!moveM.isFallen()) {
                    move = makeBoxGraphic();
                }
            }
        }
        if (move != null) {
            GridPane.setConstraints(move, y, x);
            GridPane.setHalignment(move, HPos.CENTER);
            grid.getChildren().addAll(move);
        }
    }

    @Override
    public void levelComplete() {
        System.out.println("FINISH");
        // STATUS LINE HIER EINBAUEN STATT SOUT!!!
        controller.handleComplete(this);
    }

    @Override
    public void flashPlayer() {
        Shape player = (Shape) playerGraphic;
        Timeline tl = new Timeline(
                new KeyFrame(new Duration(0),
                        new KeyValue(player.fillProperty(), Color.GREEN)),
                new KeyFrame(new Duration(125),
                        new KeyValue(player.fillProperty(), Color.RED)),
                new KeyFrame(new Duration(250),
                        new KeyValue(player.fillProperty(), Color.GREEN)));
        tl.play();
    }

    @Override
    public void levelFailedCatched() {
        System.out.println("Level Failed!! You were detected");
        controller.handleComplete(this);
    }

    

    protected Circle makeCircle(Color fill, Color stroke) {
        Circle c = new Circle(25, 25, 20);
        c.setFill(fill);
        c.setStroke(stroke);
        c.setStrokeWidth(3);
        c.setStrokeType(StrokeType.INSIDE);
        return c;
    }

    private Node makePlayerGraphic() {
        return makeCircle(Color.GREEN, Color.BLACK);
    }

    private Rectangle makeTileRect(Color fill, Color stroke) {
        Rectangle r = new Rectangle(0, 0, 50, 50);
        r.setFill(fill);
        if (stroke != null) {
            r.setStroke(stroke);
            r.setStrokeWidth(1);
            r.setStrokeType(StrokeType.INSIDE);
        }
        return r;
    }

    private Node makeWallGraphic() {
        return makeTileRect(Color.DIMGRAY, Color.BLACK);
    }

    private Node makeFloorGraphic() {
        return makeTileRect(Color.YELLOW, Color.GOLDENROD);
    }

    private Node makePlayerGoalGraphic() {
        return makeTileRect(Color.GREEN, Color.BLACK);
    }

    private Rectangle makeBoxGraphic() {
        Rectangle r = new Rectangle(0, 0, 40, 40);
        r.setFill(Color.SADDLEBROWN);
        if (Color.ALICEBLUE != null) {
            r.setStroke(Color.BLACK);
            r.setStrokeWidth(3);
            r.setStrokeType(StrokeType.INSIDE);
        }
        return r;
    }

    private Rectangle makePitfallOpenGraphic() {
        Rectangle r = new Rectangle(0, 0, 50, 50);
        r.setFill(Color.BLACK);
        if (Color.ALICEBLUE != null) {
            r.setStroke(Color.GOLDENROD);
            r.setStrokeWidth(10);
            r.setStrokeType(StrokeType.INSIDE);
        }
        return r;
    }

    private Rectangle makePitfallCloseGraphic() {
        Rectangle r = new Rectangle(0, 0, 50, 50);
        r.setFill(Color.SADDLEBROWN);
        if (Color.ALICEBLUE != null) {
            r.setStroke(Color.GOLDENROD);
            r.setStrokeWidth(10);
            r.setStrokeType(StrokeType.INSIDE);
        }
        return r;
    }

    private Group makeGuardTurnLeftGraphic() {
        Group lefti = new Group();
        Label left = new Label("L");
        left.setScaleX(2);
        left.setScaleY(2);
        left.setTranslateX(21);
        left.setTranslateY(17);
        lefti.getChildren().addAll(makeFloorGraphic(), left);
        return lefti;
    }

    private Group makeGuardTurnRightGraphic() {
        Group righti = new Group();
        Label right = new Label("R");
        right.setScaleX(2);
        right.setScaleY(2);
        right.setTranslateX(21);
        right.setTranslateY(17);
        righti.getChildren().addAll(makeFloorGraphic(), right);
        return righti;
    }

    private Node makeGuardGraphic(int rotate) {
        Circle head = makeCircle(Color.PURPLE, Color.BLACK);
        Rectangle cap = new Rectangle(25, 25, 25, 10);
        cap.setFill(Color.BLUE);
        Group guard = new Group(head, cap);
        guard.setRotate(rotate);
        return guard;
    }

    private Group makeSensorGraphic(int counter) {
        Group counti = new Group();
        Label count = new Label(String.valueOf(counter));
        count.setScaleX(2);
        count.setScaleY(2);
        count.setTranslateX(21);
        count.setTranslateY(17);
        counti.getChildren().addAll(makeSensorFloorGraphic(), count);
        return counti;
    }

    private Node makeSensorFloorGraphic() {
        return makeTileRect(Color.DEEPPINK, Color.GOLDENROD);
    }

    /**
     * handles KeyPressed from Player and informs controller.
     */
    private class KeyPressHandler implements EventHandler<KeyEvent> {

        @Override
        public void handle(KeyEvent event) {           
            KeyCode key = event.getCode();
            Movement dir = null;
            switch (key) {
                case UP:
                case KP_UP:
                case W:
                    dir = Movement.NORTH;
                    break;
                case DOWN:
                case KP_DOWN:
                case S:
                    dir = Movement.SOUTH;
                    break;
                case LEFT:
                case KP_LEFT:
                case A:
                    dir = Movement.WEST;
                    break;
                case RIGHT:
                case KP_RIGHT:
                case D:
                    dir = Movement.EAST;
                    break;
            }
            if (dir != null) {
                controller.handleMove(View.this, dir);
                event.consume();
            }
        }
    }
    
    /**
     * handles MouseClick from Player and informs controller.
     */
    private class MouseClickHandler implements EventHandler<MouseEvent> {

        @Override
        public void handle(MouseEvent event) {            
            int y = ((int) event.getX() / 50);
            int x = ((int) event.getY() / 50);
            if (x <= InterfaceModel.MAXROWS && y <= InterfaceModel.MAXCOLLUMS) {
                controller.handleClick(View.this, x, y);
                event.consume();
            }
        }
    }

}