// when tiles transition, we use special images private Image checkBorders(int x, int y, Image im, int t, Image src) { if (tval[x + 1][y] == t) im = Pix.getSlicedImage(new Point(0, 1), src, 1, 1, 1, 1); else if (tval[x - 1][y] == t) im = Pix.getSlicedImage(new Point(2, 1), src, 1, 1, 1, 1); else if (tval[x][y + 1] == t) im = Pix.getSlicedImage(new Point(1, 0), src, 1, 1, 1, 1); else if (tval[x][y - 1] == t) im = Pix.getSlicedImage(new Point(1, 2), src, 1, 1, 1, 1); else if (tval[x + 1][y + 1] == t) im = Pix.getSlicedImage(new Point(0, 0), src, 1, 1, 1, 1); else if (tval[x - 1][y + 1] == t) im = Pix.getSlicedImage(new Point(2, 0), src, 1, 1, 1, 1); else if (tval[x - 1][y - 1] == t) im = Pix.getSlicedImage(new Point(2, 2), src, 1, 1, 1, 1); else if (tval[x + 1][y - 1] == t) im = Pix.getSlicedImage(new Point(0, 2), src, 1, 1, 1, 1); return im; }
private Image checkMountain(int x, int y, int sx, int sy) { Point p = null; if (x == 0) { if (y == 0) p = new Point(0, 0); else if (y == sy - 1) p = new Point(0, 2); else p = new Point(0, 1); } else if (x == sx - 1) { if (y == 0) p = new Point(2, 0); else if (y == sy - 1) p = new Point(2, 2); else p = new Point(2, 1); } else if (y == 0) p = new Point(1, 0); else p = new Point(1, 2); return Pix.getSlicedImage(p, Pix.imSrcGrass2Mountain, 1, 1, 1, 1); }
private void drawBubble(Graphics2D g, Bubble b) { int imx = b.stage % 7; int imy = b.stage / 7; BufferedImage im = Pix.getSlicedImage(new Point(imx, imy), Pix.imSwampBubble, 1, 2, 1, 0); g.drawImage(im, bubble.x * Pix.SZ, (bubble.y - 1) * Pix.SZ, null); }
private void initGfx(Graphics2D gmain, Terra terra, double scale, int dx, int dy) { if (lastInitScale == scale) return; lastInitScale = scale; bgImg = gmain.getDeviceConfiguration().createCompatibleImage(dx, dy); Graphics2D g = (Graphics2D) bgImg.getGraphics(); // gradient background GradientPaint gp = new GradientPaint(50.0f, 0.0f, new Color(0x33dd33), 50.0f, dy, new Color(0x33bb33)); g.setPaint(gp); g.fillRect(0, 0, dx, dy); Image grass = Pix.getSlicedImage(new Point(1, 1), Pix.imSrcGrass, 1, 1, 1, 1); int sx = dx / Pix.SZ; int sy = dy / Pix.SZ; tval = new int[sx][sy]; for (int x = 0; x < sx; x++) { for (int y = 0; y < sy; y++) { tval[x][y] = Terrain.GRASS; } } // apply terrain from terra for (int i = 0; i < terra.terrain.size(); i++) { Terrain terrain = terra.terrain.get(i); Double rec = terrain.bounds; int ax = (int) ((rec.x) / terra.dimX * sx + 0.5); int ay = (int) ((rec.y) / terra.dimY * sy + 0.5); int bx = (int) ((rec.x + rec.width) / terra.dimX * sx + 0.5); int by = (int) ((rec.y + rec.height) / terra.dimY * sy + 0.5); for (int x = ax; x < bx & x < sx; x++) { for (int y = ay; y < by && y < sy; y++) { tval[x][y] = terrain.index; } } } // make mountainous border for (int x = 0; x < sx; x++) { tval[x][0] = Terrain.MOUNTAIN; tval[x][sy - 1] = Terrain.MOUNTAIN; } for (int y = 0; y < sy; y++) { tval[0][y] = Terrain.MOUNTAIN; tval[sx - 1][y] = Terrain.MOUNTAIN; } // draw terrain for (int x = 0; x < sx; x++) { for (int y = 0; y < sy; y++) { boolean chk = (x != 0 && y != 0 && x != sx - 1 && y != sy - 1); Image im; switch (tval[x][y]) { case Terrain.GRASS: default: im = grass; if (r.nextInt(27) == 0) { im = Pix.getSlicedImage(new Point(r.nextInt(3), 0), Pix.imSrcGrassCrater, 1, 1, 1, 1); } if (chk) { im = checkBorders(x, y, im, Terrain.FOREST, Pix.imSrcTree2Grass); im = checkBorders(x, y, im, Terrain.SWAMP, Pix.imSrcSwamp2Grass); } break; case Terrain.FOREST: im = Pix.getSlicedImage(new Point(1, 1), Pix.imSrcTree2Grass, 1, 1, 1, 1); break; case Terrain.SWAMP: im = Pix.getSlicedImage(new Point(1, 1), Pix.imSrcSwamp2Grass, 1, 1, 1, 1); if (r.nextInt(37) == 0) { im = Pix.imSwampPlant; } break; case Terrain.MOUNTAIN: im = checkMountain(x, y, sx, sy); break; } g.drawImage(im, x * Pix.SZ, y * Pix.SZ, null); } } }
private void drawUnits( Terra terra, List<Unit> units, double scale, Graphics2D g, boolean showInfo) { for (Unit unit : units) { Composite origComposite = g.getComposite(); double x = unit.x * scale; double y = unit.y * scale; // choose color Faction faction = unit.getFaction(); if (faction == null) g.setColor(Color.LIGHT_GRAY); else { g.setColor(factionCols[faction.index]); } int hp_off = Pix.SZ / 2 + 1; if (unit instanceof City) { City city = (City) unit; BufferedImage im; if (faction == null) { im = Pix.getSlicedImage(new Point(0, 0), Pix.imSrcBaseNeutral, 3, 3, 0, 1); } else { int stg = 0; if (cityFlashAnim.containsKey(city)) { Anim anim = cityFlashAnim.get(city); stg = (frame - anim.frameStart) / 2; if (stg >= 7) { // over stg = 0; cityFlashAnim.remove(city); } } if (city.getHp() < city.maxHP / 10.0) stg = 4; int imx = stg % 4; int imy = stg / 4; im = Pix.getSlicedImage(new Point(imx, imy), Pix.imSrcBase[faction.index], 3, 3, 0, 1); } g.drawImage(im, (int) x - im.getWidth() / 2, (int) y - Pix.SZ * 3 / 2, null); hp_off = Pix.SZ; } else { double ang = 0; if (unit instanceof Ambulatory) ang = ((Ambulatory) unit).getMoveAngle(); ang = (ang + Math.PI * 2.0) % (Math.PI * 2.0); ang -= Math.PI / 2.0; Image im = null; if (unit instanceof Grunt) im = Pix.imSrcTank[faction.index]; else if (unit instanceof Hovercraft) im = Pix.imSrcBoat[faction.index]; else if (unit instanceof Artillery) im = Pix.imSrcArtil[faction.index]; AffineTransform xform = new AffineTransform(); double px = x - 20 / 2; double py = y - 20 / 2; xform.rotate(ang, px + Pix.SZ / 2, py + Pix.SZ / 2); xform.translate(px, py); g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); g.drawImage(im, xform, null); } // draw hp bar if (unit.getHp() < unit.maxHP) { double frac = unit.getHp() / unit.maxHP; int hp_sz = 16; AlphaComposite alphaLines = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); g.setComposite(alphaLines); g.setColor(Color.LIGHT_GRAY); g.fillRect((int) x - hp_sz / 2, (int) y + hp_off, hp_sz, 2); g.setColor(Color.GREEN); g.fillRect((int) x - hp_sz / 2, (int) y + hp_off, (int) (hp_sz * frac), 2); } g.setComposite(origComposite); if (showInfo) { double vision = unit.getVision(terra.getTerrain(unit.x, unit.y)); int visRad = (int) (vision * scale); g.drawImage( Pix.imCircle, (int) (unit.x * scale - visRad), (int) (unit.y * scale - visRad), visRad * 2, visRad * 2, null); if (true) { if (unit.getWeapon() != null) { g.setColor(Color.YELLOW); double range = unit.getWeapon().getRange(); int rangeRad = (int) (range * scale); g.drawOval( (int) (unit.x * scale - rangeRad), (int) (unit.y * scale - rangeRad), rangeRad * 2, rangeRad * 2); } g.setComposite(origComposite); } } } }