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

📄 base.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
	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 java.awt.Color;
import java.awt.event.*;
import netwar.game.unit.*;
import netwar.settings.*;
import netwar.Netwar;
import netwar.utils.*;
import netwar.utils.vectorgraphics.*;

/** Large immobile GameObject which creates Units.
 * This object occupies seven hexes, is immobile, and has no weapon.
 * The official victory conditions state that losing your Base means you
 * lose the game, last player with a Base wins.
 * Implementation of that victory condition are being delayed during this
 * phase of development/testing.
 * @author Group N2 - Project Netwar
 * @author Daniel Grund
 */
public class Base extends GameObject {
	private int health;
	private Unit uToMake = null;
	private Unit making[] = new Unit[6];
	private int buildSequence[] = new int[6];
	//zol is the base itself.
	//concrete is the global concrete model
	//vcgt is the virtual copy of the concrete
	//cps is the set of points for vcgt
	//doors abd halls are for the animation
	//basePoints is for building the main base model
	private ZOrderedList zol = null;
	private static ZOrderedList concrete = null;
	private static CoherentPointSet cps = null;
	private GraphicThing vcgt = null;
	private CoherentPointSet doors[] = new CoherentPointSet[6];
	private CoherentPointSet halls[] = new CoherentPointSet[6];
	private static Point3D basePoints[] = new Point3D[25];
	private static Point3D temp = new Point3D();
        final private int MAXHEALTH = 1000;
        final private String NAME = "HeadQuarters";
        final private int ARMOR = 5;
	
       /** This constructor is and should only be used by a Base object that is to
         *  be used in comparing. Hex's getMinimapColor() uses a Base object created
         *  with this constuctor to see if the occupant of that hes is a Base.
         */
        public Base() {}
	/** Reserves the region occupied by the base. The hex coordinates passed,
	 * (X,Y), are the bottom center hex of the 7-hex region occupied by the base.
	 */
	public Base(int X, int Y) {
		x = X;
		y = Y;
		Hex.getHex(X,Y).reserve();
		Hex.getHex(X-1,Y).reserve();
		Hex.getHex(X-1,Y+1).reserve();
		Hex.getHex(X,Y+1).reserve();
		Hex.getHex(X,Y+2).reserve();
		Hex.getHex(X+1,Y+1).reserve();
		Hex.getHex(X+1,Y+2).reserve();
		Hex.getHex(X,Y).enter(this);
		Hex.getHex(X-1,Y).enter(this);
		Hex.getHex(X-1,Y+1).enter(this);
		Hex.getHex(X,Y+1).enter(this);
		Hex.getHex(X,Y+2).enter(this);
		Hex.getHex(X+1,Y+1).enter(this);
		Hex.getHex(X+1,Y+2).enter(this);
		
		health = 1000;
	}
	/** If the base is not already trying to make a Unit, this will cause it to
	 * begin attempting to make a unit. A Unit currently takes one frame of the
	 * Base's time to make, unless the Base has no available locations to build the
	 * Unit.
	 */
	public void spawnUnit(int uNumber, int numberOfPlayerWhoBuiltThisUnit) {
		if(uToMake != null)
			return;
                try {
                    UnitSet us = GameSettings.currentSettings.players[numberOfPlayerWhoBuiltThisUnit-1].unitSet;
                    uToMake = (Unit)(Class.forName(us.getUnitClassNames()[uNumber]).newInstance());
                } catch(Exception e) {
                    System.out.println("Problem making unit!");
                }

		if(uToMake.cost() > myPlayer.getCash())
			uToMake = null;
		else {
			myPlayer.addToCash(-uToMake.cost());
                        ((netwar.gui.NetwarPanel)netwar.Netwar.netwar.getDataViewer()).unitAdded(numberOfPlayerWhoBuiltThisUnit); // This tells the gui panel that another unit has been made
                }
	}
	//Make a unit in the specified space. If successful, set uToMake to null.
	private void makeUnit(int X, int Y, int F) {
		Hex buildHex = Hex.getHex(X, Y);
		if(buildHex.reserve()) {
			if(buildHex.isEmpty() && buildSequence[F] == 0) {
				buildSequence[F] = 40;
				making[F] = uToMake;
				uToMake = null;
			}else{
				buildHex.unreserve();
			}
		}
	}
	
