Jump to content

  • Log In with Google      Sign In   
  • Create Account

First try at Tower Defense


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
No replies to this topic

#1   Members   

126
Like
0Likes
Like

Posted 13 August 2014 - 11:35 AM

I tried making this tower defense game. I've made a lot of bugs in my code, and I really need some help. I'm new, and I'm basically stuck. It's been days, in fact. Please note that I'm using JMonkeyEngine 3.0, so I hope you guys are familiar with working with this engine. Please note that this is not complete, so that's why you may see some useless variables.

 

Let me explain what it basically it. Everything is made out of cubes even the floor, so we have a base which is a rectangle, and cubes moving towards it. I've got that part finished, and on both sides of the path we have two rectangular towers. The user clicks on one to select them, and presses "c" to give them a charge. A charge is basically an ammo packet, and each charge has 5 bullets.

 

Here's the code for a charge:

 

package mygame;


public class Charges {
    private int damage = 5;
    private int bullets = 5;
    


    public int getDamage() {
        return damage;
    }


    public int getRemainingShots() {
        return bullets;
    }
}

Here is the class with all the main methods, and I believe everything is in order in this class. I might be wrong, though.

 

package mygame;


import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AppState;
import com.jme3.collision.CollisionResults;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.system.AppSettings;
import java.util.ArrayList;


public class Main extends SimpleApplication {
    private static final String MAPPING_SELECT_TOWER = "Select Tower";
    private static final String MAPPING_CHARGE_TOWER = "Charge Tower";
    
    private static final Trigger TRIGGER_LEFT_CLICK = new MouseButtonTrigger(MouseInput.BUTTON_LEFT);
    private static final Trigger TRIGGER_KEY_C = new KeyTrigger(KeyInput.KEY_C);
    
    private TowerControl selected = null;


    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setSettingsDialogImage("Interface//708459-1920x1080-java1920x1200.png");
        settings.setTitle("Tower Defense");
        
        Main app = new Main();
        app.setShowSettings(false);
        app.setSettings(settings);
      //app.setDisplayStatView(false);
      //app.setDisplayFps(false);
        
        app.start();
    }


    @Override
    public void simpleInitApp() {
        flyCam.setDragToRotate(true);
        flyCam.setMoveSpeed(100);
        inputManager.setCursorVisible(true);
        
        GamePlayAppState gamePlay = new GamePlayAppState();
        stateManager.attach((AppState) gamePlay);
        
        inputManager.addMapping(MAPPING_SELECT_TOWER, TRIGGER_LEFT_CLICK);
        inputManager.addMapping(MAPPING_CHARGE_TOWER, TRIGGER_KEY_C);
        
        ActionListener selectListener = new ActionListener() {
            public void onAction(String name, boolean isPressed, float tpf) {
                if(name.equals(MAPPING_SELECT_TOWER)) {
                    if(isPressed == false) {
                        CollisionResults results = new CollisionResults();
                        Vector2f click2d = inputManager.getCursorPosition();
                        Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.getX(),
                                                                   click2d.getY()), 0f);
                        Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.getX(),
                                                               click2d.getY()), 1f).
                                                               subtractLocal(click3d);
                        Ray ray = new Ray(click3d, dir);
                        rootNode.collideWith(ray, results);
                        if(results.getClosestCollision() != null && results.getClosestCollision().getGeometry().
                            getControl(TowerControl.class) != null) {
                            TowerControl tower = results.getClosestCollision().getGeometry().
                                getControl(TowerControl.class);
                            selected = tower;
                        }
                        else
                            selected = null;
                    }
                }
            }          
        };
        
        ActionListener chargeListener = new ActionListener() {
            public void onAction(String name, boolean isPressed, float tpf) {
                if(name.equals(MAPPING_CHARGE_TOWER)) {
                    if(selected != null) {
                        TowerControl tower = selected;
                        ArrayList<Charges> charges = tower.getCharges();
                        charges.add(new Charges());
                        tower.setCharges(charges);
                        GamePlayAppState gamePlay = new GamePlayAppState();
                        gamePlay.setBudget(gamePlay.getBudget() - 1);
                        selected = null;
                    }
                }
            }
        };
        inputManager.addListener(selectListener, MAPPING_SELECT_TOWER);
        inputManager.addListener(chargeListener, MAPPING_CHARGE_TOWER);
    }


    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }


    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
    
}

As you can see, I am selecting towers by casting a ray from the visible cursor, and not the center of the screen. Now, I believe that the tower is actually receiving charges when I press c. In fact, there is a tower far enough from the enemies(creeps) that if I select, and press c it will shoot a bullet. Another problem arises from here, but I'll get to that later.

 

