defaulthud.java.svn-base

来自「一个JAVA程序员的游戏」· SVN-BASE 代码 · 共 264 行

SVN-BASE
264
字号
/*
 * Hud.java
 *
 * Created on 16. Dezember 2006, 19:07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kanjitori.graphics.hud;

import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;

/**
 *
 * @author Pirx
 */
public class DefaultHud extends AbstractHud {

    private final static Image TELEPORT_IMAGE = new ImageIcon("data/hud_tele.png").getImage();
    private final static Image CHANGE_IMAGE = new ImageIcon("data/hud_change.png").getImage();
    private final static Font FONT = new Font("Arial Unicode MS", Font.PLAIN, 25);
    
    private Quad defQuad;
    private Quad healthQuad;
    private Quad botCountQuad;
    private Quad itemTeleQuad;
    private Quad itemChangeQuad;
    private Quad timeCountQuad;

    private String definition;
    private String solution;
    
    /** Creates a new instance of Hud */
    public DefaultHud(Node rootNode) {
        
        node = new Node("HUD");
        
        int width = DisplaySystem.getDisplaySystem().getWidth();
        int height = DisplaySystem.getDisplaySystem().getHeight();
        
        Rectangle defPos = new Rectangle(0, 65, 1024, 32);
        node.attachChild(defQuad = getQuad("def", defPos));
        
        Rectangle healthPos = new Rectangle(20, height - 20, 32, 128);
        node.attachChild(healthQuad = getQuad("health", healthPos));

        Rectangle timeCountPos = new Rectangle(70, height - 20, 200, 32);
        node.attachChild(timeCountQuad = getQuad("timeCount", timeCountPos));
        
        Rectangle botCountPos = new Rectangle(70, height - 60, 100, 32);
        node.attachChild(botCountQuad = getQuad("botCount", botCountPos));

        Rectangle itemTelePos = new Rectangle(width - 150, height - 20, 128, 32);
        node.attachChild(itemTeleQuad = getQuad("teleCount", itemTelePos));

        Rectangle itemChangePos = new Rectangle(width - 150, height - 60, 128, 32);
        node.attachChild(itemChangeQuad = getQuad("teleCount", itemChangePos));
        
        rootNode.attachChild(node);
 
        node.addController(new Controller() {
            private float time = 0;
            private int seconds = 0;
            public void update(float f) {
                float currentTime = time + f;
                int currentSec = (int) FastMath.floor(currentTime);
                if (seconds < currentSec) {
                    seconds = currentSec;
                    showTimeCount(seconds);
                }
                time = currentTime;
            }
        });    
    }
    
    private Quad getQuad(String name, Rectangle pos) {
        Quad quad = new Quad(name, pos.width, -pos.height); //else strings are flipped 
        quad.setLocalTranslation(new Vector3f(pos.x + pos.width/2, pos.y - pos.height/2, 0));
        quad.setLightCombineMode(LightState.OFF);
        quad.setRenderQueueMode(display().getRenderer().QUEUE_ORTHO);
        quad.setRenderState(getTransparence());
        quad.setRenderState(getZState());
        quad.updateRenderState();
        return quad;
    }
    
    private void setBufferedImage(BufferedImage bi, Quad quad) {
        TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        Texture t = TextureManager.loadTexture(bi, Texture.MM_LINEAR, Texture.MM_LINEAR, 1, false);
        ts.setTexture(t);
        
        TextureState oldTs = (TextureState)quad.getRenderState(RenderState.RS_TEXTURE);
        
        quad.setRenderState(ts);
        quad.updateRenderState();

        if(oldTs != null) {
            oldTs.deleteAll(true);
        }

    }
    
    public void showDefinition(String definition) {
        this.definition = definition;
        this.solution = "";
        int dx = 20;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(1024, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        int width = bg.getFontMetrics().stringWidth(definition);
        bg.setColor(new Color(50,80,50,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.ORANGE);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        bg.drawString(definition, dx, dy);
        bg.dispose(); 
        
        setBufferedImage(bi, defQuad);
    }
    
    public void addSolution(String letter) {
        solution = solution + letter;
        int dx = 20;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(1024, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        int width = bg.getFontMetrics().stringWidth(definition + " - " + solution);
        bg.setColor(new Color(50,80,50,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        bg.setColor(Color.ORANGE);
        bg.drawString(definition + " - ", dx, dy);
        int start = bg.getFontMetrics().stringWidth(definition + " - " );
        bg.setColor(Color.CYAN);
        bg.drawString(solution, dx + start, dy);
        bg.dispose(); 
        
        setBufferedImage(bi, defQuad);
    }    

    public void showBotCount(int num) {
        int dx = 10;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(100, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        String botStr = "# " + num;
        int width = bg.getFontMetrics().stringWidth(botStr);
        bg.setColor(new Color(50,50,80,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.ORANGE);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        bg.drawString(botStr, dx, dy);
        bg.dispose();
        
        setBufferedImage(bi, botCountQuad);
    }
    
    private void showTimeCount(int time) {
        int dx = 10;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(200, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        String timeString = String.format("%02d:%02d",time / 60, time % 60);
        int width = bg.getFontMetrics().stringWidth(timeString);
        bg.setColor(new Color(50,50,80,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.ORANGE);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        bg.drawString(timeString, dx, dy);
        bg.dispose();
        
        setBufferedImage(bi, timeCountQuad);
    }    
    
    
    public void showHealth(int health) {
        BufferedImage bi = new BufferedImage(32, 128, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setColor(new Color(255,0,0,130));
        bg.fillRoundRect(1, 101 - health, 30, health, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(1, 1, 30, 100, 5, 5);
        bg.dispose();

        setBufferedImage(bi, healthQuad);
    }

    public void showTeleporters(int teleporter) {
        showItem(TELEPORT_IMAGE, itemTeleQuad, teleporter);
    }
    
    public void showChange(int defChanges) {
        showItem(CHANGE_IMAGE, itemChangeQuad, defChanges);
    }
    
    private void showItem(Image image, Quad quad, int count) {
        BufferedImage bi = new BufferedImage(128, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setColor(new Color(80,80,80,130));
        bg.fillRoundRect(5, 1, 121, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(5, 1, 121, 30, 5, 5);
        for (int i = 0; i < count; i++) {
            bg.drawImage(image, 90 - 40*i, 0, 32, 32, null);
        }
        bg.dispose();

        setBufferedImage(bi, quad);
    }

  
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?