dommstyle.txt
来自「Simple DOOM style navigation of a 3D sce」· 文本 代码 · 共 1,957 行 · 第 1/4 页
TXT
1,957 行
protected Transform3D m_Transform3D = null;
protected TextureAttributes m_TextureAttributes = null;
protected double rotY = 0;
public TextureAnimationBehavior(TextureAttributes texAttribs) {
m_TextureAttributes = texAttribs;
m_Transform3D = new Transform3D();
m_TextureAttributes
.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE);
// create the WakeupCriterion for the behavior
WakeupCriterion criterionArray[] = new WakeupCriterion[1];
criterionArray[0] = new WakeupOnElapsedTime(300);
// save the WakeupCriterion for the behavior
m_WakeupCondition = new WakeupOr(criterionArray);
}
public void initialize() {
// apply the initial WakeupCriterion
wakeupOn(m_WakeupCondition);
}
public void processStimulus(java.util.Enumeration criteria) {
while (criteria.hasMoreElements()) {
WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement();
if (wakeUp instanceof WakeupOnElapsedTime) {
rotY += Utils.getRandomNumber(0.01, 0.01);
m_Transform3D.rotY(rotY);
m_TextureAttributes.setTextureTransform(m_Transform3D);
}
}
// assign the next WakeUpCondition, so we are notified again
wakeupOn(m_WakeupCondition);
}
}
// this class implements a simple behavior that
// calculates and prints the size of an object
// based on the vertices in its GeometryArray
class RandomWalkBehavior extends Behavior {
// the wake up condition for the behavior
protected WakeupCondition m_WakeupCondition = null;
protected TransformGroup m_TransformGroup = null;
protected Transform3D m_Transform3D = null;
protected Vector3d TargetVector3d = null;
protected Vector3d CurrentVector3d = null;
private final double m_MovementX = 2;
private final double m_MovementY = 0;
private final double m_MovementZ = 2;
private int m_nFrameCount = 0;
private CollisionChecker m_CollisionChecker = null;
public RandomWalkBehavior(TransformGroup tg, CollisionDetector detector) {
m_TransformGroup = tg;
m_CollisionChecker = new CollisionChecker(tg, detector, false);
m_Transform3D = new Transform3D();
TargetVector3d = new Vector3d();
CurrentVector3d = new Vector3d();
// create the WakeupCriterion for the behavior
WakeupCriterion criterionArray[] = new WakeupCriterion[1];
criterionArray[0] = new WakeupOnElapsedTime(100);
// save the WakeupCriterion for the behavior
m_WakeupCondition = new WakeupOr(criterionArray);
}
public void initialize() {
// apply the initial WakeupCriterion
wakeupOn(m_WakeupCondition);
}
public void processStimulus(java.util.Enumeration criteria) {
while (criteria.hasMoreElements()) {
WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement();
if (wakeUp instanceof WakeupOnElapsedTime) {
if (m_nFrameCount % 100 == 0) {
// generate a random direction for movement
TargetVector3d.x = m_MovementX
* Utils.getRandomNumber(0, 1);
TargetVector3d.y = m_MovementY
* Utils.getRandomNumber(0, 1);
TargetVector3d.z = m_MovementZ
* Utils.getRandomNumber(0, 1);
}
CurrentVector3d.x += TargetVector3d.x
* Utils.getRandomNumber(1, 0.1);
CurrentVector3d.y += TargetVector3d.y
* Utils.getRandomNumber(1, 0.1);
CurrentVector3d.z += TargetVector3d.z
* Utils.getRandomNumber(1, 0.1);
m_Transform3D.setTranslation(CurrentVector3d);
if (m_CollisionChecker.isCollision(m_Transform3D) == false)
m_TransformGroup.setTransform(m_Transform3D);
m_nFrameCount++;
}
}
// assign the next WakeUpCondition, so we are notified again
wakeupOn(m_WakeupCondition);
}
}
abstract class ComplexObject extends BranchGroup {
protected Group m_ParentGroup = null;
protected int m_nFlags = 0;
protected BackgroundSound m_CollideSound = null;
protected Component m_Component = null;
protected TransformGroup m_TransformGroup = null;
protected TransformGroup m_BehaviorTransformGroup = null;
public static final int SOUND = 0x001;
public static final int GEOMETRY = 0x002;
public static final int TEXTURE = 0x004;
public static final int COLLISION = 0x008;
public static final int COLLISION_SOUND = 0x010;
public ComplexObject(Component comp, Group group, int nFlags) {
m_ParentGroup = group;
m_nFlags = nFlags;
m_Component = comp;
}
public Bounds getGeometryBounds() {
return new BoundingSphere(new Point3d(0, 0, 0), 100);
}
private MediaContainer loadSoundFile(String szFile) {
try {
File file = new File(System.getProperty("user.dir"));
URL url = file.toURL();
URL soundUrl = new URL(url, szFile);
return new MediaContainer(soundUrl);
} catch (Exception e) {
System.err.println("Error could not load sound file: " + e);
System.exit(-1);
}
return null;
}
protected void setTexture(Appearance app, String szFile) {
Texture tex = new TextureLoader(szFile, m_Component).getTexture();
app.setTexture(tex);
}
abstract protected Group createGeometryGroup(Appearance app,
Vector3d position, Vector3d scale, String szTextureFile,
String szSoundFile);
protected Group loadGeometryGroup(String szModel, Appearance app)
throws java.io.FileNotFoundException {
// load the object file
Scene scene = null;
Shape3D shape = null;
// read in the geometry information from the data file
ObjectFile objFileloader = new ObjectFile(ObjectFile.RESIZE);
scene = objFileloader.load(szModel);
// retrieve the Shape3D object from the scene
BranchGroup branchGroup = scene.getSceneGroup();
shape = (Shape3D) branchGroup.getChild(0);
shape.setAppearance(app);
return branchGroup;
}
protected int getSoundLoop(boolean bCollide) {
return 1;
}
protected float getSoundPriority(boolean bCollide) {
return 1.0f;
}
protected float getSoundInitialGain(boolean bCollide) {
return 1.0f;
}
protected boolean getSoundInitialEnable(boolean bCollide) {
return true;
}
protected boolean getSoundContinuousEnable(boolean bCollide) {
return false;
}
protected Bounds getSoundSchedulingBounds(boolean bCollide) {
return new BoundingSphere(new Point3d(0, 0, 0), 1.0);
}
protected boolean getSoundReleaseEnable(boolean bCollide) {
return true;
}
protected Point2f[] getSoundDistanceGain(boolean bCollide) {
return null;
}
protected void setSoundAttributes(Sound sound, boolean bCollide) {
sound.setCapability(Sound.ALLOW_ENABLE_WRITE);
sound.setCapability(Sound.ALLOW_ENABLE_READ);
sound.setSchedulingBounds(getSoundSchedulingBounds(bCollide));
sound.setEnable(getSoundInitialEnable(bCollide));
sound.setLoop(getSoundLoop(bCollide));
sound.setPriority(getSoundPriority(bCollide));
sound.setInitialGain(getSoundInitialGain(bCollide));
sound.setContinuousEnable(getSoundContinuousEnable(bCollide));
sound.setReleaseEnable(bCollide);
if (sound instanceof PointSound) {
PointSound pointSound = (PointSound) sound;
pointSound.setInitialGain(getSoundInitialGain(bCollide));
Point2f[] gainArray = getSoundDistanceGain(bCollide);
if (gainArray != null)
pointSound.setDistanceGain(gainArray);
}
}
public Group createObject(Appearance app, Vector3d position,
Vector3d scale, String szTextureFile, String szSoundFile,
String szCollisionSound) {
m_TransformGroup = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(scale);
t3d.setTranslation(position);
m_TransformGroup.setTransform(t3d);
m_BehaviorTransformGroup = new TransformGroup();
if ((m_nFlags & GEOMETRY) == GEOMETRY)
m_BehaviorTransformGroup.addChild(createGeometryGroup(app,
position, scale, szTextureFile, szSoundFile));
if ((m_nFlags & SOUND) == SOUND) {
MediaContainer media = loadSoundFile(szSoundFile);
PointSound pointSound = new PointSound(media,
getSoundInitialGain(false), 0, 0, 0);
setSoundAttributes(pointSound, false);
m_BehaviorTransformGroup.addChild(pointSound);
}
if ((m_nFlags & COLLISION) == COLLISION) {
m_BehaviorTransformGroup
.setCapability(Node.ENABLE_COLLISION_REPORTING);
m_BehaviorTransformGroup.setCollidable(true);
m_BehaviorTransformGroup.setCollisionBounds(getGeometryBounds());
if ((m_nFlags & COLLISION_SOUND) == COLLISION_SOUND) {
MediaContainer collideMedia = loadSoundFile(szCollisionSound);
m_CollideSound = new BackgroundSound(collideMedia, 1);
setSoundAttributes(m_CollideSound, true);
m_TransformGroup.addChild(m_CollideSound);
}
CollisionBehavior collision = new CollisionBehavior(
m_BehaviorTransformGroup, this);
collision.setSchedulingBounds(getGeometryBounds());
m_BehaviorTransformGroup.addChild(collision);
}
m_TransformGroup.addChild(m_BehaviorTransformGroup);
m_ParentGroup.addChild(m_TransformGroup);
return m_BehaviorTransformGroup;
}
public void onCollide(boolean bCollide) {
System.out.println("Collide: " + bCollide);
if (m_CollideSound != null && bCollide == true)
m_CollideSound.setEnable(true);
}
public void attachBehavior(Behavior beh) {
m_BehaviorTransformGroup
.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
beh.setSchedulingBounds(getGeometryBounds());
m_BehaviorTransformGroup.addChild(beh);
}
public TransformGroup getBehaviorTransformGroup() {
return m_BehaviorTransformGroup;
}
public void attachSplinePathInterpolator(Alpha alpha, Transform3D axis,
URL urlKeyframes) {
// read a spline path definition file and
// add a Spline Path Interpolator to the TransformGroup for the object.
m_BehaviorTransformGroup
.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
RotPosScaleTCBSplinePathInterpolator splineInterpolator = Utils
.createSplinePathInterpolator(alpha, m_BehaviorTransformGroup,
axis, urlKeyframes);
if (splineInterpolator != null) {
splineInterpolator.setSchedulingBounds(getGeometryBounds());
m_BehaviorTransformGroup.addChild(splineInterpolator);
} else {
System.out.println("attachSplinePathInterpolator failed for: "
+ urlKeyframes);
}
}
}
class KeyBehavior extends Behavior {
protected static final double FAST_SPEED = 20.0;
protected static final double NORMAL_SPEED = 1.0;
protected static final double SLOW_SPEED = 0.5;
protected TransformGroup transformGroup;
protected Transform3D transform3D;
protected WakeupCondition keyCriterion;
private final static double TWO_PI = (2.0 * Math.PI);
private double rotateXAmount = Math.PI / 16.0;
private double rotateYAmount = Math.PI / 16.0;
private double rotateZAmount = Math.PI / 16.0;
private double moveRate = 5;
private double speed = NORMAL_SPEED;
private final double kMoveForwardScale = 1.1;
private final double kMoveBackwardScale = 0.9;
private int forwardKey = KeyEvent.VK_UP;
private int backKey = KeyEvent.VK_DOWN;
private int leftKey = KeyEvent.VK_LEFT;
private int rightKey = KeyEvent.VK_RIGHT;
public KeyBehavior(TransformGroup tg) {
super();
transformGroup = tg;
transform3D = new Transform3D();
}
public void initialize() {
WakeupCriterion[] keyEvents = new WakeupCriterion[2];
keyEvents[0] = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
keyEvents[1] = new WakeupOnAWTEvent(KeyEvent.KEY_RELEASED);
keyCriterion = new WakeupOr(keyEvents);
wakeupOn(keyCriterion);
}
public void processStimulus(Enumeration criteria) {
WakeupCriterion wakeup;
AWTEvent[] event;
while (criteria.hasMoreElements()) {
wakeup = (WakeupCriterion) criteria.nextElement();
if (!(wakeup instanceof WakeupOnAWTEvent))
continue;
event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
for (int i = 0; i < event.length; i++) {
if (event[i].getID() == KeyEvent.KEY_PRESSED) {
processKeyEvent((KeyEvent) event[i]);
}
}
}
wakeupOn(keyCriterion);
}
protected void processKeyEvent(KeyEvent event) {
int keycode = event.getKeyCode();
if (event.isShiftDown())
speed = FAST_SPEED;
else
speed = NORMAL_SPEED;
if (event.isAltDown())
altMove(keycode);
else if (event.isControlDown())
controlMove(keycode);
else
standardMove(keycode);
}
//moves forward backward or rotates left right
private void standardMove(int keycode) {
if (keycode == forwardKey)
moveForward();
else if (keycode == backKey)
moveBackward();
else if (keycode == leftKey)
rotLeft();
else if (keycode == rightKey)
rotRight();
}
//moves left right, rotate up down
protected void altMove(int keycode) {
if (keycode == forwardKey)
rotUp();
else if (keycode == backKey)
rotDown();
else if (keycode == leftKey)
rotLeft();
else if (keycode == rightKey)
rotRight();
else if (keycode == leftKey)
moveLeft();
else if (keycode == rightKey)
moveRight();
}
//move up down, rot left right
protected void controlMove(int keycode) {
if (keycode == forwardKey)
moveUp();
else if (keycode == backKey)
moveDown();
else if (keycode == leftKey)
rollLeft();
else if (keycode == rightKey)
rollRight();
}
private void moveForward() {
doMove(new Vector3d(0.0, 0.0, kMoveForwardScale * speed));
}
private void moveBackward() {
doMove(new Vector3d(0.0, 0.0, -kMoveBackwardScale * speed));
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?