improve spawn by saving position

noStream
agp8x 2017-04-09 20:41:20 +02:00
parent 08b192a6a5
commit 0def898084
11 changed files with 155 additions and 47 deletions

View File

@ -9,7 +9,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FillGrid">
<activity android:name=".FillGrid" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

View File

@ -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;
}
}

View File

@ -9,7 +9,7 @@ import org.agp8x.android.games.fillgrid.data.Cell;
* Created by clemensk on 09.04.17.
*/
public interface CellPaintProvider {
public interface CellPaintProvider<C extends Cell> {
@NonNull
Paint getPaint(Cell cell);
Paint getPaint(C cell);
}

View File

@ -9,10 +9,10 @@ import org.agp8x.android.games.fillgrid.data.Cell;
* Created by clemensk on 09.04.17.
*/
public class CellPaintProviderBackground implements CellPaintProvider {
public class CellPaintProviderBackground<C extends Cell> implements CellPaintProvider<C> {
@NonNull
@Override
public Paint getPaint(Cell cell) {
public Paint getPaint(C cell) {
Paint paint = new Paint();
paint.setAlpha(128);
return paint;

View File

@ -11,11 +11,11 @@ import org.agp8x.android.games.fillgrid.data.GroupCell;
* Created by clemensk on 09.04.17.
*/
public class CellPaintProviderDefault implements CellPaintProvider {
public class CellPaintProviderDefault<C extends Cell> implements CellPaintProvider<C> {
@NonNull
@Override
public Paint getPaint(Cell cell) {
public Paint getPaint(C cell) {
Paint paint = new Paint();
if (cell instanceof GroupCell) {
GroupCell groupCell = (GroupCell) cell;

View File

@ -5,11 +5,11 @@ import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import org.agp8x.android.games.fillgrid.data.GridBlock;
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.Offset;
@ -25,15 +25,16 @@ import java.util.Map;
public class FillGridView extends View {
protected Pair<Integer, Integer> contentSize = null;
protected Pair<Integer, Integer> spawnSize = null;
protected Area spawnBoundaries;
protected GridBoard<GridBlock<GroupCell>, GroupCell> board;
protected List<GridBlock<GroupCell>> blocks;
protected Map<Offset, GroupCell> background;
protected GroupGridPainter painter;
protected GroupGridPainter bgPainter;
protected Map<Offset, GridBlock<GroupCell>> spawnGrid;
protected GroupGridPainter<GroupCell> painter;
protected GroupGridPainter<GroupCell> bgPainter;
private float divison;
private ArrayList<GridObject<GroupCell>> spawn;
private int spawnCount;
private SpawnPainterGroup<GroupCell> spawnPainter;
private SpawnPainterGroup<GridBlock<GroupCell>, GroupCell> spawnPainter;
public FillGridView(Context context) {
super(context);
@ -68,18 +69,18 @@ public class FillGridView extends View {
background.put(new Offset(i, j), bgCell);
}
}
spawnGrid = new HashMap<>();
CellPaintProvider bgCpp = new CellPaintProviderBackground();
bgPainter = new GroupGridPainter(dimensions, bgCpp);
bgPainter = new GroupGridPainter<>(dimensions, bgCpp);
CellPaintProvider cpp = new CellPaintProviderDefault();
painter = new GroupGridPainter(dimensions, cpp);
painter = new GroupGridPainter<>(dimensions, cpp);
spawnPainter = new SpawnPainterGroup<>(new CellPaintProviderBackground());
// 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
@ -98,8 +99,10 @@ public class FillGridView extends View {
board.drop(new Offset(4, 4), makeSquare());
board.drop(new Offset(4, 4), makeAngle());
//end TODO
populateSpawnArea();
setOnTouchListener(new FillGridTouchListener());
}
private GridBlock<GroupCell> makeSquare() {
@ -134,9 +137,7 @@ public class FillGridView extends View {
bgPainter.draw(canvas, background, contentSize);
painter.draw(canvas, board.getGrid(), contentSize);
for (int i = 0; i < spawnCount; i++) {
spawnPainter.draw(canvas,i,spawn.get(i),spawnSize);
}
spawnPainter.draw(canvas, spawnGrid, spawnBoundaries);
}
private void calculateAreaSizes() {
@ -159,6 +160,7 @@ public class FillGridView extends View {
}
contentSize = new Pair<>(contentHeight, contentWidth);
spawnSize = new Pair<>(spawnHeight, spawnWidth);
spawnBoundaries = new Area(contentHeight, paddingLeft, spawnHeight, spawnWidth);
}
private int divideSide(int sideLength) {
@ -167,8 +169,29 @@ public class FillGridView extends View {
private void populateSpawnArea() {
for (int i = 0; i < spawnCount; i++) {
GridObject<GroupCell> obj = blocks.get((int) (blocks.size() * Math.random()));
spawn.add(i, obj);
spawnGrid.put(new Offset(i, 0), getRandomGroupCellGridBlock());
}
}
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;
}
}
}

View File

@ -3,7 +3,7 @@ package org.agp8x.android.games.fillgrid;
import android.graphics.Canvas;
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 java.util.Map;
@ -12,7 +12,7 @@ import java.util.Map;
* 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);
}

View File

@ -3,7 +3,6 @@ package org.agp8x.android.games.fillgrid;
import android.graphics.Canvas;
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;
@ -13,7 +12,7 @@ import java.util.Map;
* 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 CellPaintProvider styleInfo;
private int margin;
@ -27,7 +26,7 @@ public class GroupGridPainter implements GridPainter {
}
@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));
}

View File

@ -5,11 +5,14 @@ import android.util.Pair;
import org.agp8x.android.games.fillgrid.data.Cell;
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.
*/
public interface SpawnPainter<C extends Cell> {
void draw(Canvas canvas, int index, GridObject<C> gridObject, Pair<Integer,Integer> dimensions);
public interface SpawnPainter<G extends GridObject<C>, C extends Cell> {
void draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize);
}

View File

@ -1,43 +1,53 @@
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;
import java.util.Map;
/**
* Created by clemensk on 09.04.17.
*/
public class SpawnPainterGroup<C extends GroupCell> implements SpawnPainter<C> {
private int objCount;
public class SpawnPainterGroup<G extends GridObject<C>, C extends GroupCell> implements SpawnPainter<G, C> {
private int radius;
private int margin;
private CellPaintProvider styleInfo;
private int diameter;
public SpawnPainterGroup(int objCount) {
this.objCount = objCount;
public SpawnPainterGroup(CellPaintProvider styleInfo) {
this.styleInfo = styleInfo;
radius = 30;//TODO dynamic radius to fill area
margin = radius + radius / 4;
this.styleInfo = new CellPaintProviderBackground();
margin = radius *3 + radius / 4;
diameter = radius * 2;
}
@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);
public void draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize) {
int count = spawnGrid.size();
spawnGrid.forEach((offset, groupCellGridBlock) -> drawBlock(canvas, offset, groupCellGridBlock, count, spawnSize));
}
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));
private void drawBlock(Canvas canvas, Offset offset, G groupCellGridBlock, int count, Area spawnSize) {
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;
}
}

View File

@ -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;
}
}