Newer
Older
JumpingGame / src / main / java / views / TileMapRenderer.java
package views;

import entities.Pair;
import entities.modelExtentions.Stage;
import models.IModel;
import models.JumpingGameModel;

import java.util.ArrayList;

public class TileMapRenderer implements IView {

    private double offsetY = 32d;
    private TileRenderer newTile = new TileRenderer("resources/tile.png", null, 2);
    private ArrayList<TileRenderer> tiles = new ArrayList<>();

    public TileMapRenderer(IModel model) {
        JumpingGameModel jumpGameModel = (JumpingGameModel) model;
        initTiles(jumpGameModel.getStage());
    }

    @Override
    public void update(IModel model) {
        for (TileRenderer tile : tiles) tile.update(model);
    }

    @Override
    public void display() {
        for (TileRenderer tile : tiles) tile.display();
    }

    @Override
    public void delete() {
        for (TileRenderer tile : tiles) tile.delete();
    }


    private void initTiles(Stage stage) {
        boolean isOpen = false;
        int space = 10;

        for (int i = 0; i < space; i++) {
            double x = 32 * newTile.getScaleValue() * i;
            if (i == 12) addNewTile(x, TileType.OPEN);
            else addNewTile(x, TileType.CLOSE);
        }

        for (int i = 0; i < 120; i++) {
            double x = 32 * newTile.getScaleValue() * (i + space);

            if (stage.isFlag(i)){
                isOpen = !isOpen;
                System.out.println(isOpen);
            }

            if (isOpen) addNewTile(x, TileType.OPEN);
            else addNewTile(x, TileType.CLOSE);

        }
    }

    private void addNewTile(double x, TileType tileType) {

        switch (tileType) {
            case CLOSE:
                newTile = new TileRenderer("resources/tile.png", new Pair<>(x, offsetY), 2);
                break;
            case OPEN:
                newTile = new TileRenderer("resources/hole.png", new Pair<>(x, offsetY), 2);
                break;
        }
        tiles.add(newTile);
        System.out.println("New tile created.");
    }
}