private ArrayList<Position> GetPlayerPath(Position playerCurrentPos, Position targetPos) { ArrayList<Position> p = new ArrayList<Position>(); Position next = null; int directionX, directionY; int distanceX, distanceY; distanceX = playerCurrentPos.x - targetPos.x; if (distanceX < 0) { directionX = 1; distanceX = distanceX * -1; } else directionX = -1; distanceY = playerCurrentPos.y - targetPos.y; if (distanceY < 0) { distanceY = distanceY * -1; directionY = 1; } else directionY = -1; if ((distanceX > distanceY) && (currentMap.IsWalkable(playerCurrentPos.y, playerCurrentPos.x + directionX))) { next = new Position(playerCurrentPos.x + directionX, playerCurrentPos.y); } else { if (currentMap.IsWalkable(playerCurrentPos.y + directionY, playerCurrentPos.x)) next = new Position(playerCurrentPos.x, playerCurrentPos.y + directionY); else if ((currentMap.IsWalkable(playerCurrentPos.y, playerCurrentPos.x + directionX))) next = new Position(playerCurrentPos.x + directionX, playerCurrentPos.y); } if (next != null) { if (!((next.x == targetPos.x) && (next.y == targetPos.y))) p = GetPlayerPath(next, targetPos); p.add(0, next); } return p; }
private void CreatePlayerPath(MotionEvent event) { ArrayList<Position> Path = null; if (currentMap != null) { if ((event != null) && (event.getAction() == MotionEvent.ACTION_UP)) { Position playerPosition = new Position(currentMap.getCenterColumn(), currentMap.getCenterRow()); Position targetPosition = GetMapTouchedPosition(event); if (currentMap.IsWalkable(targetPosition.y, targetPosition.x)) { Path = GetPlayerPath(playerPosition, targetPosition); player.setNewPath(Path); synchronized (this.getHolder()) { Effects.add(new Effect(this, targetPosition, currentMap)); } } } } if (Path != null) for (int i = 0; i < Path.size(); i++) { Log.d("PATH: ", Path.get(i).x + ", " + Path.get(i).y); } }