	//The remaining over-ride the Unit defaults
	/** Perform operations for this time step.
	 * Specifically, perform the Point3D transforms for any/all animations, and
	 * attempt to make a unit if a unit creation was requested.
	 */
	protected void update() {
		if(health <= 0) {
			if(frame > 0)
				animateDie();
			return;
		}
		if(uToMake != null) {
			makeUnit(x, y-1, 4);
			if(uToMake != null) {
				makeUnit(x+2, y+1, 5);
				if(uToMake != null) {
					makeUnit(x+2, y+3, 0);
					if(uToMake != null) {
						makeUnit(x, y+3, 1);
						if(uToMake != null) {
							makeUnit(x-2, y+1, 2);
							if(uToMake != null) {
								makeUnit(x-2, y-1, 3);
							}
						}
					}
				}
			}
		}
		for(int i = 0; i < 6; i++) {
			if(buildSequence[i] > 0) {
				if(buildSequence[i] > 30) {
					temp.set(2 * Trig.cos(30 + i * 60),2 * Trig.sin(30 + i * 60),0);
					halls[i].translate(temp);
					doors[i].translate(temp);
				}else if(buildSequence[i] > 20) {
					temp = doors[i].getPoint(2).getDifference(doors[i].getPoint(5));
					doors[i].rotate(doors[i].getPoint(2), temp, 351);
				}else if(buildSequence[i] > 10) {
					temp.set(-2 * Trig.cos(30 + i * 60),-2 * Trig.sin(30 + i * 60),0);
					halls[i].translate(temp);
					doors[i].translate(temp);
				}else{
					temp = doors[i].getPoint(2).getDifference(doors[i].getPoint(5));
					doors[i].rotate(doors[i].getPoint(2), temp, 9);
				}
				 ZListNode zln;
				 int n=0;
				 for(zln = zol.zNode; zln != null; zln = zln.getNext()) {
					n++;
				 }
				 ZListNode zlna[] = new ZListNode[n];
				 n=0;
				 for(zln = zol.zNode; zln != null; zln = zln.getNext()) {
					 zlna[n++] = zln;
				 }
				 for(n=0;n<zlna.length;n++) {
					 zlna[n].getThing().update();
				 }
				buildSequence[i]--;
				if(buildSequence[i] == 30) {
					Unit.newGameObject(making[i],
					x + ((i == 0 || i == 5) ? (2) : ((i == 2 || i == 3) ? (-2) : (0))),
					y + ((i == 0 || i == 1) ? (3) : ((i == 3 || i == 4) ? (-1) : (1))),
					i, myPlayer);
					making[i].setGoal(
					x + ((i == 0 || i == 5) ? (4) : ((i == 2 || i == 3) ? (-4) : (0))),
					y + ((i == 0 || i == 1) ? (5) : ((i == 3 || i == 4) ? (-3) : (1))));
					making[i] = null;
				}
			}
		}
		
	}
	/** Initialize the Point3Ds used for this Base.
	 * These are the location of the ground-center of the Base in game-space, and
	 * the relative locations of the triangle/line vertices relative to that point.
	 */
	protected void createVectors() {
		vr = new Point3D[1];
		
		vr[0] = Hex.getMapPoint(x,y + 1);
		
		if(cps == null) {
			//Make static parts.
			cps = new CoherentPointSet(25);
			cps.add(new SelfConvertingPoint(0,0,0));
			
			cps.add(new SelfConvertingPoint(HexType.m_topLeft.x * 2 - HexType.m_lowerLeft.x, HexType.m_topLeft.y * 2 - HexType.m_lowerLeft.y, 0));
			cps.add(new SelfConvertingPoint(HexType.m_topLeft.x * 3 - HexType.m_lowerLeft.x * 2, HexType.m_topLeft.y * 3 - HexType.m_lowerLeft.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_topRight.x * 3 - HexType.m_lowerRight.x * 2, HexType.m_topRight.y * 3 - HexType.m_lowerRight.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_topRight.x * 2 - HexType.m_lowerRight.x, HexType.m_topRight.y * 2 - HexType.m_lowerRight.y, 0));
			
			cps.add(new SelfConvertingPoint(HexType.m_topRight.x * 2 - HexType.m_middleLeft.x, HexType.m_topRight.y * 2 - HexType.m_middleLeft.y, 0));
			cps.add(new SelfConvertingPoint(HexType.m_topRight.x * 3 - HexType.m_middleLeft.x * 2, HexType.m_topRight.y * 3 - HexType.m_middleLeft.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_middleRight.x * 3 - HexType.m_lowerLeft.x * 2, HexType.m_middleRight.y * 3 - HexType.m_lowerLeft.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_middleRight.x * 2 - HexType.m_lowerLeft.x, HexType.m_middleRight.y * 2 - HexType.m_lowerLeft.y, 0));
			
			cps.add(new SelfConvertingPoint(HexType.m_middleRight.x * 2 - HexType.m_topLeft.x, HexType.m_middleRight.y * 2 - HexType.m_topLeft.y, 0));
			cps.add(new SelfConvertingPoint(HexType.m_middleRight.x * 3 - HexType.m_topLeft.x * 2, HexType.m_middleRight.y * 3 - HexType.m_topLeft.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_lowerRight.x * 3 - HexType.m_middleLeft.x * 2, HexType.m_lowerRight.y * 3 - HexType.m_middleLeft.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_lowerRight.x * 2 - HexType.m_middleLeft.x, HexType.m_lowerRight.y * 2 - HexType.m_middleLeft.y, 0));
			
			cps.add(new SelfConvertingPoint(HexType.m_lowerRight.x * 2 - HexType.m_topRight.x, HexType.m_lowerRight.y * 2 - HexType.m_topRight.y, 0));
			cps.add(new SelfConvertingPoint(HexType.m_lowerRight.x * 3 - HexType.m_topRight.x * 2, HexType.m_lowerRight.y * 3 - HexType.m_topRight.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_lowerLeft.x * 3 - HexType.m_topLeft.x * 2, HexType.m_lowerLeft.y * 3 - HexType.m_topLeft.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_lowerLeft.x * 2 - HexType.m_topLeft.x, HexType.m_lowerLeft.y * 2 - HexType.m_topLeft.y, 0));
			
			cps.add(new SelfConvertingPoint(HexType.m_lowerLeft.x * 2 - HexType.m_middleRight.x, HexType.m_lowerLeft.y * 2 - HexType.m_middleRight.y, 0));
			cps.add(new SelfConvertingPoint(HexType.m_lowerLeft.x * 3 - HexType.m_middleRight.x * 2, HexType.m_lowerLeft.y * 3 - HexType.m_middleRight.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_middleLeft.x * 3 - HexType.m_topRight.x * 2, HexType.m_middleLeft.y * 3 - HexType.m_topRight.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_middleLeft.x * 2 - HexType.m_topRight.x, HexType.m_middleLeft.y * 2 - HexType.m_topRight.y, 0));
			
			cps.add(new SelfConvertingPoint(HexType.m_middleLeft.x * 2 - HexType.m_lowerRight.x, HexType.m_middleLeft.y * 2 - HexType.m_lowerRight.y, 0));
			cps.add(new SelfConvertingPoint(HexType.m_middleLeft.x * 3 - HexType.m_lowerRight.x * 2, HexType.m_middleLeft.y * 3 - HexType.m_lowerRight.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_topLeft.x * 3 - HexType.m_middleRight.x * 2, HexType.m_topLeft.y * 3 - HexType.m_middleRight.y * 2, 0));
			cps.add(new SelfConvertingPoint(HexType.m_topLeft.x * 2 - HexType.m_middleRight.x, HexType.m_topLeft.y * 2 - HexType.m_middleRight.y, 0));
			
			concrete = new ZOrderedList(netwar.gui.HexViewer.getHexViewer().getTransform());
			
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(1), cps.getPoint(4), Color.lightGray));
			concrete.add(new GraphicParallelogram( cps.getPoint(1), cps.getPoint(2), cps.getPoint(3), cps.getPoint(4), Color.lightGray));
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(4), cps.getPoint(5), Color.lightGray));
			
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(5), cps.getPoint(8), Color.lightGray));
			concrete.add(new GraphicParallelogram( cps.getPoint(5), cps.getPoint(6), cps.getPoint(7), cps.getPoint(8), Color.lightGray));
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(8), cps.getPoint(9), Color.lightGray));
			
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(9), cps.getPoint(12), Color.lightGray));
			concrete.add(new GraphicParallelogram( cps.getPoint(9), cps.getPoint(10), cps.getPoint(11), cps.getPoint(12), Color.lightGray));
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(12), cps.getPoint(13), Color.lightGray));
			
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(13), cps.getPoint(16), Color.lightGray));
			concrete.add(new GraphicParallelogram( cps.getPoint(13), cps.getPoint(14), cps.getPoint(15), cps.getPoint(16), Color.lightGray));
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(16), cps.getPoint(17), Color.lightGray));
			
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(17), cps.getPoint(20), Color.lightGray));
			concrete.add(new GraphicParallelogram( cps.getPoint(17), cps.getPoint(18), cps.getPoint(19), cps.getPoint(20), Color.lightGray));
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(20), cps.getPoint(21), Color.lightGray));
			
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(21), cps.getPoint(24), Color.lightGray));
			concrete.add(new GraphicParallelogram( cps.getPoint(21), cps.getPoint(22), cps.getPoint(23), cps.getPoint(24), Color.lightGray));
			concrete.add(new GraphicTriangle( cps.getPoint(0), cps.getPoint(24), cps.getPoint(1), Color.lightGray));
			
			basePoints[0] = new Point3D(0,0,24);
			basePoints[1] = new Point3D(0,0,20);
			basePoints[1].doSum(HexType.m_middleRight);
			basePoints[7] = HexType.m_middleRight.getDifference(HexType.m_lowerLeft);
			basePoints[7].doProduct(0.5f);
			basePoints[7].doSum(HexType.m_middleRight);
			basePoints[13] = HexType.m_middleRight.getDifference(HexType.m_topLeft);
			basePoints[13].doProduct(0.5f);
			basePoints[13].doSum(HexType.m_middleRight);
			basePoints[19] = new Point3D(HexType.m_middleRight);
			for(int i = 1; i < 6; i++) {
				basePoints[1 + i] = basePoints[1].getRotate(Point3D.origin, Point3D.unitUp, 60 * i);
				basePoints[7 + i] = basePoints[7].getRotate(Point3D.origin, Point3D.unitUp, 60 * i);
				basePoints[13 + i] = basePoints[13].getRotate(Point3D.origin, Point3D.unitUp, 60 * i);
				basePoints[19 + i] = basePoints[19].getRotate(Point3D.origin, Point3D.unitUp, 60 * i);
			}
		}
		vcgt = new SpoofZ(new VirtualCoherentGraphicThing(concrete, cps, vr[0], 0), -1 * Float.MAX_VALUE);

⌨️ 快捷键说明

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