Skip to content

Commit

Permalink
Merge branch 'release/0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Hoth committed Apr 1, 2014
2 parents e777429 + 2cf0f26 commit 7c3eef2
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
versionCode 9
versionName "0.6"
versionCode 10
versionName "0.7"
}

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MCPTest(String name) {
@Override
public void setUp() throws Exception {
super.setUp();
mcp = new MCP();
mcp = new MCP(null, 4);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private void handleMoveDone(Intent intent) {
MovementChanges changes = intent.getParcelableExtra(MCP.KEY_MOVEMENT_CHANGES);

mSquareGridView.updateGrid(changes.gridStatus);
mSquareGridView.addCells(changes.getAddedCells(), changes.addedCellsValue);
//TODO animate new points
mCurrentScore += changes.pointsEarned;
mTvCurrentScore.setText(String.valueOf(mCurrentScore));
Expand Down
31 changes: 25 additions & 6 deletions app/src/main/java/de/stefanhoth/android/got2048/logic/MCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.deploygate.sdk.DeployGate;

import de.stefanhoth.android.got2048.logic.model.Cell;
import de.stefanhoth.android.got2048.logic.model.Grid;
import de.stefanhoth.android.got2048.logic.model.MOVE_DIRECTION;
Expand Down Expand Up @@ -52,9 +54,12 @@ protected Grid getPlaylingField() {

public void addStartCells() {

MovementChanges changes = new MovementChanges(getPlaylingField().getGridStatus());

Cell cell = playlingField.getRandomCell();

playlingField.setCellValue(cell.getRow(), cell.getColumn(), DEFAULT_START_VALUE);
changes.addCell(cell, DEFAULT_START_VALUE);

Cell nextCell = playlingField.getRandomCell();

Expand All @@ -65,17 +70,17 @@ public void addStartCells() {

playlingField.setCellValue(nextCell.getRow(), nextCell.getColumn(), DEFAULT_START_VALUE);
cell = nextCell;
changes.addCell(cell, DEFAULT_START_VALUE);
}


updateMoveDoneListeners(new MovementChanges(getPlaylingField().getGridStatus()));
updateMoveDoneListeners(changes);
}

public void addNewCell() {
public MovementChanges addNewCell(MovementChanges changes) {

if (playlingField.getActiveCells() == (playlingField.getGridSize() * playlingField.getGridSize())) {
Log.i(TAG, "addNewCell: Field is full. Can't add new cell.");
return;
return changes;
}

Cell cell;
Expand All @@ -85,7 +90,10 @@ public void addNewCell() {

} while (playlingField.cellHasValue(cell.getRow(), cell.getColumn()));

changes.addCell(cell, DEFAULT_START_VALUE);
playlingField.setCellValue(cell.getRow(), cell.getColumn(), DEFAULT_START_VALUE);

return changes;
}

public void move(MOVE_DIRECTION direction) {
Expand All @@ -110,8 +118,7 @@ protected void move(MOVE_DIRECTION direction, boolean spawnNewCell) {
Log.v(TAG, "move: Executing move to " + direction + ".");
MovementChanges changes = playlingField.moveCells(direction);
if (spawnNewCell) {
addNewCell();
changes.gridStatus = playlingField.getGridStatus();
changes = addNewCell(changes);
}
updateMoveDoneListeners(changes);
} else {
Expand Down Expand Up @@ -148,15 +155,27 @@ private void updateMoveDoneListeners(MovementChanges changes) {
private void updateGameOverListeners() {

sendLocalBroadcast(BROADCAST_ACTION_GAME_OVER, null);

String username = DeployGate.getLoginUsername();
String message = String.format("%s just LOST a game.", (username == null) ? "UNKNOWN" : username);
DeployGate.logInfo(message);
}

private void updateGameWonListeners() {

sendLocalBroadcast(BROADCAST_ACTION_GAME_WON, null);
String username = DeployGate.getLoginUsername();
String message = String.format("%s just WON a game.", (username == null) ? "UNKNOWN" : username);
DeployGate.logInfo(message);
}

private void sendLocalBroadcast(String action, Bundle extras) {

if (mContext == null) {
Log.e(TAG, "sendLocalBroadcast: Context=null, can't broadcast action=" + action);
return;
}

Intent localIntent =
new Intent(BROADCAST_MCP)
.setAction(action);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package de.stefanhoth.android.got2048.logic.model;

import android.os.Parcel;
import android.os.Parcelable;

/**
* Represents one cell on the playing field
*
* @author Stefan Hoth <[email protected]>
* date: 20.03.14 20:25
* @since 0.1
*/
public class Cell {
public class Cell implements Parcelable {

private int row;
private int column;
Expand All @@ -25,10 +28,11 @@ public int getColumn() {
return column;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o == null || ((Object) this).getClass() != o.getClass()) return false;

Cell cell = (Cell) o;

Expand All @@ -45,4 +49,30 @@ public int hashCode() {
result = 31 * result + column;
return result;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.row);
dest.writeInt(this.column);
}

private Cell(Parcel in) {
this.row = in.readInt();
this.column = in.readInt();
}

public static Parcelable.Creator<Cell> CREATOR = new Parcelable.Creator<Cell>() {
public Cell createFromParcel(Parcel source) {
return new Cell(source);
}

public Cell[] newArray(int size) {
return new Cell[size];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Container to carry over changes a movement caused to the playing field
*
Expand All @@ -15,11 +19,15 @@ public class MovementChanges implements Parcelable {
public boolean cellsMoved;
public int pointsEarned;
public int[][] gridStatus;
private List<Cell> addedCells;
public int addedCellsValue;

public MovementChanges(int[][] gridStatus) {
cellsMoved = false;
pointsEarned = 0;
this.gridStatus = gridStatus;
this.addedCells = new ArrayList<>();
this.addedCellsValue = 2;
}

@Override
Expand All @@ -32,12 +40,16 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte(cellsMoved ? (byte) 1 : (byte) 0);
dest.writeInt(this.pointsEarned);
dest.writeSerializable(this.gridStatus);
dest.writeInt(this.addedCellsValue);
dest.writeParcelableArray(addedCells.toArray(new Cell[addedCells.size()]), 0);
}

private MovementChanges(Parcel in) {
this.cellsMoved = in.readByte() != 0;
this.pointsEarned = in.readInt();
this.gridStatus = (int[][]) in.readSerializable();
this.addedCellsValue = in.readInt();
this.addedCells = Arrays.asList((Cell[]) in.readParcelableArray(Cell.class.getClassLoader()));
}

public static Parcelable.Creator<MovementChanges> CREATOR = new Parcelable.Creator<MovementChanges>() {
Expand All @@ -49,4 +61,14 @@ public MovementChanges[] newArray(int size) {
return new MovementChanges[size];
}
};

public void addCell(Cell cell, int startValue) {

addedCells.add(cell);
this.addedCellsValue = startValue;
}

public List<Cell> getAddedCells() {
return addedCells;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package de.stefanhoth.android.got2048.widgets;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.ArrayAdapter;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import de.stefanhoth.android.got2048.R;
import de.stefanhoth.android.got2048.logic.model.Cell;

/**
* TODO describe class
Expand Down Expand Up @@ -114,6 +122,45 @@ public void updateGrid(int[][] values) {
requestLayout();
}

public void addCells(List<Cell> cellList, int value) {

if (cellList == null || cellList.size() == 0) {
Log.d(TAG, "addCells: No or empty cell list given. Won't add cells.");
return;
}

AnimatorSet set = new AnimatorSet();


Collection<Animator> animators = new ArrayList<>(cellList.size());

for (Cell cell : cellList) {
animators.addAll(addAndAnimateCell(cell, value));
}

set.playTogether(animators);
set.setDuration(500);
set.setInterpolator(new BounceInterpolator());
set.start();
}

private List<Animator> addAndAnimateCell(Cell cell, int value) {
SquareCellView cellView = cells[cell.getRow()][cell.getColumn()];

cellView.setScaleX(0.1f);
cellView.setScaleY(0.1f);
cellView.setAlpha(0.1f);

cellView.setValue(value);

List<Animator> animations = new ArrayList<>(2);
animations.add(ObjectAnimator.ofFloat(cellView, "scaleX", 1f));
animations.add(ObjectAnimator.ofFloat(cellView, "scaleY", 1f));
animations.add(ObjectAnimator.ofFloat(cellView, "alpha", 1f));

return animations;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

Expand Down

0 comments on commit 7c3eef2

Please sign in to comment.