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

📄 hex.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.mapper.HexM;
import netwar.game.object.*;

/** This class represents a single hexagon-shaped region of game-space.
 * These regions are used to simplify path-finding algorithms,
 * graphical draw-order, and terrain logic and display.
 * A Hex may contain up to one GameObject at a time.
 * @author Group N2 - Project Netwar
 * @author Daniel Grund, with adjustments by Kyle Kakligian
 */
public class Hex {
	/** The square root of three, which is particularly relavent to hexagon geometry. */
	public static final double sqrt3 = Math.sqrt(3);
	private static Hex board[][];
	private static int radius;
	private GameObject occupant;
	private int status; //Poly-boolean. 1 = occupied flag. 2 = reserved flag.
	private HexType hexType;
	
	/** Used for determining initial base locations.
	 * @param radial a number from 0 to 11 indicating the direction from the center.
	 * @param offset the distance along that radian.
	 * @param fromEdge if true, count the distance (offset) from the edge of the map, instead of the center.
	 * @return the X coordinate of the desired location (in hex coordinates).
	 */
	public static int radialX(int radial, int offset, boolean fromEdge) {
		if(fromEdge) {
			switch (radial) {
				case 4:
				case 5:
				case 6:
					return offset;
				case 3:
				case 7:
					return (radius / 2 + offset / 2);
				case 2:
				case 8:
					return (radius - 1);
				case 1:
				case 9:
					return (3 * (radius - 1) / 2 - offset / 2);
				case 0:
				case 10:
				case 11:
					return (2 * (radius - 1) - offset);
			}
		}else{
			switch (radial) {
				case 4:
				case 5:
				case 6:
					return radius - 1 - offset;
				case 3:
				case 7:
					return (radius - 1 -  (offset + 1) / 2);
				case 2:
				case 8:
					return (radius - 1);
				case 1:
				case 9:
					return ((radius - 1) + (offset + 1) / 2);
				case 0:
				case 10:
				case 11:
					return (radius - 1 + offset);
			}
		}
		return (radius - 1);
	}
	
	/** Used for determining initial base locations.
	 * @param radial a number from 0 to 11 indicating the direction from the center.
	 * @param offset the distance along that radian.
	 * @param fromEdge if true, count the distance (offset) from the edge of the map, instead of the center.
	 * @return the Y coordinate of the desired location (in hex coordinates).
	 */
	public static int radialY(int radial, int offset, boolean fromEdge) {
		if(fromEdge) {
			switch(radial) {
				case 6:
				case 7:
				case 8:
					
					return offset;
				case 5:
				case 9:
					return (radius / 2 + (offset - 1) / 2);
				case 4:
				case 10:
					return (radius - 1);
				case 3:
				case 11:
					return (3 * (radius - 1) / 2 - (offset - 1) / 2);
				case 0:
				case 1:
				case 2:
					return (2 * (radius - 1) - offset);
			}
		}else{
			switch(radial) {
				case 6:
				case 7:
				case 8:
					return radius - 1 - offset;
				case 5:
				case 9:
					return (radius - 1 -  (offset) / 2);
				case 4:
				case 10:
					return (radius - 1);
				case 3:
				case 11:
					return ((radius - 1) + (offset) / 2);
				case 0:
				case 1:
				case 2:
					return (radius - 1 + offset);
			}
		}
		return (radius - 1);
	}
	
