Newer
Older
JumpingGame / src / main / java / views / TileMapRenderer.java
okina-kina on 19 Apr 2022 2 KB バグ修正
package views;

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

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) {
        JumpGameModel jumpGameModel = (JumpGameModel) 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.");
    }

    //---------------------------------------------------------------
}