0
votes

This is a "tower defense" game. I have a class called soldier.

    if(wave1==true){
        for(int i=0;i<(wave*10);i++){
            handler.addObject(new soldier(getX(), getY(), ID.soldier, handler));
            try {
            //  System.out.println(i);
                Thread.sleep(500);

            } catch (InterruptedException e) {

            }

        }
        wave++;
        wave1=false;
        try {
        //  System.out.println(i);
            Thread.sleep(WT);

        } catch (InterruptedException e) {

        }
        }

Every time this code is called is the soldier gets spawned 10 times for every wave, but I have one issue. I would like the tower 1 to kill the furthest soldier in range. The code for the killing:

    private void collision() {
    for(int i=0; i< Handler.object.size();i++){
        GameObject tempObject = Handler.object.get(i);
        if(tempObject.getID()== ID.ST1){
            if(getBounds().intersects(tempObject.getBounds())){         
                Hsol=(Hsol-1);

                if(Hsol<=0){
                    handler.removeObject(this);

                }
            }               
        }
        }
    }

So I know I need to do a distance formula to find which ones closer, but I'm not sure how to do that.

2
Please clarify, You want two things. 1.that soldier 1 will not get killed. 2. That the tower will 1st kill the farthest soldier in range. Is this correct?SteelToe
I didn't read further than if(wave1=true). You probably want if (wave1 == true), or much better: if (wave1). I would also try to choose much better names: having a boolean wave1 and an integer wave is quite confusing. WT, Hsol, ID don't respect naming conventions, don't mean anything, and make your code very hard to read, even for you.JB Nizet
@JBNizet i have the spawning system id like. i just need help getting the targeting system for the tower to work correctly.The Gaming Koala
@MarrieteCowby12 sorry i didnt catch that mistake while typing this. but the answer to your qustion is. i would like the tower to kill the furthest soldier in rangeThe Gaming Koala
explain why it wouldnt work pleaseSteelToe

2 Answers

1
votes

Given two points (x1, y1) and (x2, y2), the distance formula is

√[(x1-x2)^2 + (y1-y2)^2]

So, using that formula, you can loop through each soldier and see which one is closest to the point that the attack is coming from.

0
votes
private void collision() {
    for(int i=0; i< Handler.object.size();i++){
        GameObject tempObject = Handler.object.get(i);
        if(tempObject.getID()== ID.ST1){
        if(this==handler.object.get(3)){
            if(getBounds().intersects(tempObject.getBounds())){ 
                Hsol=(Hsol-1);

                if(Hsol<=0){
                    handler.removeObject(this);
                    HUD.coin=(HUD.coin+100);
                    instances=(instances-1);

                    //System.out.println(HUD.coin);
                }
            }


                }
            }               
        }
        }