Show InteractionTest.java syntax highlighted
/*
* Copyright (C) 2001-2005 Pleasant nightmare studio
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package skunkworks;
import com.jme.bounding.OrientedBoundingBox;
import com.jme.input.InputHandler;
import com.jme.input.KeyInput;
import com.jme.input.action.*;
import com.jme.intersection.BoundingCollisionResults;
import com.jme.intersection.CollisionData;
import com.jme.math.Vector3f;
import com.jme.scene.Geometry;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import org.pleasantnightmare.riotinecity.CameraChangeInputAction;
import org.pleasantnightmare.riotinecity.CameraPositionController;
import org.pleasantnightmare.riotinecity.ConsoleGame;
import org.pleasantnightmare.riotinecity.entities.Action;
import org.pleasantnightmare.riotinecity.entities.Atm;
import org.pleasantnightmare.riotinecity.spatials.*;
/**
* @author deus
* @version 1.0
* @since Feb 12, 2007 8:42:30 PM
*/
public class InteractionTest extends ConsoleGame {
private static final int MOVE_SPEED = 1;
private static final int ROOM_WIDTH = 130;
private static final int ROOM_DEPTH = 50;
private static final int ROOM_HEIGHT = 30;
private SquareRoom squareRoomWithCameras;
private Geometry person;
private BoundingCollisionResults collisionResults;
private InputHandler input;
protected void simpleInitGame() {
addRoom();
addPerson();
addTestBox();
initInputHandler();
addAtm();
collisionResults = new BoundingCollisionResults();
}
protected Node findAtmCollisionNode() {
for (int i = 0; i < collisionResults.getNumber(); i++) {
CollisionData cd = collisionResults.getCollisionData(i);
Node node = cd.getSourceMesh().getParent();
if ("test-atm".equals(node.getName()))
return node;
}
return null;
}
private void addAtm() {
Atm atm = new Atm(SkunkworkUtils.createAtmModel(), new DefaultDigipad(new AtmDigipadConfiguration()), new SwingScreen());
AtmSpatial spatial = new AtmSpatial("test-atm", atm);
spatial.setLocalTranslation(new Vector3f(ROOM_WIDTH / 2, ROOM_HEIGHT / 2, 0));
sceneRoot.attachChild(spatial);
}
private void initInputHandler() {
input = new InputHandler();
addHandler(input);
forward();
back();
left();
right();
switchCamera();
collisionDetection();
}
private void collisionDetection() {
input.addAction(new KeyInputAction() {
public void performAction(InputActionEvent inputActionEvent) {
collisionResults.clear();
sceneRoot.calculateCollisions(sceneRoot, collisionResults);
if (collisionResults.getNumber() > 0) {
Node atmNode = findAtmCollisionNode();
if (atmNode == null) {
System.out.println("Not an atm node.");
return;
}
AtmSpatial spatial = (AtmSpatial) atmNode;
Atm atm = spatial.getEntity();
for (Action a : atm.getAvailableActions())
System.out.println(a);
/* for (int i = 0; i < collisionResults.getNumber(); i++)
System.out.println("Collision - YES"
+ collisionResults.getCollisionData(i)
.getSourceMesh().getParent().getName());
*/
} else
System.out.println("Collision - NO");
}
}, "collisionDetection", KeyInput.KEY_E, false);
}
private void left() {
input.addAction(new KeyNodeStrafeLeftAction(person, MOVE_SPEED), "left", KeyInput.KEY_LEFT, true);
}
private void right() {
input.addAction(new KeyNodeStrafeRightAction(person, MOVE_SPEED), "right", KeyInput.KEY_RIGHT, true);
}
private void back() {
input.addAction(new KeyNodeBackwardAction(person, MOVE_SPEED), "back", KeyInput.KEY_DOWN, true);
}
private void forward() {
input.addAction(new KeyNodeForwardAction(person, MOVE_SPEED), "forward", KeyInput.KEY_UP, true);
}
private void switchCamera() {
// TODO FIXME
CameraPositionController controller = new CameraPositionController(camera, new PlayerSpatial());
input.addAction(new CameraChangeInputAction(controller), "camera", KeyInput.KEY_C, false);
}
private void addRoom() {
squareRoomWithCameras = new SquareRoom(ROOM_WIDTH,
ROOM_DEPTH, ROOM_HEIGHT);
sceneRoot.attachChild(squareRoomWithCameras);
}
private void addPerson() {
OrientedBoundingBox obb = new OrientedBoundingBox();
obb.setExtent(new Vector3f(6, 3, 3));
person = new Sphere("person", new Vector3f(0, 0, 0), 10, 10, 3);
person.setLocalTranslation(squareRoomWithCameras.center());
person.setModelBound(obb);
// person.getModelBound().setCenter(new Vector3f(3, 0, 0));
sceneRoot.attachChild(person);
}
private void addTestBox() {
OrientedBoundingBox obb2 = new OrientedBoundingBox();
obb2.setExtent(new Vector3f(6, 3, 3));
Geometry testBox = new Box("testBox", new Vector3f(2, 2, 2), new Vector3f(5, 5, 5));
testBox.setModelBound(obb2);
testBox.updateModelBound();
sceneRoot.attachChild(testBox);
}
public static void main(String[] args) {
InteractionTest c = new InteractionTest();
c.start();
}
}
See more files for this project here