?? hexm.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.*;
import netwar.game.GameObject;
import netwar.game.GameViewer;
import java.io.*;
/** 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 HexM {
/** The square root of three, which is particularly relavent to hexagon geometry. */
public static final double sqrt3 = Math.sqrt(3);
private static HexM board[][];
private static int radius;
private GameObject occupant;
private HexTypeM hexType;
private static HexM baseLocations[] = new HexM[12];
private boolean passable;
public static Vector hexTypes = new LVector();
private static final String IDENT = new String("Map File");
//public static Vector gameObjectList = new LVector();
public static Vector unitTypes = new LVector();
//public static Vector gameObjectLocations = new LVector();
public void addGameObject(GameObject go, int x, int y) {
clear();
go.newGameObjectM(go,x,y,3);
enter(go);
}
/** takes care of removing the contained unit. (if it exists)
*/
public void clear() {
if(occupant != null) {
occupant.remove();
occupant.killGraphics();
}
occupant = null;
}
/** sets the HexType of this Hex
*/
public void setType(HexTypeM t) {
hexType = t;
}
/** 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) {
hexTypes.clear();
//gameObjectList.clear();
unitTypes.clear();
//gameObjectLocations.clear();
ObjectComponent.curr = null;
TileComponent.curr = null;
if(netwar.Mapper.rightPanel != null)
netwar.Mapper.rightPanel.getList().removeAll();
HexTypeM tile1 = new HexTypeM("tiles/template.gif", Color.orange, true);
HexTypeM tile2 = new HexTypeM("tiles/template.gif", Color.blue, false);
hexTypes.append(tile1);
hexTypes.append(tile2);
int i, j;
radius = rad;
board = new HexM[2 * radius - 1][];
for(i = 0; i < radius; i++)
{
board[i] = new HexM[i + radius];
for(j = 0; j < i + radius; j++) {
board[i][j] = new HexM(tile1);
}
}
for(i = radius; i < (2 * radius - 1); i++)
{
board[i] = new HexM[2 * radius - 1];
for(j = i - (radius - 1); j < (2 * radius - 1); j++) {
board[i][j] = new HexM(tile2);
}
}
System.gc();
}
/** Loads a map file.
*/
public static void load() {
}
/** saves a map file.
*/
public static void save() {
FileOutputStream ostream = null;
javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
int returnVal = chooser.showOpenDialog(netwar.Mapper.theApp);
if(returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {try {
ostream = new FileOutputStream(chooser.getSelectedFile().getName());
ObjectOutputStream p = new ObjectOutputStream(ostream);
String types[] = new String[hexTypes.getLength()];
int t = 0;
for(hexTypes.goFirst(); hexTypes.isInList(); hexTypes.goNext()){
HexTypeM ht = (HexTypeM)hexTypes.get();
if(ht != null)
types[t] = ht.getClass().getName();
t++;
}
HexTypeStruct units[] = new HexTypeStruct[unitTypes.getLength()];
t = 0;
for(unitTypes.goFirst(); unitTypes.isInList(); unitTypes.goNext()){
TileComponent tc = (TileComponent)unitTypes.get();
if(tc != null)
units[t] = new HexTypeStruct(tc.fileName, tc.pass, tc.c);
t++;
}
GameObjectLocationStruct locs[] = GameObject.getLocationStruct();
String sgo[] = GameObject.getLocationStructStrings();
for(t = 0; t< sgo.length; t++) {
int i= 0;
while(i < units.length && !units[i].filename.equals(sgo[t])) i++;
locs[t].unitIndex = i;
}
int hexIndexes[][] = new int[2 * radius - 1][];
for(int i = 0; i < radius; i++) {
hexIndexes[i] = new int[i + radius];
for(int j = radius + i - 1; j >= 0; j--) {
int u = 0;
while(u < types.length && !types[u].equals(board[i][j].hexType.getClass().getName())) u++;
hexIndexes[i][j] = u;
}
}
for(int i = radius; i < (2 * radius - 1); i++) {
hexIndexes[i] = new int[2 * radius - 1];
for(int j = 1 - (radius - 1); j < (2 * radius - 1); j++) {
int u = 0;
while(u < types.length && !types[u].equals(board[i][j].hexType.getClass().getName())) u++;
hexIndexes[i][j] = u;
}
}
p.writeObject(IDENT); // String
p.writeInt(radius); // int
p.writeObject(types); // String[]
p.writeObject(units); // HexTypeStruct[]
p.writeObject(locs); // GameObjectLocationStruct[]
p.writeObject(hexIndexes); // int[][]
p.flush();
ostream.close();
} catch(Exception e) {
System.out.println(e.getMessage());
System.out.println(e);
}
}
}
/** 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 HexM 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 HexM(HexTypeM h) {
hexType = h;
passable = h.getPassable();
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++)
{
for(j = radius + i - 1; j >= 0; j--)
{
vm = getMapPoint(i,j);
board[i][j].hexType.draw(g, vm);
}
}
for(i = radius; i < 2 * radius - 1; i++)
{
for(j = 2 * (radius - 1); j >= i - (radius - 1); j--)
{
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)
{
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 HexM 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 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;
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;
}
/** Checks if the Hex is unoccupied.
* @return true if there is no GameObject in the Hex.
*/
public boolean isEmpty() {
return (occupant == null);
}
/** 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() {
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(HexTypeM.m_middleLeft).x * 2),
Math.abs(t.getPoint2D(HexTypeM.m_topLeft).y * 2));
}
/** returns the Hex's HexType
*/
public HexTypeM getHexType() {
return this.hexType;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -