reusablesceneobject.java

来自「Java mulitplayer strategy game. Adaptati」· Java 代码 · 共 215 行

JAVA
215
字号
/*
 * Created on 2005-10-11
 * $Id: ReusableSceneObject.java,v 1.3 2006/01/07 16:18:58 macx2k Exp $
 */
package net.sf.jawp.j3d.object;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Node;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;

import net.sf.jawp.j3d.picking.PickingHelper;

/**
 * @author Maciej Malecki
 * @version $Revision: 1.3 $
 * @param <RENDERABLE>
 *            type which will be rendered with this renderer
 */
public abstract class ReusableSceneObject<RENDERABLE>
{
	private static final Vector3f PARKING_POSITION = new Vector3f(200000f, 200000f, 200000f); 
	
	private final boolean pickable;

	private RENDERABLE renderedObject = null;

	private TransformGroup positionTG;
	
	private TransformGroup rotation1TG;
	
	private TransformGroup rotation2TG;
	
	private TransformGroup rotation3TG;

	private Transform3D transform3d = new Transform3D();

	private Node shapeNode = null;

	private BranchGroup branchGroup = null;

	private boolean inited = false;

	/**
	 * Creates new instance of TemporaryObjectSceneNode.
	 * 
	 * @param pickable
	 *            if this scenegraph node can be picked
	 */
	protected ReusableSceneObject(final boolean pickable)
	{
		this.pickable = pickable;
	}

	/**
	 * Attaches new business object to this temporary scenegraph node.
	 * 
	 * @param renderedObject
	 *            business object to be rendered
	 * @throws NullPointerException
	 *             if argument is null
	 * @throws IllegalStateException
	 *             if there is some business object attached to this node
	 */
	public final void connect(final RENDERABLE renderedObject)
	{
		if (renderedObject == null)
		{
			throw new NullPointerException("Argument renderedObject is null");
		}
		if (this.renderedObject != null)
		{
			throw new IllegalStateException(
					"Object renderer is already attached. Disconnect it first.");
		}
		this.renderedObject = renderedObject;
		setUserData(renderedObject);
		positionTG.getTransform(transform3d);
		transform3d.setScale(1d);
		positionTG.setTransform(transform3d);
		// notify onConnected method
		onConnected(renderedObject);
	}

	/**
	 * Disconnects business object from this temporary scenegraph node. Node
	 * will be hidden.
	 */
	public final void disconnect()
	{
		// notify onDisconnected method
		onDisconnected();
		
		this.renderedObject = null;
		setUserData(null);
		positionTG.getTransform(transform3d);
		transform3d.setScale(0d);
		transform3d.set(PARKING_POSITION);
		positionTG.setTransform(transform3d);
	}

	/**
	 * Updates scenegraph node positon with attached business object data.
	 * 
	 * @throws IllegalStateException
	 *             when there is no business object connected
	 */
	public final void update()
	{
		if (renderedObject == null)
		{
			throw new IllegalStateException(
					"Object renderer is not attached to any object. Call attach method first.");
		}
		// update position
		positionTG.getTransform(transform3d);
		updatePosition(renderedObject, transform3d);
		positionTG.setTransform(transform3d);

		// update direction
		rotation1TG.getTransform(transform3d);
		updateDirection1(renderedObject, transform3d);
		rotation1TG.setTransform(transform3d);
		
		rotation2TG.getTransform(transform3d);
		updateDirection2(renderedObject, transform3d);
		rotation2TG.setTransform(transform3d);
		
		rotation3TG.getTransform(transform3d);
		updateDirection3(renderedObject, transform3d);
		rotation3TG.setTransform(transform3d);
	}

	public final Node getSceneObject()
	{
		if (!inited)
		{
			init();
		}
		return branchGroup;
	}
	
	protected void onConnected(final RENDERABLE renderedObject)
	{
	}
	
	protected void onDisconnected()
	{
	}
	
	protected void setUserData(final RENDERABLE renderedObject)
	{
		shapeNode.setUserData(renderedObject);
	}

	private void init()
	{
		if (inited)
		{
			throw new IllegalStateException("Object is already inited");
		}
		inited = true;

		branchGroup = new BranchGroup();

		// define position transformation
		final Transform3D transform3d = new Transform3D();
		transform3d.setScale(0d);
		transform3d.set(PARKING_POSITION);
		positionTG = new TransformGroup(transform3d);
		positionTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		positionTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		
		//define direction transformations
		rotation1TG = new TransformGroup();
		rotation1TG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		rotation1TG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		
		rotation2TG = new TransformGroup();
		rotation2TG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		rotation2TG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		
		rotation3TG = new TransformGroup();
		rotation3TG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		rotation3TG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

		shapeNode = createScenegraphObject();
		assert shapeNode != null : "ScenegraphObject is null";
		if (pickable)
		{
			PickingHelper.enablePicking(shapeNode);
		}
		rotation1TG.addChild(shapeNode);
		rotation2TG.addChild(rotation1TG);
		rotation3TG.addChild(rotation2TG);
		positionTG.addChild(rotation3TG);
		branchGroup.addChild(positionTG);
	}

	protected abstract void updatePosition(final RENDERABLE renderedObject,
			final Transform3D transform3d);
	
	protected abstract void updateDirection1(final RENDERABLE renderedObject,
			final Transform3D rotate);

	protected abstract void updateDirection2(final RENDERABLE renderedObject,
			final Transform3D rotate);

	protected abstract void updateDirection3(final RENDERABLE renderedObject,
			final Transform3D rotate);

	protected abstract Node createScenegraphObject();
}

⌨️ 快捷键说明

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