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

Commit

Permalink
Amélioration/Déplacement de plusieurs cases
Browse files Browse the repository at this point in the history
* Modification de onTouchEvent dans GameView.java
* Ajout de 2 méthodes undoPush et redoClear dans Model.java afin de push
les pièces depuis onTouchEvent
  • Loading branch information
Finiganis committed Apr 4, 2016
1 parent 15a682e commit 72160f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
47 changes: 42 additions & 5 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 @@ -162,25 +163,61 @@ 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);
}
*/

@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);
this.down_x = p.getCol();
this.down_y = p.getLig();
if (current != null) {
this.down_p = m.piece(current);
}
return true;
}
case MotionEvent.ACTION_MOVE: {
if (this.current != null) {
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; }
}
}
} 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; }
}
}
}
}
return true;
}
case MotionEvent.ACTION_UP: {
if (this.current != null) {
if (m.getOrientation(current) == Direction.VERTICAL) { help_move(down_y, y); } else { help_move(down_x, x); }
if (down_p.getPos().getCol() != m.piece(current).getPos().getCol()
|| down_p.getPos().getLig() != m.piece(current).getPos().getLig()) {
this.m.undoPush(down_p);
this.m.redoClear();
this.down_p = null;
}
this.current = null;
}
return true;
Expand All @@ -190,4 +227,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

0 comments on commit 72160f2

Please sign in to comment.