Advertisement
Tugamars_PT

Untitled

Jun 13th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. private boolean isValidIndex(int y, int x) {
  2. return y >= 0 && y < matrix.length
  3. && x >= 0 && x < matrix[y].length;
  4. }
  5.  
  6. private Moveable getMoveableAt(int y, int x) {
  7. for (Player p : players) {
  8. if (p.getMatrixX() == x && p.getMatrixY() == y) {
  9. return p;
  10. }
  11. }
  12. for (Block p : blocks) {
  13. if (p.getMatrixX() == x && p.getMatrixY() == y) {
  14. return p;
  15. }
  16. }
  17. return null;
  18. }
  19.  
  20. /**
  21. * Validar posição com base nos
  22. *
  23. * @param objMoveable objeto para mover
  24. * @param dx distância em x
  25. * @param dy distância em y
  26. * @return
  27. */
  28. public boolean canMoveTo(Moveable objMoveable, int dx, int dy, boolean push) {
  29. int x = objMoveable.getMatrixX();
  30. int y = objMoveable.getMatrixY();
  31.  
  32. if (!isValidIndex(y + dy, x + dx)) {
  33. return false;
  34. }
  35. if (matrix[y + dy][x + dx] instanceof Wall) {
  36. return false;
  37. }
  38. //objeto movivel que esta na posicao que vai ser ocupada
  39. Moveable obj = getMoveableAt(y + dy, x + dx);
  40. //existe um objeto movivel mas nao consigo arrastar
  41. if( obj != null && push == false)
  42. return false;
  43. //existe um objeto e consigo arrastar
  44. if (push && obj != null) {
  45.  
  46. if(obj instanceof Block){
  47. if(canMoveTo(obj, dx, dy, false)){
  48. obj.move(dx, dy);
  49. myListener.onBlockMove((Block) obj, dx, dy);
  50. return true;
  51. }
  52. }
  53.  
  54. return canMoveTo(obj, dx, dy, false);
  55. }
  56. //esta vazio
  57. else if (matrix[y + dy][x + dx] instanceof Empty) {
  58. return true;
  59. }
  60. //é um alvo
  61. else if (matrix[y + dy][x + dx] instanceof Target) {
  62. return true;
  63. }
  64. //existe uma coisa esquesita
  65. return false;
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement