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

📄 mobileartillery.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.unit;
import netwar.utils.*;
import netwar.utils.vectorgraphics.*;
import netwar.game.*;
import netwar.game.projectile.BallisticShell;
import java.awt.*;

/** Temporary Unit class. This will be removed in Iteration 2, when the real Units are created.
 * This is a fast, durable, short range combatant.
 * It has good armor, and uses a rapid fire weapon (which is good against low armor targets).
 * Because of its long range, it has an extended scan range so that it can automatically approach enemies.
 * @author Group N2 - Project Netwar
 * @author Daniel Grund
 */
public class MobileArtillery extends Unit{
	static int firingAngles[] = null;
	static float firingForces[] = null;
	ZOrderedList zol;
	CoherentPointSet cps;
	CoherentPointSet barrel;
	int barrelAngle = 0;
	float forceProduct = 0;
	/** Constructor. This Unit has no special parameters. */
	public MobileArtillery(){
		if(firingAngles == null) {
			//need to adjust for accuracy later.
			firingAngles = new int[11];
			firingAngles[0] = 5;
			firingAngles[1] = 9;
			firingAngles[2] = 13;
			firingAngles[3] = 17;
			firingAngles[4] = 21;
			firingAngles[5] = 25;
			firingAngles[6] = 29;
			firingAngles[7] = 33;
			firingAngles[8] = 37;
			firingAngles[9] = 41;
			firingAngles[10] = 45;
			firingForces = new float[11];
			firingForces[0] = 0;
			firingForces[1] = 0.1f;
			firingForces[2] = 0.3f;
			firingForces[3] = 0.45f;
			firingForces[4] = 0.5f;
			firingForces[5] = 0.6f;
			firingForces[6] = 0.6f;
			firingForces[7] = 0.7f;
			firingForces[8] = 0.7f;
			firingForces[9] = 0.75f;
			firingForces[10] = 0.8f;
		}
             netwar.utils.vectorgraphics.Transform t;
             if(netwar.Mapper.theApp == null) t = netwar.gui.HexViewer.getHexViewer().getTransform();
             else t = netwar.mapper.HexViewerM.getHexViewer().getTransform();
             zol = new ZOrderedList(t);
	     cps = new CoherentPointSet(34);
//		The following line is slowing the game down a lot		
//		netwar.Netwar.netwar.soundPlayer.play("sound/UnitBuy1.wav");
	}
	/** Mobile Artillery requires 5 frames to move forward one hex.
	 * @return 5
	 */
	protected int framesToMove(){
		return 5; //return the number of frames needed to move one hex.
	}
	/** Mobile Artillery requires 5 frames to rotate one hex-side.
	 * @return 5
	 */
	protected int framesToRotate(){
		return 5; //return the number of frames needed to rotate one hex-side.
	}
	/** Mobile Artillery requires 10 frames to be created.
	 * @return 10
	 */
	protected int framesToMake(){
		return 20;
	}
	/** Mobile Artillery spends 10 frames doing a death scene.
	 * @return 10
	 */
	protected int framesToDie(){
		return 10;
	}
	protected void createVectors()
	{
		int i = f * 60 + 30;
		vr = new Point3D[1];

		vr[0] = Hex.getMapPoint(x,y);
		
		//Front panel with SIGMA
		cps.add(new SelfConvertingPoint(9,-3,0));
		cps.add(new SelfConvertingPoint(9,3,0));
		cps.add(new SelfConvertingPoint(7,-3,2));
		cps.add(new SelfConvertingPoint(7,3,2));
		cps.add(new SelfConvertingPoint(7.5f,1.5f,1.5f));
		cps.add(new SelfConvertingPoint(7.5f,-1.5f,1.5f));
		cps.add(new SelfConvertingPoint(8,0,1));
		cps.add(new SelfConvertingPoint(8.5f,-1.5f,0.5f));
		cps.add(new SelfConvertingPoint(8.5f,1.5f,0.5f));
		OrderedGraphicSet ogs = new OrderedGraphicSet(
			new GraphicParallelogram(cps.getPoint(0), cps.getPoint(1), cps.getPoint(3), cps.getPoint(2), myPlayer.getColor()), 5);
		ogs.add(new GraphicLine(cps.getPoint(4), cps.getPoint(5), myPlayer.getColor().darker()), false);
		ogs.add(new GraphicLine(cps.getPoint(5), cps.getPoint(6), myPlayer.getColor().darker()), false);
		ogs.add(new GraphicLine(cps.getPoint(6), cps.getPoint(7), myPlayer.getColor().darker()), false);
		ogs.add(new GraphicLine(cps.getPoint(7), cps.getPoint(8), myPlayer.getColor().darker()), false);
		zol.add(ogs);
		
		//Side and rear panels.
		cps.add(new SelfConvertingPoint(7,-3,0));
		cps.add(new SelfConvertingPoint(-9,-3,0));
		cps.add(new SelfConvertingPoint(-9,-3,2));
		cps.add(new SelfConvertingPoint(-9,3,0));
		cps.add(new SelfConvertingPoint(-9,3,2));
		cps.add(new SelfConvertingPoint(7,3,0));
		zol.add(new GraphicTriangle(cps.getPoint(9), cps.getPoint(0), cps.getPoint(2), myPlayer.getColor()));
		zol.add(new GraphicParallelogram(cps.getPoint(9), cps.getPoint(2), cps.getPoint(11), cps.getPoint(10), myPlayer.getColor()));
		zol.add(new GraphicParallelogram(cps.getPoint(10), cps.getPoint(11), cps.getPoint(13), cps.getPoint(12), myPlayer.getColor()));
		zol.add(new GraphicParallelogram(cps.getPoint(12), cps.getPoint(13), cps.getPoint(3), cps.getPoint(14), myPlayer.getColor()));
		zol.add(new GraphicTriangle(cps.getPoint(14), cps.getPoint(1), cps.getPoint(3), myPlayer.getColor()));
		
		//Top of base of unit.
		cps.add(new SelfConvertingPoint(7,-1,2));
		cps.add(new SelfConvertingPoint(7,1,2));
		cps.add(new SelfConvertingPoint(-9,1,2));
		cps.add(new SelfConvertingPoint(-9,-1,2));
		zol.add(new GraphicParallelogram(cps.getPoint(15), cps.getPoint(16), cps.getPoint(17), cps.getPoint(18), myPlayer.getColor().darker()));
		
		//Side thingies.
		cps.add(new SelfConvertingPoint(7,-1,3));
		cps.add(new SelfConvertingPoint(7,1,3));
		cps.add(new SelfConvertingPoint(-9,1,3));
		cps.add(new SelfConvertingPoint(-9,-1,3));
		zol.add(new GraphicTriangle(cps.getPoint(15), cps.getPoint(19), cps.getPoint(2), Color.darkGray));
		zol.add(new GraphicTriangle(cps.getPoint(16), cps.getPoint(20), cps.getPoint(3), Color.darkGray));
		zol.add(new GraphicTriangle(cps.getPoint(17), cps.getPoint(21), cps.getPoint(13), Color.darkGray));
		zol.add(new GraphicTriangle(cps.getPoint(18), cps.getPoint(22), cps.getPoint(11), Color.darkGray));
		zol.add(new GraphicParallelogram(cps.getPoint(15), cps.getPoint(19), cps.getPoint(22), cps.getPoint(18), Color.darkGray));
		zol.add(new GraphicParallelogram(cps.getPoint(2), cps.getPoint(19), cps.getPoint(22), cps.getPoint(11), Color.darkGray));
		zol.add(new GraphicParallelogram(cps.getPoint(16), cps.getPoint(20), cps.getPoint(21), cps.getPoint(17), Color.darkGray));
		zol.add(new GraphicParallelogram(cps.getPoint(3), cps.getPoint(20), cps.getPoint(21), cps.getPoint(13), Color.darkGray));
		
		//Barrel
		barrel = new CoherentPointSet(6);
		cps.add(new SelfConvertingPoint(7,-1,2));
		barrel.add(cps.getPoint(23));
		cps.add(new SelfConvertingPoint(7,1,2));
		barrel.add(cps.getPoint(24));
		cps.add(new SelfConvertingPoint(7,0,3));
		barrel.add(cps.getPoint(25));
		cps.add(new SelfConvertingPoint(-9,-1,2));
		barrel.add(cps.getPoint(26));
		cps.add(new SelfConvertingPoint(-9,1,2));
		barrel.add(cps.getPoint(27));
		cps.add(new SelfConvertingPoint(-9,0,3));
		barrel.add(cps.getPoint(28));
		zol.add(new GraphicParallelogram(barrel.getPoint(0), barrel.getPoint(1), barrel.getPoint(4), barrel.getPoint(3), Color.darkGray));
		zol.add(new GraphicParallelogram(barrel.getPoint(1), barrel.getPoint(2), barrel.getPoint(5), barrel.getPoint(4), Color.darkGray));
		zol.add(new GraphicParallelogram(barrel.getPoint(0), barrel.getPoint(2), barrel.getPoint(5), barrel.getPoint(3), Color.darkGray));
		
		cps.translate(vr[0]);
		cps.rotate(vr[0], Point3D.unitUp, i);
	}
	/** Mobile Artillery is drawn as a Z-Ordered List.
	 */
	public void draw(GameViewer v) {
		if(health > 0)
			zol.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) {
		if(health > 0)
			v.add(zol);
	}
	public void killGraphics() {
		zol.kill();
	}
	
