placement and collapsion of filled lines working, strange bugs remain
parent
0def898084
commit
5932062e0c
|
|
@ -4,6 +4,7 @@ import android.support.v7.app.AppCompatActivity;
|
|||
import android.os.Bundle;
|
||||
import android.util.Pair;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
||||
import org.agp8x.android.games.fillgrid.data.GridObject;
|
||||
|
|
@ -24,6 +25,7 @@ public class FillGrid extends AppCompatActivity {
|
|||
ip = new Pair<>(fp.first.intValue(), fp.second.intValue());*/
|
||||
FillGridView gridview = new FillGridView(this);
|
||||
layout.addView(gridview);
|
||||
gridview.setScoreView((TextView) findViewById(R.id.textView));
|
||||
}
|
||||
|
||||
/* private enum Asdf {
|
||||
|
|
|
|||
|
|
@ -3,10 +3,13 @@ package org.agp8x.android.games.fillgrid;
|
|||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.widget.TextViewCompat;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
||||
import org.agp8x.android.games.fillgrid.data.GridBoard;
|
||||
|
|
@ -18,6 +21,8 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.agp8x.android.games.fillgrid.Util.TAG;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 07.04.17.
|
||||
*/
|
||||
|
|
@ -35,6 +40,11 @@ public class FillGridView extends View {
|
|||
private float divison;
|
||||
private int spawnCount;
|
||||
private SpawnPainterGroup<GridBlock<GroupCell>, GroupCell> spawnPainter;
|
||||
private Dragging dragging;
|
||||
private Offset dimensions;
|
||||
private Map<GridBlock<GroupCell>, Pair<Integer, Integer>> spawnPositions;
|
||||
private TextView scoreView;
|
||||
private int score = 0;
|
||||
|
||||
public FillGridView(Context context) {
|
||||
super(context);
|
||||
|
|
@ -57,7 +67,7 @@ public class FillGridView extends View {
|
|||
}
|
||||
|
||||
protected void init(AttributeSet attrs, int defStyleAttr) {
|
||||
Offset dimensions = new Offset(10, 10);
|
||||
dimensions = new Offset(10, 10);
|
||||
board = new GridBoard<>(dimensions);
|
||||
|
||||
blocks = new ArrayList<>();
|
||||
|
|
@ -71,13 +81,13 @@ public class FillGridView extends View {
|
|||
}
|
||||
spawnGrid = new HashMap<>();
|
||||
|
||||
CellPaintProvider bgCpp = new CellPaintProviderBackground();
|
||||
CellPaintProvider<GroupCell> bgCpp = new CellPaintProviderBackground<>();
|
||||
bgPainter = new GroupGridPainter<>(dimensions, bgCpp);
|
||||
|
||||
CellPaintProvider cpp = new CellPaintProviderDefault();
|
||||
CellPaintProvider<GroupCell> cpp = new CellPaintProviderDefault<>();
|
||||
painter = new GroupGridPainter<>(dimensions, cpp);
|
||||
|
||||
spawnPainter = new SpawnPainterGroup<>(new CellPaintProviderBackground());
|
||||
spawnPainter = new SpawnPainterGroup<>(new CellPaintProviderDefault<>());
|
||||
|
||||
// TODO: create configuration
|
||||
spawnCount = 3;
|
||||
|
|
@ -103,6 +113,7 @@ public class FillGridView extends View {
|
|||
|
||||
populateSpawnArea();
|
||||
setOnTouchListener(new FillGridTouchListener());
|
||||
dragging = new Dragging();
|
||||
}
|
||||
|
||||
private GridBlock<GroupCell> makeSquare() {
|
||||
|
|
@ -137,7 +148,11 @@ public class FillGridView extends View {
|
|||
|
||||
bgPainter.draw(canvas, background, contentSize);
|
||||
painter.draw(canvas, board.getGrid(), contentSize);
|
||||
spawnPainter.draw(canvas, spawnGrid, spawnBoundaries);
|
||||
spawnPositions = spawnPainter.draw(canvas, spawnGrid, spawnBoundaries);
|
||||
if (dragging.object != null) {
|
||||
Log.d(TAG, "draw dragging");
|
||||
spawnPainter.drawDragging(canvas, dragging.xy, dragging.object);
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateAreaSizes() {
|
||||
|
|
@ -160,7 +175,7 @@ public class FillGridView extends View {
|
|||
}
|
||||
contentSize = new Pair<>(contentHeight, contentWidth);
|
||||
spawnSize = new Pair<>(spawnHeight, spawnWidth);
|
||||
spawnBoundaries = new Area(contentHeight, paddingLeft, spawnHeight, spawnWidth);
|
||||
spawnBoundaries = new Area(contentHeight, paddingLeft, spawnHeight + contentHeight, spawnWidth);
|
||||
}
|
||||
|
||||
private int divideSide(int sideLength) {
|
||||
|
|
@ -174,24 +189,132 @@ public class FillGridView extends View {
|
|||
}
|
||||
|
||||
private GridBlock<GroupCell> getRandomGroupCellGridBlock() {
|
||||
return blocks.get((int) (blocks.size() * Math.random()));
|
||||
return (GridBlock<GroupCell>) blocks.get((int) (blocks.size() * Math.random())).duplicate();
|
||||
}
|
||||
|
||||
private Offset coordinate2offset(Pair<Float, Float> xy) {
|
||||
return new Offset((int) ((xy.second / contentSize.first) * dimensions.y), (int) ((xy.first / contentSize.second) * dimensions.y));
|
||||
}
|
||||
|
||||
private Offset coordinate2spawnOffset(Pair<Float, Float> xy) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setScoreView(TextView scoreView) {
|
||||
this.scoreView = scoreView;
|
||||
}
|
||||
|
||||
private class FillGridTouchListener implements OnTouchListener {
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
System.out.println("EVENT!");
|
||||
;
|
||||
Log.d(TAG, "EVENT");
|
||||
Pair<Float, Float> position = Util.event2pair(event);
|
||||
boolean updated = false;
|
||||
if (Util.in(position, spawnBoundaries)) {
|
||||
updated = handleStart(event);
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
Log.d(TAG, "EVENT: AcitonDown");
|
||||
if (Util.in(position, spawnBoundaries)) {
|
||||
Log.d(TAG, " in boundary");
|
||||
Log.d(TAG, "");
|
||||
updated = handleStart(event, position);
|
||||
}
|
||||
break;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
updated = handleMove(event, position);
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
//TODO: drop
|
||||
updated = handleStop(event, position);
|
||||
break;
|
||||
}
|
||||
if (updated) {
|
||||
invalidate();
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
private boolean handleStart(MotionEvent event) {
|
||||
private boolean handleMove(MotionEvent event, Pair<Float, Float> position) {
|
||||
boolean updated = false;
|
||||
|
||||
if (dragging.object != null) {
|
||||
dragging.xy = position;
|
||||
updated = true;
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
private boolean handleStart(MotionEvent event, Pair<Float, Float> position) {
|
||||
if (dragging.object != null) {
|
||||
return false;
|
||||
}
|
||||
boolean updated = false;
|
||||
Log.d(TAG, "no object dragged aleady");
|
||||
// spawnPositions.entrySet().stream().filter(entry -> selects(position, entry.getValue())).findFirst().ifPresent(entry -> startDragging(entry, position));
|
||||
for (Map.Entry<GridBlock<GroupCell>, Pair<Integer, Integer>> entry : spawnPositions.entrySet()) {
|
||||
Log.d(TAG, "handleStart: "+position+"");
|
||||
if (selects(position, entry.getValue())) {
|
||||
Log.d(TAG, "handleStart: SELECTED");
|
||||
Log.d(TAG, "now we drag");
|
||||
dragging.object = entry.getKey();
|
||||
dragging.xy = position;
|
||||
updated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println(spawnPositions);
|
||||
return updated;
|
||||
}
|
||||
|
||||
private boolean handleStop(MotionEvent event, Pair<Float, Float> position) {
|
||||
boolean updated = false;
|
||||
if (dragging.object != null) {
|
||||
if (board.drop(coordinate2offset(position), dragging.object)) {
|
||||
System.out.println(spawnGrid);
|
||||
System.out.println(dragging.object);
|
||||
//Offset key = spawnGrid.entrySet().stream().filter(entry -> entry==dragging.object).findFirst().get().getKey();
|
||||
Offset key=null;
|
||||
for (Map.Entry<Offset, GridBlock<GroupCell>> entry : spawnGrid.entrySet()) {
|
||||
if (entry.getValue() == dragging.object){
|
||||
key= entry.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
spawnGrid.remove(key);
|
||||
spawnPositions.remove(dragging.object);
|
||||
}
|
||||
dragging.object = null;
|
||||
if (spawnGrid.isEmpty()){
|
||||
populateSpawnArea();
|
||||
}
|
||||
score += board.collapseFilledLines();
|
||||
if (scoreView != null) {
|
||||
scoreView.setText(String.valueOf(score));
|
||||
}
|
||||
board.removeCollapsed();
|
||||
updated = true;
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
/* private void startDragging(Map.Entry<GridBlock<GroupCell>, Pair<Integer, Integer>> entry, Pair<Float, Float> position) {
|
||||
Log.d(TAG, "now we drag");
|
||||
dragging.object = entry.getKey();
|
||||
dragging.xy = position;
|
||||
}*/
|
||||
|
||||
private boolean selects(Pair<Float, Float> eventPos, Pair<Integer, Integer> spawnPos) {
|
||||
float xdiff = Math.abs(eventPos.first - spawnPos.first);
|
||||
float ydiff = Math.abs(eventPos.second - spawnPos.second);
|
||||
System.out.println("diffs: " + xdiff + " --- " + ydiff);
|
||||
return xdiff < 40 && ydiff < 40;
|
||||
}
|
||||
}
|
||||
|
||||
private class Dragging {
|
||||
GridBlock<GroupCell> object;
|
||||
Pair<Float, Float> xy;
|
||||
Offset key;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,11 +14,11 @@ import java.util.Map;
|
|||
|
||||
public class GroupGridPainter<C extends GroupCell> implements GridPainter<C> {
|
||||
private Offset dimensions;
|
||||
private CellPaintProvider styleInfo;
|
||||
private CellPaintProvider<C> styleInfo;
|
||||
private int margin;
|
||||
private int radius;
|
||||
|
||||
public GroupGridPainter(Offset dimensions, CellPaintProvider styleInfo) {
|
||||
public GroupGridPainter(Offset dimensions, CellPaintProvider<C> styleInfo) {
|
||||
this.dimensions = dimensions;
|
||||
this.styleInfo = styleInfo;
|
||||
radius = 30;//TODO dynamic radius to fill area
|
||||
|
|
@ -30,7 +30,7 @@ public class GroupGridPainter<C extends GroupCell> implements GridPainter<C> {
|
|||
cells.forEach((offset, groupCell) -> drawCell(canvas, offset, groupCell, realDimensions));
|
||||
}
|
||||
|
||||
private void drawCell(Canvas canvas, Offset offset, GroupCell cell, Pair<Integer, Integer> realDimensions) {
|
||||
private void drawCell(Canvas canvas, Offset offset, C 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);
|
||||
canvas.drawCircle(origin.second + margin, origin.first + margin, radius, styleInfo.getPaint(cell));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,5 +14,7 @@ import java.util.Map;
|
|||
*/
|
||||
|
||||
public interface SpawnPainter<G extends GridObject<C>, C extends Cell> {
|
||||
void draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize);
|
||||
Map<G, Pair<Integer, Integer>> draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize);
|
||||
|
||||
void drawDragging(Canvas canvas, Pair<Float, Float> xy, G object);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,9 @@ 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.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 09.04.17.
|
||||
|
|
@ -16,38 +18,64 @@ import java.util.Map;
|
|||
public class SpawnPainterGroup<G extends GridObject<C>, C extends GroupCell> implements SpawnPainter<G, C> {
|
||||
private int radius;
|
||||
private int margin;
|
||||
private CellPaintProvider styleInfo;
|
||||
private CellPaintProvider<C> styleInfo;
|
||||
private int diameter;
|
||||
private Map<G, Pair<Integer, Integer>> positions;
|
||||
|
||||
public SpawnPainterGroup(CellPaintProvider styleInfo) {
|
||||
public SpawnPainterGroup(CellPaintProvider<C> styleInfo) {
|
||||
this.styleInfo = styleInfo;
|
||||
radius = 30;//TODO dynamic radius to fill area
|
||||
margin = radius *3 + radius / 4;
|
||||
margin = radius * 3 + radius / 4;
|
||||
diameter = radius * 2;
|
||||
positions = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize) {
|
||||
public Map<G, Pair<Integer, Integer>> draw(Canvas canvas, Map<Offset, G> spawnGrid, Area spawnSize) {
|
||||
int count = spawnGrid.size();
|
||||
spawnGrid.forEach((offset, groupCellGridBlock) -> drawBlock(canvas, offset, groupCellGridBlock, count, spawnSize));
|
||||
if (positions.isEmpty()) {
|
||||
for (Offset offset : spawnGrid.keySet()) {
|
||||
G obj = spawnGrid.get(offset);
|
||||
positions.put(obj, getPosition(offset, obj, count, spawnSize));
|
||||
}
|
||||
}
|
||||
//spawnGrid.forEach((offset, groupCellGridBlock) -> drawBlock(canvas, groupCellGridBlock));
|
||||
for (Offset offset : spawnGrid.keySet()) {
|
||||
drawBlock(canvas,spawnGrid.get(offset));
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
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));
|
||||
@Override
|
||||
public void drawDragging(Canvas canvas, Pair<Float, Float> xy, G object) {
|
||||
object.getObjects().forEach((offset, c) -> drawCellDragging(canvas, xy, offset, c));
|
||||
}
|
||||
|
||||
private void drawCell(Canvas canvas, Offset offset, Offset offset1, C cell, int count, Area spawnSize) {
|
||||
private Pair<Integer, Integer> getPosition(Offset offset, G obj, int count, Area spawnSize) {
|
||||
return new Pair<>(((offset.x / count) * spawnSize.right) + margin * (offset.x+1), ((offset.y / count) * spawnSize.bottom) + margin + spawnSize.top);
|
||||
}
|
||||
|
||||
private void drawCellDragging(Canvas canvas, Pair<Float, Float> xy, Offset offset, C c) {
|
||||
canvas.drawCircle(xy.first + diameter * offset.y, xy.second + diameter * offset.x, radius, styleInfo.getPaint(c));
|
||||
}
|
||||
|
||||
private void drawBlock(Canvas canvas, G groupCellGridBlock) {
|
||||
Pair<Integer, Integer> origin = positions.get(groupCellGridBlock);
|
||||
groupCellGridBlock.getObjects().forEach((offset1, groupCell) -> drawCell(canvas, origin, offset1, groupCell));
|
||||
}
|
||||
|
||||
private void drawCell(Canvas canvas, Pair<Integer, Integer> origin, Offset offset1, C cell) {
|
||||
canvas.drawCircle(
|
||||
getCx(offset, offset1, count, spawnSize),
|
||||
getCy(offset, offset1, count, spawnSize),
|
||||
origin.first + getCx(offset1),
|
||||
origin.second + getCy(offset1),
|
||||
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 getCx(Offset offset1) {
|
||||
return offset1.y * diameter;
|
||||
}
|
||||
|
||||
private float getCy(Offset offset, Offset offset1, float count, Area spawnSize) {
|
||||
return ((offset.y / count) * spawnSize.bottom) + margin + offset1.y * diameter + spawnSize.top;
|
||||
private float getCy(Offset offset1) {
|
||||
return offset1.x * diameter;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.agp8x.android.games.fillgrid;
|
||||
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
|
|
@ -15,6 +16,8 @@ public class Util {
|
|||
}
|
||||
|
||||
public static boolean in(Pair<Float, Float> probe, Area area) {
|
||||
Log.d(TAG,"probe: "+probe+" ;area: "+area);
|
||||
Log.d(TAG, "("+(area.top < probe.second)+" && "+(probe.second < area.bottom)+") && ("+(area.left < probe.first) +" && "+ (probe.first < area.right)+")");
|
||||
boolean contained = (area.top < probe.second && probe.second < area.bottom) && (area.left < probe.first && probe.first < area.right);
|
||||
return contained;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Represents a GridBoard in its entirety, i.e. Top level object
|
||||
|
|
@ -50,7 +51,9 @@ public class GridBoard<G extends GridObject<C>, C extends Cell> implements Grid<
|
|||
}
|
||||
|
||||
public void removeCollapsed() {
|
||||
contents.entrySet().stream().filter(offsetCEntry -> offsetCEntry.getValue().isCollapsed()).forEach(offsetCEntry -> contents.remove(offsetCEntry.getKey()));
|
||||
List<Offset> removals = contents.entrySet().stream().filter(offsetCEntry -> offsetCEntry.getValue().isCollapsed()).map(Map.Entry::getKey).collect(Collectors.toList());
|
||||
removals.forEach(contents::remove);
|
||||
removals.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -61,7 +64,9 @@ public class GridBoard<G extends GridObject<C>, C extends Cell> implements Grid<
|
|||
public int collapseFilledLines() {
|
||||
List<Offset> deletionQueue = findFullLines();
|
||||
deletionQueue.stream().map(contents::get).forEach(Cell::markCollapsed);
|
||||
return deletionQueue.size();
|
||||
int count = deletionQueue.size();
|
||||
deletionQueue.clear();
|
||||
return count;
|
||||
}
|
||||
|
||||
public List<Offset> findFullLines() {
|
||||
|
|
@ -134,7 +139,7 @@ public class GridBoard<G extends GridObject<C>, C extends Cell> implements Grid<
|
|||
*/
|
||||
private boolean isSettable(Offset origin, Offset offset) {
|
||||
Offset xy = Offset.add(origin, offset);
|
||||
if ((xy.x < 0 || xy.x > dimensions.x) || (xy.y < 0 || xy.y > dimensions.y)) {
|
||||
if ((xy.x < 0 || xy.x >= dimensions.x) || (xy.y < 0 || xy.y >= dimensions.y)) {
|
||||
return false;
|
||||
}
|
||||
C obj1 = contents.get(xy);
|
||||
|
|
|
|||
|
|
@ -28,12 +28,12 @@ public class GroupCell implements Cell {
|
|||
|
||||
@Override
|
||||
public void markCollapsed() {
|
||||
collapsionMark =true;
|
||||
collapsionMark = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCollapsed() {
|
||||
return collapsed;
|
||||
return collapsionMark;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,4 +9,13 @@
|
|||
android:id="@+id/mainLayout">
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="15dp"
|
||||
android:text="TextView"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_marginEnd="26dp"/>
|
||||
</RelativeLayout>
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ buildscript {
|
|||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.3.0'
|
||||
classpath 'com.android.tools.build:gradle:2.3.1'
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
|
|
|
|||
Loading…
Reference in New Issue