import java.awt.AlphaComposite; import java.awt.Composite; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.ArrayList; public class WorldScene extends Scene { private ArrayList actors = new ArrayList(); private Map map; private int tileSize; public WorldScene(Dimension gridSize, int tileSize) { this.tileSize = tileSize; map = new Map(gridSize); // TODO remove hardcoded stuff Actor player = new Actor("pipo", 100, Actor.Type.PLAYER, gridSize); this.actors.add(player); } private Composite makeAlpha(Graphics2D g2d, float alpha) { Composite originalComposite = g2d.getComposite(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); return originalComposite; } @Override public void render(Graphics g) { Graphics2D g2d = (Graphics2D) g; // draw tiles for (Tile tile : this.map.grid) { switch (tile.type) { case GRASS: g2d.setColor(Palette.GREEN); break; case WATER: g2d.setColor(Palette.BLUE); break; } g2d.fillRect( this.tileSize * tile.x, this.tileSize * tile.y, this.tileSize, this.tileSize ); Composite originalComposite = makeAlpha(g2d, .5f); if (tile.selected) { g2d.setColor(Palette.RED); g2d.fillRect( this.tileSize * tile.x, this.tileSize * tile.y, this.tileSize, this.tileSize ); } // draw grid (with composite) g2d.setPaint(Palette.BLACK); g2d.drawRect( this.tileSize * tile.x, this.tileSize * tile.y, this.tileSize, this.tileSize ); g2d.setComposite(originalComposite); } // draw actors for (Actor actor : this.actors) { switch (actor.type) { case PLAYER: if (actor.isAlive()) { g2d.setColor(Palette.ORANGE); } else { g2d.setColor(Palette.BLUE); } break; case ENEMY: g2d.setColor(Palette.RED); break; } int gap = this.tileSize / 10; g2d.fillRect( (this.tileSize * actor.getX()) + gap, (this.tileSize * actor.getY()) + gap, this.tileSize - gap * 2, this.tileSize - gap * 2 ); } } @Override public void mouseClicked(int x, int y) { this.updateTiles(); Tile tile = map.getTile( (int) (x/(double)this.tileSize) + sceneXOffset, (int) (y/(double)this.tileSize) + sceneYOffset ); //TODO remove test stuff if (tile.getActor() != null) { Actor actor = tile.getActor(); actor.damage(1000); } } public void updateTiles() { for (Tile tile : this.map.grid) { tile.clearActor(); } for (Actor actor : this.actors) { Tile tile = map.getTile(actor.getX(), actor.getY()); tile.setActor(actor); } } }