/** * get all possible playing fields which could be occur when the tile comes into play * * @return */ public ArrayList<Round> getAllNextPossiblePlayingFields() { ArrayList<Round> allpos = new ArrayList<Round>(); ArrayList<Tile> freeTiles = this.getFreeCells(); // get all unassigned tiles for (int i = 0; i < freeTiles.size(); i++) { // iterate trough all tiles Tile ctile = freeTiles.get(i); PlayingField p_new_2 = (PlayingField) this.makeCopy(); p_new_2.setValue(ctile.getX(), ctile.getY(), 2); allpos.add( new Round( p_new_2, 0.9, "2 at x=" + ctile.getX() + ", y=" + ctile.getY())); // set 2 with probability of 0.9 PlayingField p_new_4 = (PlayingField) this.makeCopy(); p_new_4.setValue(ctile.getX(), ctile.getY(), 4); allpos.add( new Round( p_new_4, 0.1, "4 at x=" + ctile.getX() + ", y=" + ctile.getY())); // set 4 with probability of 0.1 } return allpos; }
boolean checkTileCollision(Tile t) { Rectangle r1 = new Rectangle(getX(), getY(), WIDTH, HEIGHT); if (t.getCollisionType() != 0) { Rectangle r2Whole = new Rectangle(t.getX(), t.getY(), 32, 32); return (r1.intersects(r2Whole) || r2Whole.intersects(r1)); } else { Rectangle r2p1 = new Rectangle(t.getX(), t.getY(), 16, 16); Rectangle r2p2 = new Rectangle(t.getX() + 16, t.getY(), 16, 16); Rectangle r2p3 = new Rectangle(t.getX(), t.getY() + 16, 16, 16); Rectangle r2p4 = new Rectangle(t.getX() + 16, t.getY() + 16, 16, 16); // check collision on the first quadrant if the byte is 1 if (t.getTileByte(0) == 1) { return (r1.intersects(r2p1) || r2p1.intersects(r1)); // check collision on the second quadrant if the byte is 1 } else if (t.getTileByte(1) == 1) { return (r1.intersects(r2p2) || r2p2.intersects(r1)); // check collision on the third quadrant if the byte is 1 } else if (t.getTileByte(2) == 1) { return (r1.intersects(r2p3) || r2p3.intersects(r1)); // check collision on the fourth quadrant if the byte is 1 } else if (t.getTileByte(3) == 1) { return (r1.intersects(r2p4) || r2p4.intersects(r1)); // otherwise this tile does not have any bytes that are handled // we could add different types of collisions } else { return false; } } }
void fixTileCollision(Tile t) { while (checkTileCollision(t)) { if (x < t.getX()) { x -= 2; } else if (x > t.getX()) { x += 2; } if (y < t.getY()) { y -= 2; } else if (y > t.getY()) { y += 2; } } }
private void fireTilesChangedIncludeBorder(Set<Tile> tiles) { if (showBorder && (tileProvider instanceof Dimension) && (((Dimension) tileProvider).getDim() == DIM_NORMAL) && (((Dimension) tileProvider).getBorder() != null)) { final Set<Point> coordSet = new HashSet<>(); for (Tile tile : tiles) { final int tileX = tile.getX(), tileY = tile.getY(), borderSize = ((Dimension) tileProvider).getBorderSize(); for (int dx = -borderSize; dx <= borderSize; dx++) { for (int dy = -borderSize; dy <= borderSize; dy++) { coordSet.add(getTileCoordinates(tileX + dx, tileY + dy)); } } } for (TileListener listener : listeners) { listener.tilesChanged(this, coordSet); } } else { Set<Point> coords = tiles.stream().map(this::getTileCoordinates).collect(Collectors.toSet()); for (TileListener listener : listeners) { listener.tilesChanged(this, coords); } } }
public boolean changeTile(int x, int y, String image) { Tile temp = getTile(x, y); Entity ent = game.getEntityManager().getEntity(temp.getX(), temp.getY()); if (ent != null) { if (image.contains("concrete")) if (ent.getImage().contains("dinosaur") || ent.getImage().contains("fence")) return false; } if (game.getEntityManager().entityAt(temp.getX(), temp.getY()) && image.contains("water")) return false; if (game.getParkManager().purchaseEntity(image)) temp.setImage(image); return true; }
public URL getTileUrl(Tile tile) { String url = getServerUrl() + "/" + tile.getZoom() + "/" + tile.getX() + "/" + tile.getY() + ".png"; try { return new URL(url); } catch (MalformedURLException e) { return null; } }
private static Tile[] getAdjTiles(Tile[][] grid, Tile x) { Tile[] y = new Tile[4]; if (!(x.getX() == 0)) y[3] = grid[x.getX() - 1][x.getY()]; if (!(x.getY() == 0)) y[0] = grid[x.getX()][x.getY() - 1]; if (!(x.getX() == grid.length - 1)) y[1] = grid[x.getX() + 1][x.getY()]; if (!(x.getY() == grid[0].length - 1)) y[2] = grid[x.getX()][x.getY() + 1]; return y; }
public void setTile(Tile tile) { int x = tile.getX(), y = tile.getY(); if (x >= 0 && y >= 0 && x < sizeX && y < sizeY) { tiles[x][y] = tile; } }
/** * Convert the actual tile coordinates to zoom-corrected (tile provider coordinate system) * coordinates. * * @param tile The tile of which to convert the coordinates. * @return The coordinates of the tile in the tile provider coordinate system (corrected for * zoom). */ private Point getTileCoordinates(Tile tile) { return getTileCoordinates(tile.getX(), tile.getY()); }
public void Draw(Canvas canvas) { if (!initialized) { tiles = new ArrayList<Tile>(); water = new ArrayList<Tile>(); TILE_WIDTH = game.getBitmapManager().getBitmap(R.drawable.terrain_grass).getWidth(); TILE_HEIGHT = game.getBitmapManager().getBitmap(R.drawable.terrain_grass).getHeight(); generateTerrain(tiles); generateWater(water); initialized = true; } float tempWaterOffset = waterOffset; tempWaterOffset = (float) Math.round(waterOffset); tempWaterOffset = tempWaterOffset % 2 == 0 ? tempWaterOffset : tempWaterOffset + 1; for (int i = 0; i < water.size(); i++) { float offset_x = (float) (water.get(i).x) * TILE_WIDTH / 2; float offset_y = (float) (water.get(i).y) * TILE_HEIGHT; if (water.get(i).x % 2 == 1) offset_y += TILE_HEIGHT / 2; offset_x += tempWaterOffset; offset_y += tempWaterOffset / 2; offset_x -= 2 * TILE_WIDTH; offset_y -= 2 * TILE_HEIGHT; offset_x += (game.getCameraManager().getX() % 2) * TILE_WIDTH / 2; offset_y += (game.getCameraManager().getY() % 1) * TILE_HEIGHT; Bitmap bmp = game.getBitmapManager().getBitmap(water.get(i).getImageId()); canvas.drawBitmap(bmp, offset_x, offset_y, null); } float hoverOffsetX = 100000; float hoverOffsetY = 100000; int tempHoverX = hoverX - TILE_WIDTH / 2; int tempHoverY = hoverY - TILE_HEIGHT; Tile temp = tiles.get(0); for (int i = 0; i < tiles.size(); i++) { Bitmap bmp = game.getBitmapManager().getBitmap(tiles.get(i).getImageId()); float offset_x = (float) (tiles.get(i).x + game.getCameraManager().getX()) * TILE_WIDTH / 2; float offset_y = (float) (tiles.get(i).y + game.getCameraManager().getY()) * TILE_HEIGHT; if (tiles.get(i).x % 2 == 1) offset_y += TILE_HEIGHT / 2; if (Math.sqrt(Math.pow(offset_x - tempHoverX, 2) + Math.pow(offset_y - tempHoverY, 2)) < Math.sqrt( Math.pow(hoverOffsetX - tempHoverX, 2) + Math.pow(hoverOffsetY - tempHoverY, 2))) { hoverOffsetX = offset_x; hoverOffsetY = offset_y; temp = tiles.get(i); } if (offset_x < (canvas.getWidth() + TILE_WIDTH) && offset_y < (canvas.getHeight() + TILE_HEIGHT) && offset_x > -TILE_WIDTH && offset_y > -TILE_HEIGHT) canvas.drawBitmap(bmp, offset_x, offset_y, null); } int color = Color.parseColor("#96FFFF00"); Entity ent = game.getEntityManager().getEntity(temp.getX(), temp.getY()); if (ent != null) { if (hoverImage.contains("concrete")) if (ent.getImage().contains("dinosaur") || ent.getImage().contains("fence")) color = Color.parseColor("#96FF0000"); } if (game.getEntityManager().entityAt(temp.getX(), temp.getY()) && hoverImage.contains("water")) color = Color.parseColor("#96FF0000"); Paint paint = new Paint(color); ColorFilter filter = new LightingColorFilter(color, 1); paint.setColorFilter(filter); canvas.drawBitmap( game.getBitmapManager() .getBitmap( game.getResources() .getIdentifier(hoverImage, "drawable", "cpsc372.dinosaur_tycoon")), hoverOffsetX, hoverOffsetY, paint); long delta = (System.currentTimeMillis() - gameTime); Update(delta); gameTime = System.currentTimeMillis(); }
public int getOffsetY() { return tile.getY() % 8; }
/** * Searches the view port on the live map for caches. The strategy dictates if only live map * information is used or if an additional searchByCoordinates query is issued. * * @param viewport Area to search * @param tokens Live map tokens * @param strategy Strategy for data retrieval and parsing, @see Strategy */ @NonNull private static SearchResult searchByViewport( final Viewport viewport, final MapTokens tokens, final LivemapStrategy strategy) { Log.d("GCMap.searchByViewport" + viewport.toString()); final SearchResult searchResult = new SearchResult(); if (Settings.isDebug()) { searchResult.setUrl(viewport.getCenter().format(Format.LAT_LON_DECMINUTE)); } if (strategy.flags.contains(LivemapStrategy.Flag.LOAD_TILES)) { final Set<Tile> tiles = Tile.getTilesForViewport(viewport); if (Settings.isDebug()) { searchResult.setUrl( new StringBuilder() .append(tiles.iterator().next().getZoomLevel()) .append(Formatter.SEPARATOR) .append(searchResult.getUrl()) .toString()); } for (final Tile tile : tiles) { if (!Tile.cache.contains(tile)) { final Parameters params = new Parameters( "x", String.valueOf(tile.getX()), "y", String.valueOf(tile.getY()), "z", String.valueOf(tile.getZoomLevel()), "ep", "1", "app", "cgeo"); if (tokens != null) { params.put("k", tokens.getUserSession(), "st", tokens.getSessionToken()); } if (Settings.isExcludeMyCaches()) { // works only for PM params.put("hf", "1", "hh", "1"); // hide found, hide hidden } // ect: exclude cache type (probably), comma separated list if (Settings.getCacheType() != CacheType.ALL) { params.put("ect", getCacheTypeFilter(Settings.getCacheType())); } if (tile.getZoomLevel() != 14) { params.put("_", String.valueOf(System.currentTimeMillis())); } // The PNG must be requested first, otherwise the following request would always return // with 204 - No Content final Single<Bitmap> bitmapObs = Tile.requestMapTile(params).onErrorResumeNext(Single.<Bitmap>just(null)); final Single<String> dataObs = Tile.requestMapInfo(GCConstants.URL_MAP_INFO, params, GCConstants.URL_LIVE_MAP) .onErrorResumeNext(Single.just("")); try { Single.zip( bitmapObs, dataObs, new Func2<Bitmap, String, Void>() { @Override public Void call(final Bitmap bitmap, final String data) { final boolean validBitmap = bitmap != null && bitmap.getWidth() == Tile.TILE_SIZE && bitmap.getHeight() == Tile.TILE_SIZE; if (StringUtils.isEmpty(data)) { Log.w( "GCMap.searchByViewport: No data from server for tile (" + tile.getX() + "/" + tile.getY() + ")"); } else { final SearchResult search = parseMapJSON(data, tile, validBitmap ? bitmap : null, strategy); if (CollectionUtils.isEmpty(search.getGeocodes())) { Log.e( "GCMap.searchByViewport: No cache parsed for viewport " + viewport); } else { synchronized (searchResult) { searchResult.addSearchResult(search); } } synchronized (Tile.cache) { Tile.cache.add(tile); } } // release native bitmap memory if (bitmap != null) { bitmap.recycle(); } return null; } }) .toBlocking() .value(); } catch (final Exception e) { Log.e("GCMap.searchByViewPort: connection error"); } } } // Check for vanished found caches if (tiles.iterator().next().getZoomLevel() >= Tile.ZOOMLEVEL_MIN_PERSONALIZED) { searchResult.addFilteredGeocodes( DataStore.getCachedMissingFromSearch( searchResult, tiles, GCConnector.getInstance(), Tile.ZOOMLEVEL_MIN_PERSONALIZED - 1)); } } if (strategy.flags.contains(Flag.SEARCH_NEARBY) && Settings.isGCPremiumMember()) { final Geopoint center = viewport.getCenter(); if ((lastSearchViewport == null) || !lastSearchViewport.contains(center)) { // FIXME We don't have a RecaptchaReceiver!? final SearchResult search = GCParser.searchByCoords(center, Settings.getCacheType(), false, null); if (search != null && !search.isEmpty()) { final Set<String> geocodes = search.getGeocodes(); lastSearchViewport = DataStore.getBounds(geocodes); searchResult.addGeocodes(geocodes); } } } return searchResult; }
public void removeTile(Tile tile) { field[tile.getX()][tile.getY()] = null; }
public void insertTile(Tile tile) { field[tile.getX()][tile.getY()] = tile; }
private boolean assignSpeeds(Map m, ArrayList<Road> roads) { int[][] speeds = new int[m.getLengthX()][m.getLengthY()]; int count = 0; for (Road r : roads) { Tile[] adjTiles = getAdjTiles(m.grid, m.grid[r.getX()][r.getY()]); Road start = null; for (int i = 0; i < 4; i++) { if (adjTiles[i] instanceof TrafficLight) { start = r; } if (adjTiles[i] == null || adjTiles[i] instanceof NonRoad) { if (i == r.getDirection() || i == Math.abs(r.getDirection() - 4)) { start = r; } } } if (start != null) { int dir = start.getDirection(); int length = 1; Tile t = r; // if(m.getInDir(r,dir) != null){ t = m.getInDir(r, dir); if (t instanceof TrafficLight || t == null || t instanceof NonRoad) { int reverseDir = (dir + 2 > 3) ? dir - 2 : dir + 2; if (m.getInDir(r, reverseDir) != null) t = m.getInDir(r, reverseDir); while (!(t == null || t instanceof TrafficLight || t instanceof NonRoad)) { length++; if (m.getInDir(t, reverseDir) != null) t = m.getInDir(t, reverseDir); else break; } for (int i = 0; i < length; i++) { t = m.getInDir(t, dir); // reverse direction speeds[t.getX()][t.getY()] = determineSpeed(length); } } else { while (!(t == null || t instanceof TrafficLight || t instanceof NonRoad)) { length++; if (m.getInDir(t, dir) != null) t = m.getInDir(t, dir); else break; } int reverseDir = (dir + 2 > 3) ? dir - 2 : dir + 2; for (int i = 0; i < length; i++) { t = m.getInDir(t, reverseDir); // reverse directions speeds[t.getX()][t.getY()] = determineSpeed(length); } } } // } count++; } for (Road r : roads) { r.setSpeed(speeds[r.getX()][r.getY()]); } return true; }