⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hextypem.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
字号:
/*
	Netwar
	Copyright (C) 2002  Daniel Grund, Kyle Kakligian, Jason Komutrattananon, & Brian Hibler.
 
	This file is part of Netwar.
 
	Netwar is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
 
	Netwar is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
 
	You should have received a copy of the GNU General Public License
	along with Netwar; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package netwar.mapper;
import netwar.utils.*;
import netwar.utils.vectorgraphics.*;
import java.awt.*;

/** The <CODE>HexType</CODE> class contains every bit of information that is needed to describe a whole set of similar tiles. This structure does not contain specific information such as whether or not a unit is present. (etc.) It does, however, hold methods and fields related to all similar tiles. As an example, a single <CODE>HexType</CODE> could describe all the trees in a given map; this one class would take care of drawing all of the tree tiles as well as making them all impassable.
 * @author Kyle Kakligian
 */
public class HexTypeM {
	private static final int ZOOMS = 6; // the number of different bmp sizes
	/** Contains the common dimensions of all the tiles.
	 */
	public static Point2D Resolutions[];
	//passable. If false, no units may enter.
	private boolean passable;
        static double sqrt3 = Math.sqrt(3);
	static Point3D m_topRight, m_topLeft, m_middleLeft, m_lowerLeft, m_middleRight, m_lowerRight;
	
	/** Holds images for each zoom level. This is done for speed efficiency, as scaling each tile is expensive.
	 */
	Image bmp[] = new Image[ZOOMS];
	/** The color of this tile for the world map viewer.
	 */
	Color color;
	
	/** Creates a new instance of HexType
	 * @param fileName A string containing the file name of the hex's image reletive to the /media/ directory in the JAR file.
	 */
	public HexTypeM(String fileName) {
		init(fileName, Color.black, true);
	}
	