	/** Mobile Artillery moves by sliding. There is no articulate animation. */
	protected void animateMove(){
		//Update the necessary vectors to move forward during one frame.
		int i = Trig.normalize(f * 60 + 30);
		Point3D pt = new Point3D(Trig.cos(i) * 4, Trig.sin(i) * 4,0);
		vr[0].doSum(pt);
		cps.translate(pt);
	}			
	/** Mobile Artillery rotates left by sliding. There is no articulate animation */
	protected void animateRotateLeft(){
		cps.rotate(vr[0], Point3D.unitUp, 12);
	}
	/** Mobile Artillery rotates right by sliding. There is no articulate animation */
	protected void animateRotateRight(){
		cps.rotate(vr[0], Point3D.unitUp, 348);
	}
	protected void animateMake() {
	}
	/** Mobile Artillery explodes. */
	protected void animateDie() {
		if(frame == framesToDie())
			zol.explode((new Point3D(0,0,-5)).doSum(vr[0]));
		frame--;
		if(frame == 0)
		{
			remove();
			Hex.getHex(resX,resY).unreserve();
			Hex.getHex(x,y).leave(this);
		}
	}

//	protected void think() { //Default is not ok for this one. Need to change it... oh time.. precious time...

	/** Mobile Artillery is 4.0 units tall. 
	 * @return 4
	 */
	public float getHeight() {
		return 4.0f;
	}
	/** Mobile Artillery is 9.0 units wide. 
	 * @return 9
	 */
	public float getWidth() {
		return 9.0f;
	}
	/** Mobile Artillery has a weapon range of 210.0 units, or 10 hexes. 
	 * @return 44100
	 */
	public float weaponRangeSquared(){
		return 44100.0f;
	}
	/** Mobile Artillery pursues to a range of 10 hexes. 
	 * @return 10
	 */
	public int followRange(){
		return 10;
	}
	/** Mobile Artillery can fire every 25th frame. 
	 * @return 24
	 */
	public int weaponDelay(){
		return 24;
	}
	/** Mobile Artillery fires a Ballistic Shell.
	 */
	protected boolean fire() {
		Point3D pt = barrel.getPoint(2).getDifference(barrel.getPoint(5));
		pt.doProduct(forceProduct);
		Point3D pt2 = new Point3D(targetSSMDS.getVector(this));
		pt2.doProduct((float)(Math.sqrt((pt.x * pt.x + pt.y * pt.y)/targetSSMDS.getDistanceSquared())));
		pt2.z = pt.z;
		Projectile.newProjectile(new BallisticShell(), barrel.getPoint(2), pt2, 55, 100, 100, this, target);
		return true;
	}
	/** Mobile Artillery has a maximum of 80 health.
	 * @return 80
	 */
	protected int maxHealth() {
		return 80;
	}
	/** Mobile Artillery has an armor rating of 5
	 * @return 5
	 */
	protected int armor() {
		return 5;
	}
	/** Mobile Artillery costs 200.
	 * @return 200
	 */
	public int cost() {
		return 300;
	}
	protected boolean aim(){
		if(!super.aim()) return false;
		
		int range = (int)Math.round(Math.sqrt(targetSSMDS.getDistanceSquared()) / 20);
		if(range > 10) range = 10;
		int angleChange = firingAngles[range] - barrelAngle;
		if(angleChange > 5) {
			barrel.rotate(barrel.getPoint(3), barrel.getPoint(3).getDifference(barrel.getPoint(4)), 5);
			barrelAngle +=5;
			return false;
		}
		if(angleChange < -5) {
			barrel.rotate(barrel.getPoint(3), barrel.getPoint(3).getDifference(barrel.getPoint(4)), 355);
			barrelAngle -=5;
			return false;
		}
		barrel.rotate(barrel.getPoint(3), barrel.getPoint(3).getDifference(barrel.getPoint(4)), Trig.normalize(angleChange));
		barrelAngle = firingAngles[range];
		forceProduct = firingForces[range];
		return true;
	}
}


⌨️ 快捷键说明

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