<commit_before>package ui;

import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import model.Entity;
import model.Player;
import model.Position;
import model.World;

import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

/**
 * Renders the world on the scene
 */
public class WorldRenderer implements Observer {
    private int scale = 20;
    private static final int BOUNDING_BOX_X = 10;
    private static final int BOUNDING_BOX_Y = 5;
    private static final Paint FLOOR_COLOR = Color.BEIGE;
    private final Player player;

    private Canvas worldCanvas;
    private World world;
    private Position localWorldCenter;
    private Point2D center;
    private Group group;

    /**
     * Connect this renderer to the given world
     * @param world world to render
     */
    WorldRenderer(World world, double width, double height, Player player) {
        group = new Group();
        worldCanvas = new Canvas(width, height);
        group.getChildren().add(worldCanvas);
        this.world = world;
        this.center = new Point2D(width / 2, height / 2);
        this.player = player;
        this.localWorldCenter = player.getPosition();

        world.addObserver(this);
        updateGroup();
    }

    private void updateGroup() {
        GraphicsContext graphicsContext = worldCanvas.getGraphicsContext2D();
        clearCanvas(graphicsContext);

        updateLocalWorldCenter();

        for (Position position : computeVisiblePositions()) {
            if (world.getActivePositions().contains(position)) {
                double x = (position.getX() - localWorldCenter.getX()) * scale + center.getX();
                double y = (position.getY() - localWorldCenter.getY()) * scale + center.getY();

                if (world.get(position).isPresent()) {
                    graphicsContext.setFill(world.get(position).get().getColor());
                } else {
                    graphicsContext.setFill(FLOOR_COLOR);
                }

                graphicsContext.fillRect(x, y, scale, scale);
            }
        }
    }

    private void updateLocalWorldCenter() {
        if (localWorldCenter.getX() - player.getPosition().getX() > BOUNDING_BOX_X) {
            localWorldCenter = localWorldCenter.add(-1,0);
        } else if (localWorldCenter.getX() - player.getPosition().getX() < - BOUNDING_BOX_X) {
            localWorldCenter = localWorldCenter.add(1,0);
        }

        if (localWorldCenter.getY() - player.getPosition().getY() > BOUNDING_BOX_Y) {
            localWorldCenter = localWorldCenter.add(0,-1);
        } else if (localWorldCenter.getY() - player.getPosition().getY() < - BOUNDING_BOX_Y) {
            localWorldCenter = localWorldCenter.add(0,1);
        }
    }

    private Set<Position> computeVisiblePositions() {
        Set<Position> visiblePositions = new HashSet<>();
        for (int x = (int) (-center.getX() / scale); x < center.getX() / scale; x++) {
            for (int y = (int) (-center.getY() / scale); y < center.getY() / scale; y++) {
                Position visiblePosition = new Position(
                        x + localWorldCenter.getX(),
                        y + localWorldCenter.getY());

                visiblePositions.add(visiblePosition);
            }
        }


        return visiblePositions;
    }

    private void clearCanvas(GraphicsContext graphicsContext) {
        graphicsContext.clearRect(0,0, worldCanvas.getWidth(), worldCanvas.getHeight());
    }

    /**
     * Produce true if the given position is on the screen
     */
    private boolean isOnScreen(Position position) {
        int x = (position.getX() + localWorldCenter.getX());
        int y = (position.getY() + localWorldCenter.getY());

        return ((- center.getX() / scale <= x && x <= center.getX() / scale) &&
                (- center.getY() / scale <= y && y <= center.getY() / scale));
    }

    /**
     * This method is called whenever the observed object is changed. An
     * application calls an <tt>Observable</tt> object's
     * <code>notifyObservers</code> method to have all the object's
     * observers notified of the change.
     *
     * @param o   the observable object.
     * @param arg an argument passed to the <code>notifyObservers</code>
     */
    @Override
    public void update(Observable o, Object arg) {
        if (!world.isEnded()) {
            if (isOnScreen((Position) arg)) {
                updateGroup();
            }
        } else {
            GraphicsContext graphicsContext = worldCanvas.getGraphicsContext2D();
            clearCanvas(graphicsContext);

            Text endText = new Text(center.getX(), center.getY(),
                    "Well done!" +
                            "\nYou " + (world.isWin()? "won" : "lost") +
                            "\nTime spent playing: " + player.getTime() + " s" +
                            "\nTotal number of moves made: " + player.getMoves() +
                            "\nNumber of coins collected: " + player.getCoins() +
                            "\nDust piles collected: " + player.getInventory().getOrDefault(Entity.DUST, 0));
            endText.setFill(Color.WHITE);

            group.getChildren().clear();
            group.getChildren().add(endText);
        }
    }

    void zoomIn() {
        scale++;
        updateGroup();
    }

    void zoomOut() {
        scale = Math.max(scale - 1, 1);
        updateGroup();
    }

    Node getGroup() {
        return group;
    }
}
<commit_msg>Finished working on the rendering only the changes<commit_after>package ui;

import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import model.Entity;
import model.Player;
import model.Position;
import model.World;

import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

/**
 * Renders the world on the scene
 */
public class WorldRenderer implements Observer {
    private int scale = 20;
    private static final int BOUNDING_BOX_X = 10;
    private static final int BOUNDING_BOX_Y = 5;
    private static final Paint FLOOR_COLOR = Color.BEIGE;
    private final Player player;

    private Canvas worldCanvas;
    private World world;
    private Position localWorldCenter;
    private Point2D center;
    private Group group;

    /**
     * Connect this renderer to the given world
     * @param world world to render
     */
    WorldRenderer(World world, double width, double height, Player player) {
        group = new Group();
        worldCanvas = new Canvas(width, height);
        group.getChildren().add(worldCanvas);
        this.world = world;
        this.center = new Point2D(width / 2, height / 2);
        this.player = player;
        this.localWorldCenter = player.getPosition();

        world.addObserver(this);
        updateGroup();
    }

    private void updateGroup() {
        GraphicsContext graphicsContext = worldCanvas.getGraphicsContext2D();
        clearCanvas(graphicsContext);

        updateLocalWorldCenter();

        for (Position position : computeVisiblePositions()) {
            if (world.getActivePositions().contains(position)) {
                double x = (position.getX() - localWorldCenter.getX()) * scale + center.getX();
                double y = (position.getY() - localWorldCenter.getY()) * scale + center.getY();

                if (world.get(position).isPresent()) {
                    graphicsContext.setFill(world.get(position).get().getColor());
                } else {
                    graphicsContext.setFill(FLOOR_COLOR);
                }

                graphicsContext.fillRect(x, y, scale, scale);
            }
        }
    }

    private void updateLocalWorldCenter() {
        if (localWorldCenter.getX() - player.getPosition().getX() > BOUNDING_BOX_X) {
            localWorldCenter = localWorldCenter.add(-1,0);
        } else if (localWorldCenter.getX() - player.getPosition().getX() < - BOUNDING_BOX_X) {
            localWorldCenter = localWorldCenter.add(1,0);
        }

        if (localWorldCenter.getY() - player.getPosition().getY() > BOUNDING_BOX_Y) {
            localWorldCenter = localWorldCenter.add(0,-1);
        } else if (localWorldCenter.getY() - player.getPosition().getY() < - BOUNDING_BOX_Y) {
            localWorldCenter = localWorldCenter.add(0,1);
        }
    }

    private Set<Position> computeVisiblePositions() {
        Set<Position> visiblePositions = new HashSet<>();
        for (int x = (int) (-center.getX() / scale); x < center.getX() / scale; x++) {
            for (int y = (int) (-center.getY() / scale); y < center.getY() / scale; y++) {
                Position visiblePosition = new Position(
                        x + localWorldCenter.getX(),
                        y + localWorldCenter.getY());

                visiblePositions.add(visiblePosition);
            }
        }


        return visiblePositions;
    }

    private void clearCanvas(GraphicsContext graphicsContext) {
        graphicsContext.clearRect(0,0, worldCanvas.getWidth(), worldCanvas.getHeight());
    }

    /**
     * Produce true if the given position is on the screen
     */
    private boolean isOnScreen(Position position) {
        int x = (position.getX() - localWorldCenter.getX());
        int y = (position.getY() - localWorldCenter.getY());

        return ((- center.getX() / scale <= x && x <= center.getX() / scale) &&
                (- center.getY() / scale <= y && y <= center.getY() / scale));
    }

    /**
     * This method is called whenever the observed object is changed. An
     * application calls an <tt>Observable</tt> object's
     * <code>notifyObservers</code> method to have all the object's
     * observers notified of the change.
     *
     * @param o   the observable object.
     * @param arg an argument passed to the <code>notifyObservers</code>
     */
    @Override
    public void update(Observable o, Object arg) {
        if (!world.isEnded()) {
            if (isOnScreen((Position) arg)) {
                updateGroup();
            }
        } else {
            GraphicsContext graphicsContext = worldCanvas.getGraphicsContext2D();
            clearCanvas(graphicsContext);

            Text endText = new Text(center.getX(), center.getY(),
                    "Well done!" +
                            "\nYou " + (world.isWin()? "won" : "lost") +
                            "\nTime spent playing: " + player.getTime() + " s" +
                            "\nTotal number of moves made: " + player.getMoves() +
                            "\nNumber of coins collected: " + player.getCoins() +
                            "\nDust piles collected: " + player.getInventory().getOrDefault(Entity.DUST, 0));
            endText.setFill(Color.WHITE);

            group.getChildren().clear();
            group.getChildren().add(endText);
        }
    }

    void zoomIn() {
        scale++;
        updateGroup();
    }

    void zoomOut() {
        scale = Math.max(scale - 1, 1);
        updateGroup();
    }

    Node getGroup() {
        return group;
    }
}

{'fraction_non_alphanumeric': 0.118822200990185, 'fraction_numerical': 0.004342916702857639, 'mean_word_length': 2.173649393605292, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5799870', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3140, 'n_words': 769}