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

import entities.Pair;
import entities.TileType;
import models.IModel;

import java.util.ArrayList;

//---------------------------------------------------------------
// タイル生成
public class TileMapRenderer implements IView {

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

    //---------------------------------------------------------------
    //---------------------------------------------------------------
    public TileMapRenderer(ArrayList<Integer> flags) {
        initTiles(flags);
    }

    //---------------------------------------------------------------
    //---------------------------------------------------------------
    //
    @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(ArrayList<Integer> flags) {
        boolean isOpen = false;
        int space = 20;

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

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

            // フラグのチェック
            if (isFlag(i, flags)) isOpen = !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.");
    }

    //---------------------------------------------------------------
    //---------------------------------------------------------------
    // フラグのタイミングと一致しているかをチェック
    private boolean isFlag(int timing, ArrayList<Integer> flags) {
        for (int flag : flags) return (timing == flag);
        return false;
    }
    //---------------------------------------------------------------
}