Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private boolean isValidIndex(int y, int x) {
- return y >= 0 && y < matrix.length
- && x >= 0 && x < matrix[y].length;
- }
- private Moveable getMoveableAt(int y, int x) {
- for (Player p : players) {
- if (p.getMatrixX() == x && p.getMatrixY() == y) {
- return p;
- }
- }
- for (Block p : blocks) {
- if (p.getMatrixX() == x && p.getMatrixY() == y) {
- return p;
- }
- }
- return null;
- }
- /**
- * Validar posição com base nos
- *
- * @param objMoveable objeto para mover
- * @param dx distância em x
- * @param dy distância em y
- * @return
- */
- public boolean canMoveTo(Moveable objMoveable, int dx, int dy, boolean push) {
- int x = objMoveable.getMatrixX();
- int y = objMoveable.getMatrixY();
- if (!isValidIndex(y + dy, x + dx)) {
- return false;
- }
- if (matrix[y + dy][x + dx] instanceof Wall) {
- return false;
- }
- //objeto movivel que esta na posicao que vai ser ocupada
- Moveable obj = getMoveableAt(y + dy, x + dx);
- //existe um objeto movivel mas nao consigo arrastar
- if( obj != null && push == false)
- return false;
- //existe um objeto e consigo arrastar
- if (push && obj != null) {
- if(obj instanceof Block){
- if(canMoveTo(obj, dx, dy, false)){
- obj.move(dx, dy);
- myListener.onBlockMove((Block) obj, dx, dy);
- return true;
- }
- }
- return canMoveTo(obj, dx, dy, false);
- }
- //esta vazio
- else if (matrix[y + dy][x + dx] instanceof Empty) {
- return true;
- }
- //é um alvo
- else if (matrix[y + dy][x + dx] instanceof Target) {
- return true;
- }
- //existe uma coisa esquesita
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement