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

Commit

Permalink
Merge pull request #23 from darnuria/Amelioration/Deplacement_de_plus…
Browse files Browse the repository at this point in the history
…ieurs_cases

Amélioration/Déplacement de plusieurs cases
  • Loading branch information
Axel Viala committed Apr 6, 2016
2 parents 15a682e + 61c0bd1 commit b014b13
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 36 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/android/rushdroid/GameActivity.java
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
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
70 changes: 48 additions & 22 deletions app/src/main/java/android/rushdroid/GameView.java
Expand Up @@ -33,6 +33,7 @@ final public class GameView extends SurfaceView {
private Integer current = null;
private final Model m;
private final Bitmap[] bitmaps = new Bitmap[4];
private Piece down_p = null;
private int down_x;
private int down_y;

Expand Down Expand Up @@ -63,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 @@ -113,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 @@ -162,25 +162,51 @@ private Position interpolation(int x, int y) {
return new Position(x * this.game_width / this.surface_width, y * this.game_height / this.surface_height);
}

/*
// A lot of side effects.
private boolean help_move(int prev, int now) {
return prev <= now ? this.m.moveForward(this.current) : this.m.moveBackward(this.current);
}
*/

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();
int y = (int) e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN: {
this.current = m.getIdByPos(interpolation(x, y));
this.down_x = x;
this.down_y = y;
// System.out.println(p);
Position p = interpolation(x, y);
this.current = m.getIdByPos(p);
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_UP: {
} case MotionEvent.ACTION_MOVE: {
if (this.current != null) {
if (m.getOrientation(current) == Direction.VERTICAL) { help_move(down_y, y); } else { help_move(down_x, x); }
Position p = interpolation(x, y);
if (m.getOrientation(current) == Direction.VERTICAL) {
this.down_y = this.help_move(down_y, p.y);
} else {
this.down_x = this.help_move(down_x, p.x);
}
}
return true;
} case MotionEvent.ACTION_UP: {
if (this.current != null) {
if (!down_p.getPos().equals(m.piece(current).getPos())) {
this.m.undoPush(down_p);
this.m.redoClear();
this.down_p = null;
}
this.current = null;
}
return true;
Expand All @@ -190,4 +216,4 @@ public boolean onTouchEvent(@NonNull MotionEvent e) {
}
}
}
}
}
11 changes: 4 additions & 7 deletions app/src/main/java/android/rushdroid/model/Model.java
Expand Up @@ -15,7 +15,6 @@ public class Model implements IModel {
final private Deque<Piece> undo = new ArrayDeque<>();
final private Deque<Piece> redo = new ArrayDeque<>();


public Model(@NonNull List<Piece> pieces) {
this.pieces = pieces;
this.setAllPieces();
Expand Down Expand Up @@ -121,6 +120,10 @@ public int getCol(int id) {
return this.pieces.get(id).getPos().getCol();
}

public void undoPush (Piece p) { undo.push(p); }

public void redoClear () { redo.clear(); }

// TODO: Using soft-wired end-of-game position.
public boolean endOfGame() {
Integer id = this.grid.get(new Position(5, 2));
Expand All @@ -132,9 +135,6 @@ public boolean moveForward(int id) {
Position pos = p.getPos();
int size = p.getSize();

this.undo.push(p);
while (!redo.isEmpty()) { redo.pop(); }

switch (p.getOrientation()) {
case HORIZONTAL: {
int x = pos.getCol() + size;
Expand Down Expand Up @@ -164,9 +164,6 @@ public boolean moveBackward(int id) {
Position pos = p.getPos();
int offset = p.getSize() - 1;

this.undo.push(p);
while (!redo.isEmpty()) { redo.pop(); }

switch (p.getOrientation()) {
case HORIZONTAL: {
int x = pos.getCol() - 1;
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/android/rushdroid/model/Position.java
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 b014b13

Please sign in to comment.