improve spawn by saving position
parent
08b192a6a5
commit
0def898084
|
|
@ -9,7 +9,7 @@
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme">
|
||||||
<activity android:name=".FillGrid">
|
<activity android:name=".FillGrid" android:screenOrientation="portrait">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN"/>
|
<action android:name="android.intent.action.MAIN"/>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
package org.agp8x.android.games.fillgrid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by clemensk on 09.04.17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public final class Area {
|
||||||
|
public final int top;
|
||||||
|
public final int left;
|
||||||
|
public final int bottom;
|
||||||
|
public final int right;
|
||||||
|
|
||||||
|
public Area(int top, int left, int bottom, int right) {
|
||||||
|
this.top = top;
|
||||||
|
this.left = left;
|
||||||
|
this.bottom = bottom;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Area{" +
|
||||||
|
"top=" + top +
|
||||||
|
", left=" + left +
|
||||||
|
", bottom=" + bottom +
|
||||||
|
", right=" + right +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Area area = (Area) o;
|
||||||
|
|
||||||
|
if (top != area.top) return false;
|
||||||
|
if (left != area.left) return false;
|
||||||
|
if (bottom != area.bottom) return false;
|
||||||
|
return right == area.right;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = top;
|
||||||
|
result = 31 * result + left;
|
||||||
|
result = 31 * result + bottom;
|
||||||
|
result = 31 * result + right;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,7 +9,7 @@ import org.agp8x.android.games.fillgrid.data.Cell;
|
||||||
* Created by clemensk on 09.04.17.
|
* Created by clemensk on 09.04.17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface CellPaintProvider {
|
public interface CellPaintProvider<C extends Cell> {
|
||||||
@NonNull
|
@NonNull
|
||||||
Paint getPaint(Cell cell);
|
Paint getPaint(C cell);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,10 @@ import org.agp8x.android.games.fillgrid.data.Cell;
|
||||||
* Created by clemensk on 09.04.17.
|
* Created by clemensk on 09.04.17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class CellPaintProviderBackground implements CellPaintProvider {
|
public class CellPaintProviderBackground<C extends Cell> implements CellPaintProvider<C> {
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public Paint getPaint(Cell cell) {
|
public Paint getPaint(C cell) {
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
paint.setAlpha(128);
|
paint.setAlpha(128);
|
||||||
return paint;
|
return paint;
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,11 @@ import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||||
* Created by clemensk on 09.04.17.
|
* Created by clemensk on 09.04.17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class CellPaintProviderDefault implements CellPaintProvider {
|
public class CellPaintProviderDefault<C extends Cell> implements CellPaintProvider<C> {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public Paint getPaint(Cell cell) {
|
public Paint getPaint(C cell) {
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
if (cell instanceof GroupCell) {
|
if (cell instanceof GroupCell) {
|
||||||
GroupCell groupCell = (GroupCell) cell;
|
GroupCell groupCell = (GroupCell) cell;
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,11 @@ import android.graphics.Canvas;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
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;
|
||||||
|
|
||||||
|
|
@ -25,15 +25,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 Pair<Integer, Integer> spawnSize = null;
|
||||||
|
protected Area spawnBoundaries;
|
||||||
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 Map<Offset, GridBlock<GroupCell>> spawnGrid;
|
||||||
protected GroupGridPainter bgPainter;
|
protected GroupGridPainter<GroupCell> painter;
|
||||||
|
protected GroupGridPainter<GroupCell> bgPainter;
|
||||||
private float divison;
|
private float divison;
|
||||||
private ArrayList<GridObject<GroupCell>> spawn;
|
|
||||||
private int spawnCount;
|
private int spawnCount;
|
||||||
private SpawnPainterGroup<GroupCell> spawnPainter;
|
private SpawnPainterGroup<GridBlock<GroupCell>, GroupCell> spawnPainter;
|
||||||
|
|
||||||
public FillGridView(Context context) {
|
public FillGridView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
|
@ -68,18 +69,18 @@ public class FillGridView extends View {
|
||||||
background.put(new Offset(i, j), bgCell);
|
background.put(new Offset(i, j), bgCell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
spawnGrid = new HashMap<>();
|
||||||
|
|
||||||
CellPaintProvider bgCpp = new CellPaintProviderBackground();
|
CellPaintProvider bgCpp = new CellPaintProviderBackground();
|
||||||
bgPainter = new GroupGridPainter(dimensions, bgCpp);
|
bgPainter = new GroupGridPainter<>(dimensions, bgCpp);
|
||||||
|
|
||||||
CellPaintProvider cpp = new CellPaintProviderDefault();
|
CellPaintProvider cpp = new CellPaintProviderDefault();
|
||||||
painter = new GroupGridPainter(dimensions, cpp);
|
painter = new GroupGridPainter<>(dimensions, cpp);
|
||||||
|
|
||||||
|
spawnPainter = new SpawnPainterGroup<>(new CellPaintProviderBackground());
|
||||||
|
|
||||||
// TODO: create configuration
|
// TODO: create configuration
|
||||||
spawnCount = 3;
|
spawnCount = 3;
|
||||||
spawnPainter = new SpawnPainterGroup<>(spawnCount);
|
|
||||||
spawn = new ArrayList<>(spawnCount);
|
|
||||||
|
|
||||||
divison = 0.5625f; // divides *HD-resolutions in a square and a non-square
|
divison = 0.5625f; // divides *HD-resolutions in a square and a non-square
|
||||||
|
|
||||||
|
|
@ -98,8 +99,10 @@ public class FillGridView extends View {
|
||||||
|
|
||||||
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());
|
||||||
|
//end TODO
|
||||||
|
|
||||||
populateSpawnArea();
|
populateSpawnArea();
|
||||||
|
setOnTouchListener(new FillGridTouchListener());
|
||||||
}
|
}
|
||||||
|
|
||||||
private GridBlock<GroupCell> makeSquare() {
|
private GridBlock<GroupCell> makeSquare() {
|
||||||
|
|
@ -134,9 +137,7 @@ public class FillGridView extends View {
|
||||||
|
|
||||||
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, spawnGrid, spawnBoundaries);
|
||||||
spawnPainter.draw(canvas,i,spawn.get(i),spawnSize);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void calculateAreaSizes() {
|
private void calculateAreaSizes() {
|
||||||
|
|
@ -159,6 +160,7 @@ public class FillGridView extends View {
|
||||||
}
|
}
|
||||||
contentSize = new Pair<>(contentHeight, contentWidth);
|
contentSize = new Pair<>(contentHeight, contentWidth);
|
||||||
spawnSize = new Pair<>(spawnHeight, spawnWidth);
|
spawnSize = new Pair<>(spawnHeight, spawnWidth);
|
||||||
|
spawnBoundaries = new Area(contentHeight, paddingLeft, spawnHeight, spawnWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int divideSide(int sideLength) {
|
private int divideSide(int sideLength) {
|
||||||
|
|
@ -167,8 +169,29 @@ public class FillGridView extends View {
|
||||||
|
|
||||||
private void populateSpawnArea() {
|
private void populateSpawnArea() {
|
||||||
for (int i = 0; i < spawnCount; i++) {
|
for (int i = 0; i < spawnCount; i++) {
|
||||||
GridObject<GroupCell> obj = blocks.get((int) (blocks.size() * Math.random()));
|
spawnGrid.put(new Offset(i, 0), getRandomGroupCellGridBlock());
|
||||||
spawn.add(i, obj);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private GridBlock<GroupCell> getRandomGroupCellGridBlock() {
|
||||||
|
return blocks.get((int) (blocks.size() * Math.random()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private class FillGridTouchListener implements OnTouchListener {
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
Pair<Float, Float> position = Util.event2pair(event);
|
||||||
|
boolean updated = false;
|
||||||
|
if (Util.in(position, spawnBoundaries)) {
|
||||||
|
updated = handleStart(event);
|
||||||
|
}
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean handleStart(MotionEvent event) {
|
||||||
|
boolean updated = false;
|
||||||
|
|
||||||
|
return updated;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +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.GroupCell;
|
import org.agp8x.android.games.fillgrid.data.Cell;
|
||||||
import org.agp8x.android.games.fillgrid.data.Offset;
|
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -12,7 +12,7 @@ import java.util.Map;
|
||||||
* Created by clemensk on 07.04.17.
|
* Created by clemensk on 07.04.17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface GridPainter {
|
public interface GridPainter<C extends Cell> {
|
||||||
|
|
||||||
void draw(Canvas canvas, Map<Offset, GroupCell> cells, Pair<Integer,Integer> realDimensions);
|
void draw(Canvas canvas, Map<Offset, C> cells, Pair<Integer,Integer> realDimensions);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ 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;
|
||||||
|
|
||||||
|
|
@ -13,7 +12,7 @@ import java.util.Map;
|
||||||
* Created by clemensk on 07.04.17.
|
* Created by clemensk on 07.04.17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class GroupGridPainter implements GridPainter {
|
public class GroupGridPainter<C extends GroupCell> implements GridPainter<C> {
|
||||||
private Offset dimensions;
|
private Offset dimensions;
|
||||||
private CellPaintProvider styleInfo;
|
private CellPaintProvider styleInfo;
|
||||||
private int margin;
|
private int margin;
|
||||||
|
|
@ -27,7 +26,7 @@ public class GroupGridPainter implements GridPainter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Canvas canvas, Map<Offset, GroupCell> cells, Pair<Integer, Integer> realDimensions) {
|
public void draw(Canvas canvas, Map<Offset, C> cells, Pair<Integer, Integer> realDimensions) {
|
||||||
cells.forEach((offset, groupCell) -> drawCell(canvas, offset, groupCell, realDimensions));
|
cells.forEach((offset, groupCell) -> drawCell(canvas, offset, groupCell, realDimensions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,14 @@ import android.util.Pair;
|
||||||
|
|
||||||
import org.agp8x.android.games.fillgrid.data.Cell;
|
import org.agp8x.android.games.fillgrid.data.Cell;
|
||||||
import org.agp8x.android.games.fillgrid.data.GridObject;
|
import org.agp8x.android.games.fillgrid.data.GridObject;
|
||||||
|
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by clemensk on 09.04.17.
|
* Created by clemensk on 09.04.17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface SpawnPainter<C extends Cell> {
|
public interface SpawnPainter<G extends GridObject<C>, C extends Cell> {
|
||||||
void draw(Canvas canvas, int index, GridObject<C> gridObject, Pair<Integer,Integer> dimensions);
|
void draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,43 +1,53 @@
|
||||||
package org.agp8x.android.games.fillgrid;
|
package org.agp8x.android.games.fillgrid;
|
||||||
|
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Paint;
|
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
import org.agp8x.android.games.fillgrid.data.GridObject;
|
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;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by clemensk on 09.04.17.
|
* Created by clemensk on 09.04.17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class SpawnPainterGroup<C extends GroupCell> implements SpawnPainter<C> {
|
public class SpawnPainterGroup<G extends GridObject<C>, C extends GroupCell> implements SpawnPainter<G, C> {
|
||||||
private int objCount;
|
|
||||||
private int radius;
|
private int radius;
|
||||||
private int margin;
|
private int margin;
|
||||||
private CellPaintProvider styleInfo;
|
private CellPaintProvider styleInfo;
|
||||||
|
private int diameter;
|
||||||
|
|
||||||
public SpawnPainterGroup(int objCount) {
|
public SpawnPainterGroup(CellPaintProvider styleInfo) {
|
||||||
this.objCount = objCount;
|
this.styleInfo = styleInfo;
|
||||||
radius = 30;//TODO dynamic radius to fill area
|
radius = 30;//TODO dynamic radius to fill area
|
||||||
margin = radius + radius / 4;
|
margin = radius *3 + radius / 4;
|
||||||
this.styleInfo = new CellPaintProviderBackground();
|
diameter = radius * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Canvas canvas, int index, GridObject<C> gridObject, Pair<Integer, Integer> dimensions) {
|
public void draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize) {
|
||||||
System.out.println(dimensions);
|
int count = spawnGrid.size();
|
||||||
int widthS = Math.max(dimensions.first, dimensions.second);
|
spawnGrid.forEach((offset, groupCellGridBlock) -> drawBlock(canvas, offset, groupCellGridBlock, count, spawnSize));
|
||||||
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) {
|
private void drawBlock(Canvas canvas, Offset offset, G groupCellGridBlock, int count, Area spawnSize) {
|
||||||
canvas.drawCircle(width + margin+ (offset.x*radius*2), height + margin + (offset.y*radius*2), radius, styleInfo.getPaint(cell));
|
groupCellGridBlock.getObjects().forEach((offset1, groupCell) -> drawCell(canvas, offset, offset1, groupCell, count, spawnSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawCell(Canvas canvas, Offset offset, Offset offset1, C cell, int count, Area spawnSize) {
|
||||||
|
canvas.drawCircle(
|
||||||
|
getCx(offset, offset1, count, spawnSize),
|
||||||
|
getCy(offset, offset1, count, spawnSize),
|
||||||
|
radius, styleInfo.getPaint(cell));
|
||||||
|
}
|
||||||
|
|
||||||
|
private float getCx(Offset offset, Offset offset1, float count, Area spawnSize) {
|
||||||
|
return ((offset.x / count) * spawnSize.right) + margin + offset1.x * diameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float getCy(Offset offset, Offset offset1, float count, Area spawnSize) {
|
||||||
|
return ((offset.y / count) * spawnSize.bottom) + margin + offset1.y * diameter + spawnSize.top;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package org.agp8x.android.games.fillgrid;
|
||||||
|
|
||||||
|
import android.util.Pair;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by clemensk on 09.04.17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Util {
|
||||||
|
public static final String TAG = "FillGrid";
|
||||||
|
|
||||||
|
public static Pair<Float, Float> event2pair(MotionEvent event) {
|
||||||
|
return new Pair<>(event.getX(), event.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean in(Pair<Float, Float> probe, Area area) {
|
||||||
|
boolean contained = (area.top < probe.second && probe.second < area.bottom) && (area.left < probe.first && probe.first < area.right);
|
||||||
|
return contained;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue