Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
Polish and littles changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
darnuria committed Apr 6, 2016
1 parent 72160f2 commit 61c0bd1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/android/rushdroid/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
public class GameActivity extends Activity {
Model game;


@Override
protected void onCreate(Bundle savedInstanceState) {
this.game = ((GameApplication) this.getApplication()).game();
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/android/rushdroid/GameThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ class GameThread extends Thread {
final private GameView view;
final private SurfaceHolder holder;
private boolean running = false;
private boolean work = false;

public GameThread(GameView view, SurfaceHolder holder) {
this.view = view;
this.holder = holder;
}

// flag to hold game state
protected void setRunning(boolean running) {
this.running = running;
protected void setOff() {
this.running = false;
}

protected void setOn() {
this.running = true;
}

/**
Expand All @@ -46,7 +51,7 @@ public void run() {
}
}
try {
Thread.sleep(600);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down
71 changes: 30 additions & 41 deletions app/src/main/java/android/rushdroid/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public void surfaceCreated(@NonNull SurfaceHolder h) {
surface_height = GameView.this.getHeight();
fillArray(xs, surface_width, game_width);
fillArray(ys, surface_height, game_height);
this.th.setRunning(true);
this.th.setOn();
this.th.start();
}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder h) {
// tell the thread to shut down and wait for it to finish
// this is a clean shutdown
this.th.setRunning(false);
this.th.setOff();
boolean retry = true;
while (retry) {
try {
Expand Down Expand Up @@ -114,20 +114,19 @@ private void drawGrid(Canvas c) {
private void drawGame(@NonNull Canvas c, Iterable<Piece> pieces) {
int ratioY = this.surface_height / this.game_height;
int ratioX = this.surface_width / this.game_width;
for (Piece p : pieces) {
int xp = p.getPos().getCol();
int yp = p.getPos().getLig();

if (p.getOrientation() == Direction.VERTICAL) {
int x2 = (xp + 1) * ratioX;
int y2 = (yp + p.getSize()) * ratioY;
Bitmap bitmap = (p.getSize() == 2) ? (this.bitmaps[0]) : (this.bitmaps[1]);
c.drawBitmap(bitmap, null, new RectF(xp * ratioX, yp * ratioY, x2, y2), null);
for (Piece piece : pieces) {
Position p = piece.getPos();
int size = piece.getSize();
if (piece.getOrientation() == Direction.VERTICAL) {
int x2 = (p.x + 1) * ratioX;
int y2 = (p.y + size) * ratioY;
Bitmap bitmap = (size == 2) ? (this.bitmaps[0]) : (this.bitmaps[1]);
c.drawBitmap(bitmap, null, new RectF(p.x * ratioX, p.y * ratioY, x2, y2), null);
} else {
int x2 = (xp + p.getSize()) * ratioX;
int y2 = (yp + 1) * ratioY;
Bitmap bitmap = (p.getSize() == 2) ? (this.bitmaps[2]) : (this.bitmaps[3]);
c.drawBitmap(bitmap, null, new RectF(xp * ratioX, yp * ratioY, x2, y2), null);
int x2 = (p.x + size) * ratioX;
int y2 = (p.y + 1) * ratioY;
Bitmap bitmap = (size == 2) ? (this.bitmaps[2]) : (this.bitmaps[3]);
c.drawBitmap(bitmap, null, new RectF(p.x * ratioX, p.y * ratioY, x2, y2), null);
}
}
drawGrid(c);
Expand Down Expand Up @@ -170,6 +169,12 @@ private boolean help_move(int prev, int now) {
}
*/

int help_move(int current, int test) {
if (current < test && this.m.moveForward(this.current)) { return current + 1; }
else if (current > test && this.m.moveBackward(this.current)) { return current - 1; }
else { return current; }
}

@Override
public boolean onTouchEvent(@NonNull MotionEvent e) {
int x = (int) e.getX();
Expand All @@ -179,41 +184,25 @@ public boolean onTouchEvent(@NonNull MotionEvent e) {
// System.out.println(p);
Position p = interpolation(x, y);
this.current = m.getIdByPos(p);
this.down_x = p.getCol();
this.down_y = p.getLig();
if (current != null) {
this.down_p = m.piece(current);
if (this.current != null) {
this.down_p = m.piece(this.current);
this.down_x = p.x;
this.down_y = p.y;
}
return true;
}
case MotionEvent.ACTION_MOVE: {
} case MotionEvent.ACTION_MOVE: {
if (this.current != null) {
Position p = interpolation(x, y);
if (m.getOrientation(current) == Direction.VERTICAL) {
int test = interpolation(x, y).getLig();
if (down_y < test) {
if (this.m.moveForward(this.current)) { down_y += 1; }
} else {
if (down_y > test) {
if (this.m.moveBackward(this.current)) { down_y -= 1; }
}
}
this.down_y = this.help_move(down_y, p.y);
} else {
int test = interpolation(x, y).getCol();
if (down_x < test) {
if (this.m.moveForward(this.current)) { down_x += 1; }
} else {
if (down_x > test) {
if (this.m.moveBackward(this.current)) { down_x -= 1; }
}
}
this.down_x = this.help_move(down_x, p.x);
}
}
return true;
}
case MotionEvent.ACTION_UP: {
} case MotionEvent.ACTION_UP: {
if (this.current != null) {
if (down_p.getPos().getCol() != m.piece(current).getPos().getCol()
|| down_p.getPos().getLig() != m.piece(current).getPos().getLig()) {
if (!down_p.getPos().equals(m.piece(current).getPos())) {
this.m.undoPush(down_p);
this.m.redoClear();
this.down_p = null;
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/android/rushdroid/model/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import android.support.annotation.Nullable;

final public class Position implements IPosition {
private final int x;
private final int y;
public final int x;
public final int y;

/**
* @param x An integer
Expand Down Expand Up @@ -63,7 +63,6 @@ public Position add(@NonNull Position p) {
public boolean equals(@Nullable Object o) {
if (this == o) { return true; }
if (o == null || this.getClass() != o.getClass()) { return false; }

Position p = (Position) o;
return this.x == p.x && this.y == p.y;
}
Expand Down

0 comments on commit 61c0bd1

Please sign in to comment.