	/** Creates a new instance of HexType
	 * @param fileName A string containing the file name of the hex's image reletive to the /media/ directory in the JAR file.
	 * @param worldMapColor The color of this tile for the world map viewer.
	 */
	public HexTypeM(String fileName, Color worldMapColor) {
		init(fileName, worldMapColor, true);
	}
	//Constructor for a Hextype which may not be passable.
	/** Creates a new instance of HexType
	 * @param fileName A string containing the file name of the hex's image reletive to the /media/ directory in the JAR file.
	 * @param worldMapColor The color of this tile for the world map viewer.
	 * @param Passable true if this tile type can contain a unit, false otherwise.
	 */
	public HexTypeM(String fileName, Color worldMapColor, boolean Passable) {
		init(fileName, worldMapColor, Passable);
	}
	//Istaran added boolean Passable to set passable
	private void init(String f, Color c, boolean Passable) {
		if(m_lowerLeft == null) {
			m_middleRight = new Point3D(Trig.cos(0) * 20 / sqrt3, Trig.sin(0) * 20 / sqrt3, 0);
			m_topRight = new Point3D(Trig.cos(60) * 20 / sqrt3, Trig.sin(60) * 20 / sqrt3, 0);
			m_topLeft = new Point3D(Trig.cos(120) * 20 / sqrt3, Trig.sin(120) * 20 / sqrt3, 0);
			m_middleLeft = new Point3D(Trig.cos(180) * 20 / sqrt3, Trig.sin(180) * 20 / sqrt3, 0);
			m_lowerLeft = new Point3D(Trig.cos(240) * 20 / sqrt3, Trig.sin(240) * 20 / sqrt3, 0);
			m_lowerRight = new Point3D(Trig.cos(300) * 20 / sqrt3, Trig.sin(300) * 20 / sqrt3, 0);
		}
		passable = Passable;
		bmp[0] = LoadImage.LoadWaitForImage(f);
		Assert.notNull(bmp[0], "Image loading error in HexType. (bad tile filename?)");
		color = c;
		makeRes();
	}
	private void makeRes() {
		if(Resolutions == null ) {
			Resolutions = new Point2D[ZOOMS];
			Resolutions[0] = new Point2D(bmp[0].getWidth(null),bmp[0].getHeight(null));
			for(int t = 1; t<ZOOMS; t++)
				Resolutions[t] = new Point2D(.5f * Resolutions[t-1].x,.5f * Resolutions[t-1].y);
		}
		// create a compatible image for this monitor:
		GraphicsConfiguration gconfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		
		for(int t = 1; t<ZOOMS; t++) {
			Assert.notFalse(Resolutions[t].x == .5f * Resolutions[t-1].x, "At least one tile is of different dimensions than the others.");
			Assert.notFalse(Resolutions[t].y == .5f * Resolutions[t-1].y, "At least one tile is of different dimensions than the others.");
			
			// create the new bmp, make it transparent (so that the copied transparency still works
			bmp[t] = gconfig.createCompatibleImage(Resolutions[t].getIntx(),Resolutions[t].getInty(),Transparency.BITMASK);
			Graphics2D g2 = (Graphics2D)(bmp[t].getGraphics());
			
			// copy the image and scale it down
			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);  // the nearest neighbor method is a must or no transparency. Why the hell don't the others scale the alpha?
			g2.drawImage(bmp[0],0,0,Resolutions[t].getIntx(),Resolutions[t].getInty(),HexViewerM.getHexViewer());
		}
	}
	/** Draws the HexType onto the backgound layer.
	 * @param v What to draw the tile on.
	 * @param pt Where to draw the tile. (in game space)
	 */
        private int x[] = new int[6], y[] = new int[6];
        private Point3D temp2 = new Point3D();
	public void draw(Graphics2D g, Point3D p3) {
            HexViewerM v = HexViewerM.getHexViewer();
            if(v.drawpass || v.drawcolor) {
                if(v.drawpass && passable) v.setColor(Color.green);
                else if(v.drawpass) v.setColor(Color.red);
                else v.setColor(color);
                
		Point2D temp1;
                
                temp2.set(p3);
	        temp1 = v.getTransform().getPoint2D(temp2.doSum(m_topRight));
		x[0] = temp1.getIntx();y[0] = temp1.getInty();
                temp2.set(p3);
	        temp1 = v.getTransform().getPoint2D(temp2.doSum(m_topLeft));
		x[1] = temp1.getIntx();y[1] = temp1.getInty();
                temp2.set(p3);
	        temp1 = v.getTransform().getPoint2D(temp2.doSum(m_middleLeft));
		x[2] = temp1.getIntx();y[2] = temp1.getInty();
                temp2.set(p3);
	        temp1 = v.getTransform().getPoint2D(temp2.doSum(m_lowerLeft));
		x[3] = temp1.getIntx();y[3] = temp1.getInty();
                temp2.set(p3);
	        temp1 = v.getTransform().getPoint2D(temp2.doSum(m_lowerRight));
		x[4] = temp1.getIntx();y[4] = temp1.getInty();
                temp2.set(p3);
	        temp1 = v.getTransform().getPoint2D(temp2.doSum(m_middleRight));
		x[5] = temp1.getIntx();y[5] = temp1.getInty();
        
		g.fillPolygon(x,y,6);
            } else {
		Point2D p2 = v.getTransform().getPoint2D(p3);
		int x = p2.getIntx();
		int y = p2.getInty();
		x -= bmp[MouseParserM.maxZoom-MouseParserM.getZoomLevel()].getWidth(null) / 2;
		y -= bmp[MouseParserM.maxZoom-MouseParserM.getZoomLevel()].getHeight(null) / 2;
		g.drawImage(bmp[MouseParserM.maxZoom-MouseParserM.getZoomLevel()], x, y, null);
            }
            if(v.drawguilds){
                    v.setColor(Color.yellow);
                    v.drawLine(p3.getSum(m_topRight), p3.getSum(m_topLeft));
                    v.drawLine(p3.getSum(m_topLeft), p3.getSum(m_middleLeft));
                    v.drawLine(p3.getSum(m_middleLeft), p3.getSum(m_lowerLeft));
                    v.drawLine(p3.getSum(m_lowerLeft), p3.getSum(m_lowerRight));
                    v.drawLine(p3.getSum(m_lowerRight), p3.getSum(m_middleRight));
                    v.drawLine(p3.getSum(m_middleRight), p3.getSum(m_topRight));
            }
	}

        /** Gets the color for the world map.
	 * @return Color for the world map.
	 */
	public Color getColor() {
		return color;
	}
	//Returns passable boolean
	/** Gets whether or not the tile is passable.
	 * @return true if passable.
	 */
	public boolean getPassable() {
		return passable;
	}
	
	//Returns default status for Hex with this hextype. Currently only affected by passability.
	/** Returns default status for Hex with this <CODE>HexType</CODE>. Currently only affected by passablility.
	 * @return The flag variable from <CODE>Hex</CODE>.
	 */
	public int getDefaultStatus() {
		if(passable)
			return 0;
		return 3;
	}
}


⌨️ 快捷键说明

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