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

📄 hextype.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.game;
import netwar.utils.*;
import netwar.utils.vectorgraphics.*;
import java.awt.*;
import netwar.gui.HexViewer;
import netwar.gui.MouseParser;

/** 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 HexType {
	private static final int ZOOMS = 5; // 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 HexType(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 HexType(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 HexType(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 +1,.5f * Resolutions[t-1].y +1); //*** get rid of these +1's once the tile size is a multiple of 2^4
		}
		// 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 +1, "At least one tile is of different dimensions than the others.");//*** get rid of the +1 once the tile size is a multiple of 2^4
			Assert.notFalse(Resolutions[t].y == .5f * Resolutions[t-1].y +1, "At least one tile is of different dimensions than the others.");//*** get rid of the +1 once the tile size is a multiple of 2^4
			
			// 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 work?
			g2.drawImage(bmp[0],0,0,Resolutions[t].getIntx(),Resolutions[t].getInty(),HexViewer.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)
	 */
	public void draw(Graphics2D g, Point3D p3) {
		Point2D p2 = HexViewer.getHexViewer().getTransform().getPoint2D(p3);
		int x = p2.getIntx();
		int y = p2.getInty();
		x -= bmp[MouseParser.maxZoom-MouseParser.getZoomLevel()].getWidth(null) / 2;
		y -= bmp[MouseParser.maxZoom-MouseParser.getZoomLevel()].getHeight(null) / 2;
		g.drawImage(bmp[MouseParser.maxZoom-MouseParser.getZoomLevel()], x, y, null);
	}
	/** 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 + -