Package Hexel.rendering.overlays

Source Code of Hexel.rendering.overlays.HUD

package Hexel.rendering.overlays;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Set;

import Hexel.Engine;
import Hexel.LogData;
import Hexel.blocks.types.Block;
import Hexel.rendering.TextureAtlas;
import Hexel.things.types.Player;

public class HUD {

  private Player player;

  public boolean displayDebug = false;

  private int w;
  private int h;

  private BufferedImage tex;
  private Engine engine;

  public HUD(Engine engine) {
    this.tex = TextureAtlas.getAtlas();
    this.engine = engine;
  }

  public void setPlayer(Player player) {
    this.player = player;
  }

  public void updateSize(int w, int h) {
    this.w = w;
    this.h = h;
  }

  private Color UNDERWATER_COLOR = new Color(0.0f, 0.0f, 1.0f, .5f);

  private Color CROSSHAIR_COLOR = new Color(1.0f, 1.0f, 1.0f, .1f);
  private Stroke CROSSHAIR_STROKE = new BasicStroke(3);

  private Color BAR_COLOR = new Color(0.8f, 0.8f, 0.8f, 1f);
  private Stroke BAR_STROKE = new BasicStroke(1);
 
  private Color HEALTH_COLOR = new Color(1.0f, 0.0f, 0.0f, 1f);
  private Color HUNGER_COLOR = new Color(221/255.0f, 182/255.0f, 1/255.0f, 1f);
 

  private Color BLOCK_AVAILABILITY_COLOR = new Color(1.0f, 1.0f, 1.0f, .5f);

  public void draw(Graphics2D page){
    drawUnderwaterOverlay(page);
    drawCrosshairs(page);
    drawBar(page, this.w/3-this.w/16 , HEALTH_COLOR, this.player.health);
    drawBar(page, this.w*2/3-this.w/16, HUNGER_COLOR, this.player.hunger);
    drawCurrentBlock(page);
    if (displayDebug)
      drawDebug(page);
  }

  private void drawUnderwaterOverlay(Graphics2D page) {
    if (!this.player.isUnderwater())
      return;
    page.setColor(this.UNDERWATER_COLOR);
    page.fillRect(0, 0, this.w, this.h);
  }

  private void drawCrosshairs(Graphics2D page) {
    int cs = Math.min(this.w / 24, this.h / 24);
    int ch = cs * 3 / 4;

    page.setColor(this.CROSSHAIR_COLOR);
    page.setStroke(this.CROSSHAIR_STROKE);
    page.drawLine(this.w / 2 - cs / 2, this.h / 2, this.w / 2 + cs / 2, this.h / 2);
    page.drawLine(this.w / 2, this.h / 2 - ch / 2, this.w / 2, this.h / 2 + ch / 2);
  }

  private void drawBar(Graphics2D page, int startX, Color color, double value){
    if (value < 0)
      value = 0;
    page.setStroke(this.BAR_STROKE);
    page.setColor(this.BAR_COLOR);
    int width = this.w/8;
    page.fillRect(startX, this.h-20, width, 6);
    page.setColor(color);
    page.fillRect(startX, this.h-20, (int)(width*value), 6);
  }

  private void drawCurrentBlock(Graphics2D page) {
    Block b = this.player.getBlockToPlace();
    if (b == null)
      return;
    int availability = this.player.getBlockAvailability(b.getClass());

    int i = b.getTopTextureIndex();
    int texW = this.tex.getWidth() / TextureAtlas.HOR;
    int texH = this.tex.getHeight() / TextureAtlas.VER;
    int texX = i % TextureAtlas.HOR * texW;
    int texY = i / TextureAtlas.HOR * texH;

    BufferedImage subtex = this.tex.getSubimage(texX, texY, texW, texH);

    AffineTransform at = new AffineTransform();
    int iw = Math.max(200, this.h / 5);
    int ih = Math.max(200, this.h / 5);
    at.translate(0, this.h - ih / 2);
    at.scale(iw * 1.0 / subtex.getWidth(), ih * 1.0 / subtex.getHeight());
    at.rotate(Math.PI / 4);
    page.drawImage(subtex, at, null);

    Font font = new Font("Arial", Font.PLAIN, 64);
    page.setFont(font);
    page.setColor(this.BLOCK_AVAILABILITY_COLOR);
    page.drawString("" + availability, texH / 2, this.h - texH / 2);
  }

  private void drawDebug(Graphics2D page){
    Font font = new Font("Arial", Font.PLAIN, 16);
    page.setFont(font);
    page.setColor(Color.white);

//    final String[] keys = {
//        "chunksToBuffer",
//        "chunksToLoad"
//    };

    int i = 0;
    Set<Entry<String, Object>> keys = LogData.getAll();
    for (Entry<String, Object> entry : keys){
      i++;
      page.drawString(entry.getKey() + ": " + entry.getValue(), 0, i*20);
    }
  }

TOP

Related Classes of Hexel.rendering.overlays.HUD

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.