@Override public void render(Graphics g) { if (map.needsToBeRendered() || reRender) { Graphics g1 = cachedViewport.getGraphics(); // Draw a black background g1.setColor(Color.BLACK); g1.fillRect(0, 0, viewportWidth, viewportHeight); // Get the avatar's location. This location will be shown in the center of the viewport. Point logicalPoint = avatar.getLocation(); Point pixelPoint = new Point(viewportWidth / 2, viewportHeight / 2); // Create a 2D graphcis obj Graphics2D g2 = (Graphics2D) g1.create(); // Clear the existing status bar locations this.entityLocationTuples.clear(); breadthFirstRender(logicalPoint, pixelPoint, g2); map.setNeedsToBeRendered(false); reRender = false; g1.dispose(); } // draws the viewport g.drawImage(cachedViewport, 0, 0, viewportWidth, viewportHeight, getDisplay()); for (EntityLocationTuple et : this.entityLocationTuples) { drawEntityHealthBar(g, et); } if (displayDebugInformation) { g.setColor(Color.WHITE); g.drawString( avatar.getLocation().toString(), viewportWidth - g.getFontMetrics().stringWidth(avatar.getLocation().toString()) - 50, 25); } }
private void drawEntityHealthBar(Graphics g, EntityLocationTuple entityLocationHealthTriple) { Point p = entityLocationHealthTriple.point; Entity entity = entityLocationHealthTriple.entity; int oldHealth = entityHealthMap.get(entity).intValue(); int entityX = (int) p.getX() - hexWidth * 3 / 8; int entityY = (int) p.getY() - hexHeight * 3 / 8; Stats stats = entity.getStats(); int entitysCurrentActualHealth = stats.getStat(Stats.Type.HEALTH); // Only render health on NPCs // Also only update health bar, if the entities current health is diff than its old health if (!(entity == avatar)) { if (!entityEffedUpHealthMap.containsKey(entity) || oldHealth != entitysCurrentActualHealth) { System.out.println("CHANGIN ENTITY HEALTH"); Skill observation = avatar.getSpecificSkill(Skill.SkillDictionary.OBSERVATION); ObservationSkill observationSkill = (ObservationSkill) observation; observationSkill.onUpdate(entity); int effedUpHealth = observationSkill.getCombatError(entitysCurrentActualHealth); entityEffedUpHealthMap.put(entity, effedUpHealth); entityHealthMap.put(entity, entitysCurrentActualHealth); } // int health = entityEffedUpHealthMap.get(entity); int health = entitysCurrentActualHealth + entityEffedUpHealthMap.get(entity); int maxHealth = stats.getStat(Stats.Type.MAX_HEALTH); health = health > maxHealth ? maxHealth : health; health = health < 0 ? 0 : health; // Sizes int healthBarWidth = getScreenWidth() / 12; int healthBarHeight = getScreenHeight() / 53; // Set the font Font f = new Font("Courier New", 1, 14); g.setFont(f); // Set the location and size of the health bar. int healthBarX = entityX - healthBarWidth / 3; int healthBarY = entityY - healthBarHeight; // Draw the outline of the health bar. g.setColor(Color.RED); g.fillRect(healthBarX, healthBarY, healthBarWidth, healthBarHeight); // Determine what fraction of the healthbar should be shown. double healthFraction = (double) health / (double) maxHealth; int healthFillWidth = (int) (healthFraction * healthBarWidth); // Fill the healthbar g.setColor(Color.GREEN); g.fillRect(healthBarX, healthBarY, healthFillWidth, healthBarHeight); // Display the fraction of health g.setColor(Color.WHITE); String healthFractionString = "(" + health + "/" + maxHealth + ")"; FontMetrics fm = g.getFontMetrics(f); // Place the font at the right of the bar Rectangle2D healthFractionRect = fm.getStringBounds(healthFractionString, g); int healthFractionX = healthBarX + (healthBarWidth - (int) healthFractionRect.getWidth()) / 2; int healthFractionY = healthBarY + healthBarHeight - 4; g.drawString(healthFractionString, healthFractionX, healthFractionY); } }
// This will traverse through all the tiles using a breadth first search. It will then render that // tile. private void breadthFirstRender(Point logicalPoint, Point pixelPoint, Graphics2D g) { // Get the radius of visibliity. int radiusOfVisibility = avatar.getRadiusOfVisiblility(); // This hashmap will keep track of what we have already renderred in our traversal. HashMap<Point, Boolean> hasBeenRendered = new HashMap<>(); // Create an empty queue of tile nodes. Queue<TileNode> tileQueue = new LinkedList<>(); // Convert the first tile into a TileNode and push it into the queue. TileNode root = new TileNode(map.getTileAt(logicalPoint), logicalPoint, pixelPoint); root.distanceFromAvatar = 0; // Offset the root based upon the offset ammound root.pixelPoint.translate((int) viewportOffset.getX(), (int) viewportOffset.getY()); tileQueue.offer(root); // offer is analogous to push (or enqueue). while (!tileQueue.isEmpty()) { // Pop the current tile off the queue. TileNode currentTileNode = tileQueue.poll(); // poll is analogous to pop (or dequeue). // Check to see if the tile has already been renderred. if ((hasBeenRendered.get(currentTileNode.logicalPoint) == null) || !hasBeenRendered.get(currentTileNode.logicalPoint) && isInRangeOfViewport(currentTileNode.pixelPoint)) { hasBeenRendered.put( currentTileNode.logicalPoint, true); // Mark the tile as having been renderred. // Render the current Tile if (!displayDebugInformation) { if (currentTileNode.distanceFromAvatar < radiusOfVisibility) { // Mark this tile as having been seen. seenTiles.put(new Point(currentTileNode.logicalPoint), new Tile(currentTileNode.tile)); // Set the opacity based on the distance from the avatar. float opacity = 1.0f - (1 - MIN_OPACITY) * (currentTileNode.distanceFromAvatar / (float) radiusOfVisibility); opacity = opacity < MIN_OPACITY ? MIN_OPACITY : opacity; renderTile(currentTileNode, g, opacity); // Render the tile. } else if (seenTiles.get(currentTileNode.logicalPoint) != null) { currentTileNode.tile = seenTiles.get( currentTileNode.logicalPoint); // Switch out the actual tile with the seen tile. renderTile(currentTileNode, g, SEEN_OPACITY); } } else { // Yes, Austin put this in. You can tell by the spacing. renderTile(currentTileNode, g, 1f); } // Push all the adjacent nodes onto the queue for (TileNode tileNode : getAdjacentTiles(currentTileNode)) { tileNode.distanceFromAvatar = currentTileNode.distanceFromAvatar + 1; tileQueue.offer(tileNode); } } } }