	/** Initializes the board, which is a static array of Hex objects,
	 * designed to produce a hexagon of hexagons.
	 * Also initializes the HexType objects, and assigns them to the appropriate Hexes.
	 * @param rad The radius of the board in Hexes.
	 */
	public static void makeBoard(int rad) {
		Color brown = new Color(112,48,0);
                Color aqua = new Color(0,128,255);
		HexType temp[] = {  new HexType("tiles/brown1.gif", brown, true),
		new HexType("tiles/brown2.gif", brown, true),
		new HexType("tiles/brown3.gif", brown, true),
		new HexType("tiles/brown2.gif", brown, true),
		new HexType("tiles/brown1.gif", brown, true),
		new HexType("tiles/brown2.gif", brown, true),
		new HexType("tiles/brown1.gif", brown, true),
                new HexType("tiles/blue1.gif", aqua, false)};
		
		int i, j;
		
		
		radius = rad;
		board = new Hex[2 * radius - 1][];
		for(i = 0; i < radius; i++) {
			board[i] = new Hex[i + radius];
			for(j = 0; j < i + radius; j++) {
				board[i][j] = new Hex(temp[netwar.Netwar.netwar.random.nextInt(8)]); // temp until we can load a map
				if((board[i][j].status == 0) && (i > 7) && (j > 7) && (j < i + radius - 8) && (netwar.Netwar.netwar.random.nextFloat() < .1))
					switch(netwar.Netwar.netwar.random.nextInt(4)) {
						case 0:
							GameObject.newGameObject( new Bush(), i, j, 0, null);
						break;
						case 1:
							GameObject.newGameObject( new BushRed(), i, j, 0, null);
						break;
						case 2:
							GameObject.newGameObject( new Palm(), i, j, 0, null);
						break;
						case 3:
							GameObject.newGameObject( new PineTree(), i, j, 0, null);
						break;
					}
			}
		}
		for(i = radius; i < (2 * radius - 1); i++) {
			board[i] = new Hex[2 * radius - 1];
			for(j = i - (radius - 1); j < (2 * radius - 1); j++) {
				board[i][j] = new Hex(temp[netwar.Netwar.netwar.random.nextInt(8)]); // temp until we can load a map
				if((board[i][j].status == 0) && (i < 2 * radius - 9) && (j - i + radius - 1 > 7) && (j < 2 * radius - 9) && (netwar.Netwar.netwar.random.nextFloat() < .1))
					switch(netwar.Netwar.netwar.random.nextInt(4)) {
						case 0:
							GameObject.newGameObject( new Bush(), i, j, 0, null);
						break;
						case 1:
							GameObject.newGameObject( new BushRed(), i, j, 0, null);
						break;
						case 2:
							GameObject.newGameObject( new Palm(), i, j, 0, null);
						break;
						case 3:
							GameObject.newGameObject( new PineTree(), i, j, 0, null);
						break;
					}
			}
		}
	}
	/** Retrieves a Hex from the board.
	 * @param x The x part of the Hex coordinate of the desired hex.
	 * @param y The y part of the Hex coordinate of the desired hex.
	 * @return The desired Hex, or null if the coordinates are invalid.
	 */
	public static Hex getHex(int x, int y) {
		//Confirm validity
		if(x < 0 || x >= 2 * radius - 1 || y < 0 || y >= 2 * radius - 1 || (x < radius && y >= radius + x))
			return null;
		return board[x][y];
	}
	/** This constructor initializes the Hex according to a HexType's initializing data.
	 * @param h The HexType containing the data for this type of Hex.
	 */
	protected Hex(HexType h) {
		hexType = h;
		status = h.getDefaultStatus();
		occupant = null;
	}
	/** Draws all hexes onto GameViewer v.
	 * @param v A GameViewer seeking to display the entire map (or at least the visible portion).
	 */
	public static void draw(Graphics2D g) {
		int i, j;
		Point3D vm = new Point3D();
		for(i = 0; i < radius; i++) {
			//if(x is within viewable range) {
			for(j = radius + i - 1; j >= 0; j--) {
				//if(y is within viewable range)
				vm = getMapPoint(i,j);
				board[i][j].hexType.draw(g, vm);
			}
			//}
		}
		for(i = radius; i < 2 * radius - 1; i++) {
			//if(x is within viewable range) {
			for(j = 2 * (radius - 1); j >= i - (radius - 1); j--) {
				//if(y is within viewable range)
				vm = getMapPoint(i,j);
				board[i][j].hexType.draw(g, vm);
			}
			//}
		}
	}
	/** Draws all the GameObjects in the game onto GameViewer v.
	 * These are drawn in order from south to north to enable the Z-order to
	 * be preserved.
	 * @param v The GameViewer seeking to display all of the GameObjects.
	 */
	public static void drawGameObject(GameViewer v) {
		int i, j;
		Point3D vm = new Point3D();
		//if(x is within viewable range) {
		for(j = 2 * (radius - 1); j > radius - 1; j--) {
			for(i = j - (radius - 1); i <= 2 * (radius - 1); i++) {
				//if(y is within viewable range)
				if(board[i][j].occupant != null)
					board[i][j].occupant.draw(v);
			}
			//}
		}
		//if(x is within viewable range) {
		for(j = radius - 1; j >= 0; j--) {
			for(i = 0; i < radius + j; i++) {
				//if(y is within viewable range)
				if(board[i][j].occupant != null)
					board[i][j].occupant.draw(v);
			}
			//}
		}
	}
	/** Gets the ground-center of the Hex at hex coordinate (x,y).
	 * @param x The x part of the hex coordinate.
	 * @param y The y part of the hex coordinate.
	 * @return The ground-center of the specified hex.
	 */
	public static Point3D getMapPoint(int x, int y) 	{
		if(radius == 0) // called from Mapper
			return HexM.getMapPoint(x,y);
		return (new Point3D(10 * sqrt3 * (-radius + x), 10 * (2 * y - radius - x + 1), 0));
	}
	/** Gets the Hex containing a game-space point, and alters the parameter
	 * to contain the hex coordinates, by storing the hex x in vr.x and the
	 * hex y in vr.y
	 * @param vr The game-space point to locate, and the storage space for the x and y.
	 * @return The Hex containing the game-space point.
	 */
	public static Hex getXY(Point3D vr) {
		vr.x = Math.round(vr.x/(10 * sqrt3) + radius);
		vr.y = Math.round((vr.y/10 + radius + vr.x - 1)/2);
		return getHex((int)vr.x, (int)vr.y);
	}
	/** Attempts to reserve this Hex for the caller.
	 * @return true if the Hex was reserved for the caller.
	 */
	public boolean reserve() {
		if((status & 2) == 2)
			return false;
		status |= 2;
		return true;
	}
	/** Unreserves this Hex.
	 * This should only be called by something which successfully called reserve().
	 */
	public void unreserve() {
		status -= status & 2;
	}
	/** Attempts to put the GameObject into this Hex.
	 * @param u The GameObject which is attempting to occupy the Hex.
	 * @return True if the GameObject was put into this Hex.
	 */
	public boolean enter(GameObject u) {
		if(occupant != null)
			return false;
		occupant = u;
		status |= 1;
		
		return true;
	}
	/** Removes the GameObject from the Hex, if it is the GameObject which was in the Hex.
	 * @param u The GameObject which is leaving the Hex.
	 */
	public void leave(GameObject u) {
		if(occupant != u)
			return;
		occupant = null;
		status -= status & 1;
	}
	/** Checks if the Hex is unoccupied.
	 * @return true if there is no GameObject in the Hex.
	 */
	public boolean isEmpty() {
		return ((status & 1) == 0);
	}
	/** Returns the GameObject which is in the Hex.
	 * @return the occupant, or null if the Hex is empty.
	 */
	public GameObject getOccupant() {
		return occupant;
	}
	/** Returns the Color to display on the minimap.
	 * @return A Color for use on the Minimap.
	 */
	public Color getMinimapColor() {
		try{
			if(occupant != null && Class.forName("netwar.game.Base").isInstance(occupant))
				return occupant.getMinimapColor();
		} catch(ClassNotFoundException cnfe) {
			System.err.println("Class netwar.game.Base not found. This class is absolutely necessary for Netwar.");
		}
		return hexType.getColor();
	}
	/** Returns the size of a rectangle just big enough to contain one Hex.
	 * This size is stored into a Point2D as x = width, y = height.
	 * @return the Point2D containing the size of the rectangle.
	 */
	public static Point2D getHexDimension() {
		netwar.utils.vectorgraphics.Transform t = netwar.gui.HexViewer.getHexViewer().getTransform();
		return new Point2D(Math.abs(t.getPoint2D(HexType.m_middleLeft).x * 2),
		Math.abs(t.getPoint2D(HexType.m_topLeft).y * 2));
	}
	public static int getRadius() { return radius;}
}

⌨️ 快捷键说明

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