Here's the necessary code for the Tower Control:

package mygame;
 
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.shape.Line;
import java.util.ArrayList;
 
 
public class TowerControl extends AbstractControl {
    private int chargesNum;
    private int index;
    private int height = 2;
    private GamePlayAppState gamePlay;
    private ArrayList<Charges> charges = new ArrayList<Charges>();
    private ArrayList<Spatial> creeps = new ArrayList<Spatial>();    
    private ArrayList<Spatial> reachables = new ArrayList<Spatial>();
    
    TowerControl(GamePlayAppState gamePlay, int chargesNum, int index) {
        this.gamePlay = gamePlay;
        this.chargesNum = chargesNum;
        this.index = index;
        this.creeps = gamePlay.getCreeps();
    }
    
    @Override
    public void controlUpdate(float tpf) {
        Geometry bullet = null;
        for(Spatial creep: getCreeps()) {
            if(spatial.getLocalTranslation().distance(creep.getLocalTranslation()) < 5) {
                getReachables().add(creep);
            }
        }
        if(getReachables().size() > 0 && charges.size() > 0) {
            for(Spatial creep : getReachables()) {
                int bullets = charges.get(0).getRemainingShots();
                for(int i = bullets; i > 0; i--) {
                    Line line = new Line(spatial.getLocalTranslation().add(0, 3, 0), 
                                         creep.getLocalTranslation());
                    bullet = new Geometry("Bullet", line);
                    Material black = new Material(getGamePlay().getAssetManager(),
                                                  "Common/MatDefs/Misc/Unshaded.j3md");
                    black.setColor("Color", ColorRGBA.Black);
                    bullet.setMaterial(black);
                    getGamePlay().getRootNode().attachChild(bullet);
                }
                getGamePlay().getRootNode().detachChild(bullet);
                getCharges().remove(0);
            }
        }
    }

 

Now, my controlUpdate method is suppose to be repeatedly checking if there is a charge and if so use up the bullets on the nearest creep. I had some trouble on how to do this, but I tried my best. Sadly, an exception is being thrown from line 40, and I've tried so many things. I just can't figure it out.

 

Here's the stack trace: 

at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at mygame.TowerControl.controlUpdate(TowerControl.java:40)
at com.jme3.scene.control.AbstractControl.update(AbstractControl.java:112)
at com.jme3.scene.Spatial.runControlUpdate(Spatial.java:570)
at com.jme3.scene.Spatial.updateLogicalState(Spatial.java:688)
at com.jme3.scene.Node.updateLogicalState(Node.java:152)
at com.jme3.scene.Node.updateLogicalState(Node.java:152)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:244)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:744)

Now when I said I could manage to get a bullet to be shot(which is a simple drawn line) it stays there, doesn't detach itself from the rootNode, and doesn't creep the creep no matter what damage, health I set.

 

package mygame;


import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;


public class CreepControl extends AbstractControl{
    private int health;
    private int index;
    private GamePlayAppState gamePlay;
    
    CreepControl(GamePlayAppState gamePlay, int health, int index) {
        this.gamePlay = gamePlay;
        this.health = health;
        this.index = index;
    }
    
    @Override
    public void controlUpdate(float tpf) {
        if(health > 0)
            spatial.move(0, 0, -tpf);
        else if(health < 0) {
            gamePlay.getRootNode().detachChild(spatial);
            gamePlay.setBudget(gamePlay.getBudget() + 10);
        }       
        System.out.println(spatial.getLocalTranslation());
        if(spatial.getLocalTranslation().z > 0) {
            gamePlay.getRootNode().detachChild(spatial);
            gamePlay.setHealth(gamePlay.getHealth() - 20);
        }
    }
    
    @Override
    public void controlRender(RenderManager rm, ViewPort vp) {
        
    }




    public int getHealth() {
        return health;
    }


    public void setHealth(int health) {
        this.health = health;
    }


    public int getIndex() {
        return index;
    }


    public void setIndex(int index) {
        this.index = index;
    }
}

Also, I used if(spatial.getLocalTranslation < 0), so they can detach themselves from the rootNode if that condition is met, but they just keep going on infinitely. 

 

I'll also show my appState class if that can help in any way.

package mygame;


import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import java.util.ArrayList;


public class GamePlayAppState extends AbstractAppState {
    private SimpleApplication app;
    private Node rootNode;
    private AssetManager assetManager;   
    private int health;
    private int level;
    private int budget;
    private int score;
    private ArrayList<Spatial> creeps = new ArrayList<Spatial>();
    private boolean lastGameWon;
    
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        this.setApp((SimpleApplication) app);
        this.setRootNode(this.getApp().getRootNode());
        this.setAssetManager(this.getApp().getAssetManager());
        
