package samodelkin.androidsimplestgamelibrary; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Rect; import samodelkin.androidsimplestgamelibrary.Layer; import samodelkin.androidsimplestgamelibrary.LayerSprite; public class LayerTiles extends Layer { private int[] cells; private int cols; private int rows; private Bitmap[] tiles; public LayerTiles(Resources res, int[] id, int[] cells, int cols, int rows) { super(); this.tiles = new Bitmap[id.length]; int i = 0; while (i < id.length) { this.tiles[i] = BitmapFactory.decodeResource(res, id[i]); i += 1; } this.cells = new int[cells.length]; this.cells = cells; this.cols = cols; this.rows = rows; } private int getNumberCell(int col, int row) { return (((this.cols * row) - (this.cols - col)) - 1); } private Rect getRect(int col, int row) { return new Rect((this.getTileWidth() * col), (this.getTileHeight() * row), ((this.getTileWidth() * col) + this.getTileWidth()), ((this.getTileHeight() * row) + this.getTileHeight())); } public boolean collidesWith(LayerSprite layerSprite) { boolean b = false; int col = 1; while (col < (this.cols + 1)) { int row = 1; while (row < (this.rows + 1)) { if (this.cells[(((this.cols * row) - (this.cols - col)) - 1)] == 0) { row += 1; } else { if (Rect.intersects(this.getRect((col - 1), (row - 1)), layerSprite.getBoundsRect())) { b = true; } row += 1; } } col += 1; } return b; } public int getCols() { return this.cols; } public int getRows() { return this.rows; } public int getTileHeight() { return this.tiles[0].getHeight(); } public int getTileWidth() { return this.tiles[0].getWidth(); } public void paint(Canvas c) { int col = 1; while (col < (this.cols + 1)) { int row = 1; while (row < (this.rows + 1)) { if (this.cells[(((this.cols * row) - (this.cols - col)) - 1)] == 0) { row += 1; } else { c.drawBitmap(this.tiles[(this.cells[(((this.cols * row) - (this.cols - col)) - 1)] - 1)], ((float) (((col - 1) * this.getTileWidth()))), ((float) (((row - 1) * this.getTileHeight()))), null); row += 1; } } col += 1; } } public void setCell(int col, int row, int index) { this.cells[this.getNumberCell(col, row)] = index; } public void setCells(int[] cells) { this.cells = new int[cells.length]; this.cells = cells; } }