I got a design question; I’m trying to create a basic game on the example on the last exercise of the chapter 3 of “JME Beginner’s Guide.”
The structure is the following:
The main class:
[java]Key Mappings(R click, L click);
stateManager.attach(new TowerGameAppState());
[/java]
A Tower class:
[java]private: int bullets, lifePoints;
Geometry geometry;
A constructor, getGeomtry(), getLifePoints().[/java]
A Creeper class:
[java]private: int lifePoints;
Geometry geometry;[/java]
A constructor and methods…
Then I create the control classes: CreeperControl and TowerControll
TowerControl:
[java]private TowerGameAppState appState;
ArrayList<Creeper> reachable;
public TowerControl(TowerGameAppState appState) {
this.appState = appState;
}
@Override
protected void controlUpdate(float tpf) {
//It searches in the creeper array list for those creepers near the base
}
[/java]
CreeperControll: move the creeper from the initial position to the base, when the creeper reaches the base it erases itself.
Game App State class:
[java] private int score;
private int health;
private int budget;
private Boolean lastGameWon;
private SimpleApplication app;
private AssetManager assetManager;
private Camera cam;
private Node rootNode;
private ArrayList<Creeper> creepersList = new ArrayList<Creeper>();
private Material greenMat;
private Material creeperMat;
private int creeperCount=0;
@Override
public void initialize(AppStateManager stateManager, Application app) {
//create a floor
//create two towers (on the right and on the left of the base)
}
public void update(float tpf) {
//create creepers in a loop and attach to them the control class
}
}
public Creeper getCreeperFromList(String name);[/java]
The problem is the following: if I want to use the key mappings to decide which tower have to shoot the creepers, Where I should insert this feature? Here’s my solution:
in the Main class:
[java]
…
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals(MAPPING_SHOT_DX)){
rootNode.getChild(“Tower.0”).getControl(TowerControl.class).hit();
} else if (name.equals(MAPPING_SHOT_SX)){
rootNode.getChild(“Tower.1”).getControl(TowerControl.class).hit();
}
}
};[/java]
to use this solution I removed the shooting process form the tower control and I created a hit method:
[java]public void hit(){
for (int i = 0; i < reachable.size(); i++) {
Line ray = new Line(spatial.getLocalTranslation(),
reachable.get(i).getGeometry().getLocalTranslation());
Geometry rayGeom = new Geometry(“ray”, ray);
rayGeom.setMaterial(appState.getGreenColor());
…
}
[/java]