        createScene(getRootNode(), getAssetManager());
    }
    
    @Override
    public void update(float tpf) {
        
    }
    
    @Override
    public void cleanup() {
        getRootNode().detachAllChildren();
    }
    
    private void createScene(Node rootNode, AssetManager assetManager) {
    Box bFloor = new Box(33, 1, 33);
    Geometry floor = new Geometry("Box", bFloor);
    floor.setLocalTranslation(0, -5, 0);
    Material orange = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    orange.setColor("Color", ColorRGBA.Orange);
    floor.setMaterial(orange);


    Box bBase = new Box(2.5f, 1, 1);
    Geometry base = new Geometry("Box", bBase);
    Material yellow = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    yellow.setColor("Color", ColorRGBA.Yellow);
    base.setMaterial(yellow);


    Box bTower = new Box(.4f, 2, .4f);
    Geometry tower = new Geometry("Box", bTower);
    Material green = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    green.setColor("Color", ColorRGBA.Green);
    tower.setMaterial(green);
    Geometry tower2 = tower.clone();
    
    tower.setUserData("chargesNum", 10);
    tower2.setUserData("chargesNum", 10);
    tower.setUserData("index", 0);
    tower2.setUserData("index", 1);
    
    tower.addControl(new TowerControl(this, 100, 0));
    tower2.addControl(new TowerControl(this, 100, 1));
    
    


    Box bCreep = new Box(.4f, .4f, .4f);
    Geometry creep = new Geometry("Box", bCreep);
    Material black = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    black.setColor("Color", ColorRGBA.Black);
    creep.setMaterial(black);
    
    Geometry creep2 = creep.clone();
    Geometry creep3 = creep.clone();
    Geometry creep4 = creep.clone();
    Geometry creep5 = creep.clone();
    Geometry creep6 = creep.clone();
    
    creep.setUserData("health", 100);
    creep2.setUserData("health", 100);
    creep3.setUserData("health", 100);
    creep4.setUserData("health", 100);
    creep5.setUserData("health", 100);
    creep6.setUserData("health", 100);
    creep.setUserData("index", 0);
    creep2.setUserData("index", 1);
    creep3.setUserData("index", 2);
    creep4.setUserData("index", 3);
    creep5.setUserData("index", 4);
    creep6.setUserData("index", 5);
        
    creep.addControl(new CreepControl(this, 25, 0));
    creep2.addControl(new CreepControl(this, 25, 1));
    creep3.addControl(new CreepControl(this, 25, 2));
    creep4.addControl(new CreepControl(this, 25, 3));
    creep5.addControl(new CreepControl(this, 25, 4));
    creep6.addControl(new CreepControl(this, 25, 5));
    
    
    
        getCreeps().add(creep);
        getCreeps().add(creep2);
        getCreeps().add(creep3);
        getCreeps().add(creep4);
        getCreeps().add(creep5);
        getCreeps().add(creep6);




    Node playerNode = new Node();
    Node towerNode = new Node();
    Node creepNode = new Node();


    floor.setLocalTranslation(0, -2, 1);
    base.setLocalTranslation(Vector3f.ZERO);


    tower.setLocalTranslation(-2.2f, 0, 4);
    tower2.setLocalTranslation(2.5f, 0, 5.5f);


    creep.setLocalTranslation(0, -0.6f, 12);
    creep2.setLocalTranslation(1, -0.6f, 9f);
    creep3.setLocalTranslation(-1, -0.6f, 15);
    creep4.setLocalTranslation(1.5f, -0.6f, 18);
    creep5.setLocalTranslation(1.1f, -0.6f, 19);
    creep6.setLocalTranslation(-1.15f, -0.6f, 20);




    rootNode.attachChild(floor);


    rootNode.attachChild(playerNode);
    playerNode.attachChild(base);


    rootNode.attachChild(towerNode);
    towerNode.attachChild(tower);
    towerNode.attachChild(tower2);


    rootNode.attachChild(creepNode);
    creepNode.attachChild(creep);
    creepNode.attachChild(creep2);
    creepNode.attachChild(creep3);
    creepNode.attachChild(creep4);
    creepNode.attachChild(creep5);
    creepNode.attachChild(creep6);        
    }

 

I'm really begging for help on this, so please help me debug my code on step at a time.

 

 

 


Edited by cooljava50544, 13 August 2014 - 11:39 AM.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.