added spawn area
parent
fd02dcffd1
commit
08b192a6a5
|
|
@ -2,6 +2,7 @@ package org.agp8x.android.games.fillgrid;
|
||||||
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Pair;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
|
|
||||||
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
||||||
|
|
@ -18,10 +19,14 @@ public class FillGrid extends AppCompatActivity {
|
||||||
//GridObject<Asdf> ago = new GridBlock<>(Asdf.A);
|
//GridObject<Asdf> ago = new GridBlock<>(Asdf.A);
|
||||||
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
|
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
|
||||||
|
|
||||||
|
/* Pair<Float, Float> fp = new Pair<>(1.0f, 2.0f);
|
||||||
|
Pair<Integer, Integer> ip = new Pair<>(1, 2);
|
||||||
|
ip = new Pair<>(fp.first.intValue(), fp.second.intValue());*/
|
||||||
FillGridView gridview = new FillGridView(this);
|
FillGridView gridview = new FillGridView(this);
|
||||||
layout.addView(gridview);
|
layout.addView(gridview);
|
||||||
}
|
}
|
||||||
private enum Asdf{
|
|
||||||
A,B,C;
|
/* private enum Asdf {
|
||||||
}
|
A, B, C;
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import android.view.View;
|
||||||
|
|
||||||
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
||||||
import org.agp8x.android.games.fillgrid.data.GridBoard;
|
import org.agp8x.android.games.fillgrid.data.GridBoard;
|
||||||
|
import org.agp8x.android.games.fillgrid.data.GridObject;
|
||||||
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||||
import org.agp8x.android.games.fillgrid.data.Offset;
|
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||||
|
|
||||||
|
|
@ -23,11 +24,16 @@ import java.util.Map;
|
||||||
|
|
||||||
public class FillGridView extends View {
|
public class FillGridView extends View {
|
||||||
protected Pair<Integer, Integer> contentSize = null;
|
protected Pair<Integer, Integer> contentSize = null;
|
||||||
|
protected Pair<Integer, Integer> spawnSize = null;
|
||||||
protected GridBoard<GridBlock<GroupCell>, GroupCell> board;
|
protected GridBoard<GridBlock<GroupCell>, GroupCell> board;
|
||||||
protected List<GridBlock<GroupCell>> blocks;
|
protected List<GridBlock<GroupCell>> blocks;
|
||||||
protected Map<Offset, GroupCell> background;
|
protected Map<Offset, GroupCell> background;
|
||||||
protected GroupGridPainter painter;
|
protected GroupGridPainter painter;
|
||||||
protected GroupGridPainter bgPainter;
|
protected GroupGridPainter bgPainter;
|
||||||
|
private float divison;
|
||||||
|
private ArrayList<GridObject<GroupCell>> spawn;
|
||||||
|
private int spawnCount;
|
||||||
|
private SpawnPainterGroup<GroupCell> spawnPainter;
|
||||||
|
|
||||||
public FillGridView(Context context) {
|
public FillGridView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
|
@ -54,6 +60,29 @@ public class FillGridView extends View {
|
||||||
board = new GridBoard<>(dimensions);
|
board = new GridBoard<>(dimensions);
|
||||||
|
|
||||||
blocks = new ArrayList<>();
|
blocks = new ArrayList<>();
|
||||||
|
|
||||||
|
background = new HashMap<>();
|
||||||
|
GroupCell bgCell = new GroupCell(-1);
|
||||||
|
for (int i = 0; i < dimensions.x; i++) {
|
||||||
|
for (int j = 0; j < dimensions.y; j++) {
|
||||||
|
background.put(new Offset(i, j), bgCell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CellPaintProvider bgCpp = new CellPaintProviderBackground();
|
||||||
|
bgPainter = new GroupGridPainter(dimensions, bgCpp);
|
||||||
|
|
||||||
|
CellPaintProvider cpp = new CellPaintProviderDefault();
|
||||||
|
painter = new GroupGridPainter(dimensions, cpp);
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: create configuration
|
||||||
|
spawnCount = 3;
|
||||||
|
spawnPainter = new SpawnPainterGroup<>(spawnCount);
|
||||||
|
spawn = new ArrayList<>(spawnCount);
|
||||||
|
|
||||||
|
divison = 0.5625f; // divides *HD-resolutions in a square and a non-square
|
||||||
|
|
||||||
GridBlock<GroupCell> block0 = new GridBlock<>();
|
GridBlock<GroupCell> block0 = new GridBlock<>();
|
||||||
block0.getObjects().put(new Offset(0, 0), new GroupCell(0));
|
block0.getObjects().put(new Offset(0, 0), new GroupCell(0));
|
||||||
blocks.add(block0);
|
blocks.add(block0);
|
||||||
|
|
@ -67,21 +96,10 @@ public class FillGridView extends View {
|
||||||
block0.getObjects().put(new Offset(0, 2), new GroupCell(2));
|
block0.getObjects().put(new Offset(0, 2), new GroupCell(2));
|
||||||
blocks.add(block0);
|
blocks.add(block0);
|
||||||
|
|
||||||
background = new HashMap<>();
|
|
||||||
GroupCell bgCell = new GroupCell(-1);
|
|
||||||
for (int i = 0; i < dimensions.x; i++) {
|
|
||||||
for (int j = 0; j < dimensions.y; j++) {
|
|
||||||
background.put(new Offset(i, j), bgCell);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CellPaintProvider bgCpp = new CellPaintProviderBackground();
|
|
||||||
bgPainter = new GroupGridPainter(dimensions,bgCpp);
|
|
||||||
|
|
||||||
CellPaintProvider cpp = new CellPaintProviderDefault();
|
|
||||||
painter = new GroupGridPainter(dimensions, cpp);
|
|
||||||
board.drop(new Offset(4, 4), makeSquare());
|
board.drop(new Offset(4, 4), makeSquare());
|
||||||
board.drop(new Offset(4,4), makeAngle());
|
board.drop(new Offset(4, 4), makeAngle());
|
||||||
|
|
||||||
|
populateSpawnArea();
|
||||||
}
|
}
|
||||||
|
|
||||||
private GridBlock<GroupCell> makeSquare() {
|
private GridBlock<GroupCell> makeSquare() {
|
||||||
|
|
@ -90,6 +108,7 @@ public class FillGridView extends View {
|
||||||
grid.getObjects().put(new Offset(0, 1), new GroupCell(0));
|
grid.getObjects().put(new Offset(0, 1), new GroupCell(0));
|
||||||
grid.getObjects().put(new Offset(1, 0), new GroupCell(0));
|
grid.getObjects().put(new Offset(1, 0), new GroupCell(0));
|
||||||
grid.getObjects().put(new Offset(1, 1), new GroupCell(0));
|
grid.getObjects().put(new Offset(1, 1), new GroupCell(0));
|
||||||
|
blocks.add(grid);
|
||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,6 +119,7 @@ public class FillGridView extends View {
|
||||||
grid.getObjects().put(new Offset(2, 2), new GroupCell(1));
|
grid.getObjects().put(new Offset(2, 2), new GroupCell(1));
|
||||||
grid.getObjects().put(new Offset(2, 1), new GroupCell(1));
|
grid.getObjects().put(new Offset(2, 1), new GroupCell(1));
|
||||||
grid.getObjects().put(new Offset(2, 0), new GroupCell(1));
|
grid.getObjects().put(new Offset(2, 0), new GroupCell(1));
|
||||||
|
blocks.add(grid);
|
||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,16 +127,48 @@ public class FillGridView extends View {
|
||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
if (contentSize == null) {
|
if (contentSize == null || spawnSize == null) {
|
||||||
//TODO: maybe fix for square? optional?
|
calculateAreaSizes();
|
||||||
int paddingTop = getPaddingTop();
|
System.out.println(contentSize + " :: " + spawnSize);
|
||||||
int paddingBottom = getPaddingBottom();
|
|
||||||
int paddingLeft = getPaddingLeft();
|
|
||||||
int paddingRight = getPaddingRight();
|
|
||||||
contentSize = new Pair<>(getHeight() - paddingTop - paddingBottom, getWidth() - paddingLeft - paddingRight);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bgPainter.draw(canvas, background, contentSize);
|
bgPainter.draw(canvas, background, contentSize);
|
||||||
painter.draw(canvas, board.getGrid(), contentSize);
|
painter.draw(canvas, board.getGrid(), contentSize);
|
||||||
|
for (int i = 0; i < spawnCount; i++) {
|
||||||
|
spawnPainter.draw(canvas,i,spawn.get(i),spawnSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculateAreaSizes() {
|
||||||
|
int paddingTop = getPaddingTop();
|
||||||
|
int paddingBottom = getPaddingBottom();
|
||||||
|
int paddingLeft = getPaddingLeft();
|
||||||
|
int paddingRight = getPaddingRight();
|
||||||
|
int height = getHeight();
|
||||||
|
int width = getWidth();
|
||||||
|
int contentHeight = height - paddingTop - paddingBottom;
|
||||||
|
int contentWidth = width - paddingLeft - paddingRight;
|
||||||
|
int spawnHeight = contentHeight;
|
||||||
|
int spawnWidth = contentWidth;
|
||||||
|
if (contentHeight > contentWidth) {
|
||||||
|
spawnHeight = contentHeight - contentWidth;
|
||||||
|
contentHeight = contentWidth;
|
||||||
|
} else {
|
||||||
|
spawnWidth = contentWidth - contentHeight;
|
||||||
|
contentWidth = contentHeight;
|
||||||
|
}
|
||||||
|
contentSize = new Pair<>(contentHeight, contentWidth);
|
||||||
|
spawnSize = new Pair<>(spawnHeight, spawnWidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int divideSide(int sideLength) {
|
||||||
|
return (int) (sideLength * divison);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void populateSpawnArea() {
|
||||||
|
for (int i = 0; i < spawnCount; i++) {
|
||||||
|
GridObject<GroupCell> obj = blocks.get((int) (blocks.size() * Math.random()));
|
||||||
|
spawn.add(i, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package org.agp8x.android.games.fillgrid;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
|
import org.agp8x.android.games.fillgrid.data.GridObject;
|
||||||
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||||
import org.agp8x.android.games.fillgrid.data.Offset;
|
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||||
|
|
||||||
|
|
@ -21,7 +22,7 @@ public class GroupGridPainter implements GridPainter {
|
||||||
public GroupGridPainter(Offset dimensions, CellPaintProvider styleInfo) {
|
public GroupGridPainter(Offset dimensions, CellPaintProvider styleInfo) {
|
||||||
this.dimensions = dimensions;
|
this.dimensions = dimensions;
|
||||||
this.styleInfo = styleInfo;
|
this.styleInfo = styleInfo;
|
||||||
radius = 30;
|
radius = 30;//TODO dynamic radius to fill area
|
||||||
margin = radius + radius / 4;
|
margin = radius + radius / 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,6 +33,6 @@ public class GroupGridPainter implements GridPainter {
|
||||||
|
|
||||||
private void drawCell(Canvas canvas, Offset offset, GroupCell cell, Pair<Integer, Integer> realDimensions) {
|
private void drawCell(Canvas canvas, Offset offset, GroupCell cell, Pair<Integer, Integer> realDimensions) {
|
||||||
Pair<Float, Float> origin = new Pair<>((offset.x / (float) dimensions.x) * realDimensions.first, (offset.y / (float) dimensions.y) * realDimensions.second);
|
Pair<Float, Float> origin = new Pair<>((offset.x / (float) dimensions.x) * realDimensions.first, (offset.y / (float) dimensions.y) * realDimensions.second);
|
||||||
canvas.drawCircle(origin.second + margin, origin.first + margin, 30, styleInfo.getPaint(cell));
|
canvas.drawCircle(origin.second + margin, origin.first + margin, radius, styleInfo.getPaint(cell));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package org.agp8x.android.games.fillgrid;
|
||||||
|
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.util.Pair;
|
||||||
|
|
||||||
|
import org.agp8x.android.games.fillgrid.data.Cell;
|
||||||
|
import org.agp8x.android.games.fillgrid.data.GridObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by clemensk on 09.04.17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface SpawnPainter<C extends Cell> {
|
||||||
|
void draw(Canvas canvas, int index, GridObject<C> gridObject, Pair<Integer,Integer> dimensions);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package org.agp8x.android.games.fillgrid;
|
||||||
|
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Paint;
|
||||||
|
import android.util.Pair;
|
||||||
|
|
||||||
|
import org.agp8x.android.games.fillgrid.data.GridObject;
|
||||||
|
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||||
|
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by clemensk on 09.04.17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class SpawnPainterGroup<C extends GroupCell> implements SpawnPainter<C> {
|
||||||
|
private int objCount;
|
||||||
|
private int radius;
|
||||||
|
private int margin;
|
||||||
|
private CellPaintProvider styleInfo;
|
||||||
|
|
||||||
|
public SpawnPainterGroup(int objCount) {
|
||||||
|
this.objCount = objCount;
|
||||||
|
radius = 30;//TODO dynamic radius to fill area
|
||||||
|
margin = radius + radius / 4;
|
||||||
|
this.styleInfo = new CellPaintProviderBackground();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw(Canvas canvas, int index, GridObject<C> gridObject, Pair<Integer, Integer> dimensions) {
|
||||||
|
System.out.println(dimensions);
|
||||||
|
int widthS = Math.max(dimensions.first, dimensions.second);
|
||||||
|
int height = (int) (Math.min(dimensions.first, dimensions.second) * 0.25f) + widthS;
|
||||||
|
float widthPerObject = widthS / (1f * objCount);
|
||||||
|
int width = (int) (index * widthPerObject);
|
||||||
|
|
||||||
|
gridObject.getObjects().forEach((offset, groupCell) -> drawCell(canvas, height, width, offset, groupCell));
|
||||||
|
//drawCell(canvas, new Pair<>(height, width), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawCell(Canvas canvas, int height, int width, Offset offset, C cell) {
|
||||||
|
canvas.drawCircle(width + margin+ (offset.x*radius*2), height + margin + (offset.y*radius*2), radius, styleInfo.getPaint(cell));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,57 +0,0 @@
|
||||||
package org.agp8x.android.games.fillgrid.data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by clemensk on 29.03.17.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class Coordinate {
|
|
||||||
private double x;
|
|
||||||
private double y;
|
|
||||||
|
|
||||||
public Coordinate() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Coordinate(double x, double y) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getX() {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setX(double x) {
|
|
||||||
this.x = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getY() {
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setY(double y) {
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
|
|
||||||
Coordinate that = (Coordinate) o;
|
|
||||||
|
|
||||||
if (Double.compare(that.x, x) != 0) return false;
|
|
||||||
return Double.compare(that.y, y) == 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
int result;
|
|
||||||
long temp;
|
|
||||||
temp = Double.doubleToLongBits(x);
|
|
||||||
result = (int) (temp ^ (temp >>> 32));
|
|
||||||
temp = Double.doubleToLongBits(y);
|
|
||||||
result = 31 * result + (int) (temp ^ (temp >>> 32));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue