Show ActionMenuTestGame.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.app.SimpleGame;
import com.jme.input.KeyInput;
import static com.jme.input.KeyInput.*;
import com.jme.input.KeyInputListener;
import static org.pleasantnightmare.riotinecity.ActionNames.*;
import org.pleasantnightmare.riotinecity.entities.Action;
import org.pleasantnightmare.riotinecity.entities.Entity;
import org.pleasantnightmare.riotinecity.model.ActionMenuModel;
import org.pleasantnightmare.riotinecity.spatials.ActionMenuSpatial;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author: deus
* @since: Jun 8, 2007 7:35:01 AM
* @version: 1.0
*/
public abstract class ActionMenuTestGame extends SimpleGame {
private ActionMenuModel model;
private ActionMenuSpatial spatial;
private Map<String, Integer> transformer;
public ActionMenuTestGame() {
transformer = new HashMap<String, Integer>();
transformer.put(FORWARD, 0);
transformer.put(BACKWARD, 1);
transformer.put(ROTATE_LEFT, 2);
transformer.put(ROTATE_RIGHT, 3);
transformer.put(FORWARD_RIGHT, 4);
transformer.put(BACKWARD_RIGHT, 5);
transformer.put(BACKWARD_LEFT, 6);
transformer.put(FORWARD_LEFT, 7);
}
protected void initActionMenu() {
createModel();
createSpatial();
createController();
}
private void createController() {
final KeyInput ki = get();
ki.addListener(new KeyInputListener() {
public void onKey(char character, int keyCode, boolean pressed) {
if (keyCode != KEY_LCONTROL && !ki.isKeyDown(KEY_LCONTROL))
return;
if (keyCode == KEY_LCONTROL && pressed)
spatial.show(rootNode);
else if (keyCode == KEY_LCONTROL && !pressed) {
spatial.hide();
doAction();
} else if (keyCode == KEY_SPACE) {
model.chooseOrMarkDefault();
} else if (ki.isKeyDown(KEY_UP) && ki.isKeyDown(KEY_LEFT))
model.choose(transformer.get(FORWARD_LEFT));
else if (ki.isKeyDown(KEY_UP) && ki.isKeyDown(KEY_RIGHT))
model.choose(transformer.get(FORWARD_RIGHT));
else if (ki.isKeyDown(KEY_DOWN) && ki.isKeyDown(KEY_RIGHT))
model.choose(transformer.get(BACKWARD_RIGHT));
else if (ki.isKeyDown(KEY_DOWN) && ki.isKeyDown(KEY_LEFT))
model.choose(transformer.get(BACKWARD_LEFT));
else if (ki.isKeyDown(KEY_DOWN))
model.choose(transformer.get(BACKWARD));
else if (ki.isKeyDown(KEY_UP))
model.choose(transformer.get(FORWARD));
else if (ki.isKeyDown(KEY_LEFT))
model.choose(transformer.get(ROTATE_LEFT));
else if (ki.isKeyDown(KEY_RIGHT))
model.choose(transformer.get(ROTATE_RIGHT));
if (!pressed)
model.chooseNothing();
}
});
}
private void doAction() {
int chosen = model.getChosen();
if (chosen == ActionMenuModel.NOT_SELECTED)
return;
Action action = model.getActions().get(chosen);
action.doIt();
}
private void createSpatial() {
spatial = new ActionMenuSpatial();
spatial.setModel(model);
}
private void createModel() {
model = new ActionMenuModel();
model.setActions(getActions());
}
protected List<Action> getActions() {
return getEntity().getAvailableActions();
}
protected abstract Entity getEntity();
}
See more files for this project here