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

📄 virtualcoherentgraphicthing.java

📁 NetWar是一个实时策略游戏
💻 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.utils.vectorgraphics;
import netwar.utils.*;

/** Nonprimitive GraphicThing
 * Used for static GraphicThings which use a CoherentPointSet.
 * Performs draws by moving the GraphicThing by an offset, drawing it, then moving it back.
 * Should only be used for GraphicThings which are only used in this way.
 */
public class VirtualCoherentGraphicThing extends GraphicThing {
	
	private CoherentPointSet points;
	
	private GraphicThing graphic;
	
	private Point3D offset;
	private Point3D unoffset;
	private int angle;

	private boolean inPlace = false;
	
	/** Creates a new instance of VirtualCoherentGraphicThing 
	 * @param gt The static GraphicThing to use.
	 * @param cps The set of points that the GraphicThing uses.
	 * @param p3d The ground-center of this copy of the GraphicThing.
	 * @param theta Angle to rotate this copy of the GraphicThing about its ground-center.
	 */
	public VirtualCoherentGraphicThing(GraphicThing gt, CoherentPointSet cps, Point3D p3d, int theta) {
		graphic = gt;
		points = cps;
		offset = new Point3D(p3d);
		unoffset = offset.getProduct(-1);
		angle = Trig.normalize(theta);
	}
	
	public void draw(netwar.game.GameViewer v) {
		if(!inPlace){
			inPlace = true;
			points.translate(offset);
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, angle);
			graphic.update();
			graphic.draw(v);
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, Trig.fullCircle - angle);
			points.translate(unoffset);
			graphic.update();
			inPlace = false;
		}else{
			graphic.draw(v);
		}
	}
	
	public float getZ(Transform t) {
		if(!inPlace) {
			inPlace = true;
			points.translate(offset);
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, angle);
			graphic.update();
			float z = graphic.getZ(t);
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, Trig.fullCircle - angle);
			points.translate(unoffset);
			graphic.update();
			inPlace = false;
			return z;
		}else{
			float z = graphic.getZ(t);
			return z;
		}
	}
	
	public boolean isOnScreen(Transform t) {
		if(!inPlace) {
			inPlace = true;
			points.translate(offset);
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, angle);
			graphic.update();
			boolean on = graphic.isOnScreen(t);
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, Trig.fullCircle - angle);
			points.translate(unoffset);
			graphic.update();
			inPlace = false;
			return on;
		}
		return graphic.isOnScreen(t);
	}
	/** Rotate this GraphicThing.
	 * NOTE: VirtualCoherentGraphicThing will only allow rotation around the
	 * line extending up from its ground-center.
	 * @param axisPt A point on the axis of rotation.
	 * @param axisDir The direction of the axis of rotation.
	 * @param angle The number of degrees to rotate.
	 * @return this GraphicThing, freshly rotated.
	 */
	public GraphicThing rotate(Point3D axisPt, Point3D axisDir, int theta) {
		Assert.notFalse(axisPt.equals(offset), "Illegal axis of rotation in VirtualCoherentGraphicThing");
		Assert.notFalse((axisDir.x == 0) && (axisDir.y == 0) && (axisDir.z > 0), "Axis of rotation must be vertical in VirtualCoherentGraphicThing");
		angle = Trig.normalize(angle + theta);
		return this;
	}
	
	public GraphicThing translate(Point3D p3d) {
		offset.doSum(p3d);
		return this;
	}
	
	public void explode(Point3D epicenter) {
		GraphicThing gt;
		if(!inPlace) {
			inPlace = true;
			points.translate(offset);
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, angle);
			graphic.update();
			gt = graphic.copy();
			if(angle > 0)
				points.rotate(offset, Point3D.unitUp, Trig.fullCircle - angle);
			points.translate(unoffset);
			graphic.update();
			inPlace = false;
		}else
			gt = graphic.copy();
		gt.explode(epicenter);
		kill();
	}
	Point3D getCenter() {
		return offset;
	}
	void unlink() {}
	public GraphicThing copy() {
		return new VirtualCoherentGraphicThing(graphic.copy(), points, new Point3D(offset), angle);
	}
}

⌨️ 快捷键说明

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