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

📄 pinetree.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.object;
import netwar.game.*;
import netwar.utils.*;
import netwar.utils.vectorgraphics.*;

/** Experimental Tree object. Destroyable obstacle.
 * @author Group N2 - Project Netwar
 * @author Daniel Grund
 */
public class PineTree extends netwar.game.GameObject {
	//Precalculated Point3D values for the tree. These can be done statically, because there is no
	//animation except during death sequence.
	//Note: after actually writing in all these semirandom constants.. I was thinking a little random engine usage could make the trees non-identical..
	//Maybe I will making these on a per-tree basis later. -Istaran
	private static SelfConvertingPoint drawVectorTop = new SelfConvertingPoint(0,0,20);
	private static SelfConvertingPoint drawVectorHigh1 = new SelfConvertingPoint(4.5 * Math.cos(Math.PI * 1f / 9f),4.5 * Math.sin(Math.PI * 1f / 9f),10);
	private static SelfConvertingPoint drawVectorHigh2 = new SelfConvertingPoint(4.6 * Math.cos(Math.PI * 4f / 9f),4.6 * Math.sin(Math.PI * 4f / 9f),10);
	private static SelfConvertingPoint drawVectorHigh3 = new SelfConvertingPoint(4.5 * Math.cos(Math.PI * 7f / 9f),4.5 * Math.sin(Math.PI * 7f / 9f),10.3);
	private static SelfConvertingPoint drawVectorHigh4 = new SelfConvertingPoint(4.5 * Math.cos(Math.PI * 12f / 9f),4.5 * Math.sin(Math.PI * 12f / 9f),10);
	private static SelfConvertingPoint drawVectorHigh5 = new SelfConvertingPoint(4.4 * Math.cos(Math.PI * 16f / 9f),4.4 * Math.sin(Math.PI * 16f / 9f),9.9);
	private static SelfConvertingPoint drawVectorLow1 = new SelfConvertingPoint(6.2 * Math.cos(Math.PI * 2f / 9f),6.2 * Math.sin(Math.PI * 2f / 9f),5.9);
	private static SelfConvertingPoint drawVectorLow2 = new SelfConvertingPoint(6.0 * Math.cos(Math.PI * 4f / 9f),6.0 * Math.sin(Math.PI * 4f / 9f),6);
	private static SelfConvertingPoint drawVectorLow3 = new SelfConvertingPoint(5.9 * Math.cos(Math.PI * 7f / 9f),5.9 * Math.sin(Math.PI * 7f / 9f),6.1);
	private static SelfConvertingPoint drawVectorLow4 = new SelfConvertingPoint(6.0 * Math.cos(Math.PI * 12f / 9f),6.0 * Math.sin(Math.PI * 12f / 9f),6);
	private static SelfConvertingPoint drawVectorLow5 = new SelfConvertingPoint(6.1 * Math.cos(Math.PI * 16f / 9f),6.1 * Math.sin(Math.PI * 16f / 9f),6.2);
	private static SelfConvertingPoint drawVectorGround1 = new SelfConvertingPoint(1.0 * Math.cos(Math.PI * 2f / 5f),1.0 * Math.sin(Math.PI * 2f / 5f),0);
	private static SelfConvertingPoint drawVectorGround2 = new SelfConvertingPoint(1.0 * Math.cos(Math.PI * 4f / 5f),1.0 * Math.sin(Math.PI * 4f / 5f),0);
	private static SelfConvertingPoint drawVectorGround3 = new SelfConvertingPoint(1.0 * Math.cos(Math.PI * 6f / 5f),1.0 * Math.sin(Math.PI * 6f / 5f),0);
	private static SelfConvertingPoint drawVectorGround4 = new SelfConvertingPoint(1.0 * Math.cos(Math.PI * 8f / 5f),1.0 * Math.sin(Math.PI * 8f / 5f),0);
	private static SelfConvertingPoint drawVectorGround5 = new SelfConvertingPoint(1.0 * Math.cos(Math.PI * 10f / 5f),1.0 * Math.sin(Math.PI * 10f / 5f),0);
	private static java.awt.Color trunkBrown = new java.awt.Color(94, 22, 4);
	private static java.awt.Color leafGreen = new java.awt.Color(3, 89, 5);
	
	private static CoherentPointSet points = new CoherentPointSet(16);
	private static GraphicThing appearance= null;
	private VirtualCoherentGraphicThing myAppearance;;
	
	/** Creates a new instance of PineTree */
	public PineTree() {
	}
	
