Show ActionList.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 org.pleasantnightmare.riotinecity.console;
import com.jme.intersection.BoundingCollisionResults;
import com.jme.scene.Node;
import org.pleasantnightmare.riotinecity.entities.Action;
import org.pleasantnightmare.riotinecity.spatials.InteractibleNode;
import java.util.HashSet;
import java.util.Set;
/**
* @author: deus
* @since: Mar 26, 2007 2:50:48 PM
* @version: 1.0
*/
public class ActionList extends InputProcessor {
public static final String COMMAND = "al";
private BoundingCollisionResults collisionResults;
private String id;
public ActionList() {
collisionResults = new BoundingCollisionResults();
}
private Set<InteractibleNode> filterInteractibleNodes() {
Set<InteractibleNode> interactibleNodes = new HashSet<InteractibleNode>();
for (int i = 0; i < collisionResults.getNumber(); i++) {
Node node = collisionResults.getCollisionData(i).getSourceMesh().getParent();
if (node instanceof InteractibleNode)
interactibleNodes.add((InteractibleNode) node);
}
return interactibleNodes;
}
public void process(String input) throws ProcessingError {
String[] splits = input.trim().split(" ");
if (splits.length > 1)
id = splits[1];
else
id = null;
rootNode.calculateCollisions(rootNode, collisionResults);
processList();
}
private void processList() throws ProcessingError {
if (collisionResults.getNumber() <= 0)
throw new ProcessingError("No collisions detected.");
Set<InteractibleNode> iNodes = filterInteractibleNodes();
if (iNodes.isEmpty())
throw new ProcessingError("No Interactible detected.");
else if (id != null)
listById(iNodes);
else
listNearest(iNodes);
}
private void listNearest(Set<InteractibleNode> iNodes) throws ProcessingError {
if (iNodes.size() > 1)
throw new ProcessingError("More than one Interactible detected, use 'al <id>'.");
printActions(iNodes.iterator().next());
}
private void printActions(InteractibleNode node) {
for (Action action : node.getEntity().getAvailableActions())
model.writeLine(action.getName());
}
private void listById(Set<InteractibleNode> iNodes) throws ProcessingError {
InteractibleNode node = findNodeById(iNodes);
if (node == null)
throw new ProcessingError("No node found: " + id);
printActions(node);
}
private InteractibleNode findNodeById(Set<InteractibleNode> iNodes) {
for (InteractibleNode iNode : iNodes)
if (iNode.getName().equals(id))
return iNode;
return null;
}
public boolean isProcessable(String input) {
return input.trim().startsWith(COMMAND);
}
public String getCommand() {
return COMMAND;
}
}
See more files for this project here