package views; import entities.Pair; import entities.modelExtentions.Flags; 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.getFlags()); } //--------------------------------------------------------------- //--------------------------------------------------------------- // @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(Flags flags) { 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 < 100; i++) { double x = 32 * newTile.getScaleValue() * (i + space); // フラグのチェック if (flags.isFlag(i)) 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."); } //--------------------------------------------------------------- }