	/** 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);
		
		if(appearance == null) {
			appearance = new OrderedGraphicSet(
			new GraphicTriangle( (drawVectorTop), (drawVectorGround1), (drawVectorGround2), trunkBrown), 15);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorGround2), (drawVectorGround3), trunkBrown), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorGround3), (drawVectorGround4), trunkBrown), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorGround4), (drawVectorGround5), trunkBrown), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorGround5), (drawVectorGround1), trunkBrown), false);
			
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorLow1), (drawVectorLow2), leafGreen), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorLow2), (drawVectorLow3), leafGreen), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorLow3), (drawVectorLow4), leafGreen), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorLow4), (drawVectorLow5), leafGreen), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorLow5), (drawVectorLow1), leafGreen), false);
			
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorHigh1), (drawVectorHigh2), leafGreen.brighter()), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorHigh2), (drawVectorHigh3), leafGreen.brighter()), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorHigh3), (drawVectorHigh4), leafGreen.brighter()), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorHigh4), (drawVectorHigh5), leafGreen.brighter()), false);
			((OrderedGraphicSet)appearance).add(new GraphicTriangle( (drawVectorTop), (drawVectorHigh5), (drawVectorHigh1), leafGreen.brighter()), false);
			
			points.add(drawVectorTop);
			
			points.add(drawVectorHigh1);
			points.add(drawVectorHigh2);
			points.add(drawVectorHigh3);
			points.add(drawVectorHigh4);
			points.add(drawVectorHigh5);
			
			points.add(drawVectorLow1);
			points.add(drawVectorLow2);
			points.add(drawVectorLow3);
			points.add(drawVectorLow4);
			points.add(drawVectorLow5);
			
			points.add(drawVectorGround1);
			points.add(drawVectorGround2);
			points.add(drawVectorGround3);
			points.add(drawVectorGround4);
			points.add(drawVectorGround5);
			
		}
		if(netwar.Mapper.theApp != null) myAppearance = new VirtualCoherentGraphicThing(appearance, points, vr[0], (new java.util.Random()).nextInt(360));
                else myAppearance = new VirtualCoherentGraphicThing(appearance, points, vr[0], netwar.Netwar.netwar.random.nextInt(360));
		
	}
	
	/** Returns false, because the tree has no weapon.
	 * @return false
	 */
	protected boolean aim() {
		return false;
	}
	
	/** Perform the data changes for one frame of animation while dying. */
	protected void animateDie() {
		if(frame == framesToDie()) {
			Hex.getHex(x,y).unreserve();
			Hex.getHex(x,y).leave(this);
			myAppearance.explode(vr[0]);
		}
		frame--;
		if(frame == 0)
			remove();
	}
	
	/** Perform the data changes for one frame of animation while being made.
	 * Pine trees pre-exist: there is no formation animation.
	 */
	protected void animateMake() {
	}
	
	/** Draw this GameObject onto GameViewer v, by using v's drawing methods.
	 * @param v The GameViewer which will display this GameObject.
	 * @see netwar.gui.HexViewer
	 */
	public void draw(GameViewer v) {
		myAppearance.draw(v);
	}
	/** Requests that the GraphicThing(s) used by this object be added to GameViewer v.
	 * @param v The GameViewer which will be displaying the object.
	 */
	public void addTo(GameViewer v) {
		v.add(myAppearance);
	}
	public void killGraphics() {
		myAppearance.kill();
	}
	/** Returns the number of frames of animation for the death sequence.
	 * @return 5
	 */
	protected int framesToDie() {
		return 10;
	}
	
	/** Returns the number of frames of animation for the creation sequence.
	 * @return 0
	 */
	protected int framesToMake() {
		return 0;
	}
	
	/** Return the height of this GameObject for selection box and explosion hit calculations.
	 * @return 20
	 */
	public float getHeight() {
		return 20f;
	}
	
	/** Return the width of this GameObject for selection box and explosion hit calculations.
	 * @return 5
	 */
	public float getWidth() {
		return 6.2f;
	}
	
	/** Clear the current long-term goal. */
	public void setGoal() {
	}
	
	/** Set the current long-term goal to a GameObject.
	 * This is a tree. It does not use the goal system.
	 * @param u The GameObject which is the goal.
	 */
	public void setGoal(GameObject u) {
	}
	
	/** Set the current long-term goal to a location, at hex coordinate (gx, gy).
	 * This is a tree. It does not use the goal system.
	 * @param gx The x coordinate of the goal, in hex coords.
	 * @param gy The y coordinate of the goal, in hex coords.
	 */
	public void setGoal(int gx, int gy) {
	}
	
	/** Perform operations for this time step.
	 * Specifically, perform the Point3D transforms for any/all animations,
	 * fire at an enemy if possible, and do any required decision making.
	 * This is a tree. It is only animated if it is dying.
	 */
	protected void update() {
		if(action == 5)
			animateDie();
	}
	
	/** Return the number of frames to wait between firing shots.
	 * @return 0
	 */
	public int weaponDelay() {
		return 0;
	}
	
	/** Return the square of the maximum weapon range of this GameObject.
	 * @return 0
	 */
	public float weaponRangeSquared() {
		return 0f;
	}
	
	public boolean isDead() {
		return (action == 5);
	}
	public boolean damageable() {
		return (action != 5);
	}
	/** The tree uses the 'threshold damage system'.
	 * If sufficient damage (15 or more) is dealt in one blow,
	 * the tree is destroyed.
	 */
	public void recieveDamage(int dam) {
		if(dam >= 15) {
			action = 5;
			frame = framesToDie();
		}
	}
	
	static final String[] PROPERTIES = {};
	protected String[] PROPERTIES() {
		return PROPERTIES;
	}
	protected String getProperty(int p) {
		switch(p) {
			default:
				Assert.notFalse(false, "getProperty(int) error.");
		}
		return null;
	}
	
	
}

⌨️ 快捷键说明

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