This site requires JavaScript, please enable it in your browser!
Greenfoot back
darkmist002
darkmist002 wrote ...

2013/7/15

getting hp to scale with waves

darkmist002 darkmist002

2013/7/15

#
ok, last thing i have left for this game is to get the hp to scale when it finishes waves 5, 10, 15, etc (boss waves) and starts the spawning over again. i go the wave system fixed so it will spawn them, then when they all die a timer comes up to count down till the next wave, then the next wave starts. i have it where it spawns from 5 different enemy units depending on the wave number (hence the inc hp after waves 5 and so on). here's a link to the scenario: http://www.greenfoot.org/scenarios/8919 i put some coding i thought would work in the spawning class, but it didn't change their health, so i commented it out. spawning class
public class spawning extends Actor
{
    /**
     * Act - do whatever the spawning wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int spawnsMade = 0;
    private int spawnsAlive = 0;
    private static final int maxspawns = 25;
    private int unit = 0;
    private long time = System.currentTimeMillis();
    private long counter = 0;
    private long totaltime = 0;
    private Random rand = new Random();
    private int wave;
    private int spawnTimer = 0;
    private int spawnTime = 120;
    private int i = 0;
    private int hpinc = 0;
    /**
    //if the time is right for the spawn, it spawns a random unit from 1-4. will implement a spawning for 
    //bosses as well.
    //find out how to pause the timer for the wavetime when the pause button is pressed
       * add option in here for each minion to see if it's killed, and if so, add minerals, or in each class
    **/
    public void act() 
    {
        // Add your action code here.
        wave = ((tdpath)getWorld()).waveNum();
        if(wave <= 5 && spawnTime() && spawnsMade <= maxspawns)
        {
            spawn();
            spawnTimer = spawnTime;
        }
//         if(hpinc < 1 && wave > 5 && wave%5 > 0)
//         {
//             hpinc++;
//             ((tdpath)getWorld()).incSpawnHP();
//         }
//         if(wave%5 == 0)
//         {
//             hpinc = 0;
//         }
        if(wave > 5 && spawnTime() && spawnsMade <= maxspawns)
        {
            wave = wave%5;
            //((tdpath)getWorld()).
            spawn();
            spawnTimer = spawnTime;
        }
    } 
    
    //right now randomly spawns units from 1-4, but will add a spawn of bosses for certain waves which will 
    //be 5.    
    private void spawn()
    {
        if(spawnsMade < maxspawns)
        {
                switch(wave)
                {
                    case 1:
                    {
                        getWorld().addObject(new minion1(), 35, 100);  
                        spawnsMade++;  
                        spawnsAlive++;
                        break;
                    }
                    case 2:
                    {
                        getWorld().addObject(new minion2(), 35, 100);  
                        spawnsMade++;
                        spawnsAlive++;
                        break;
                    }
                    case 3:
                    {
                        getWorld().addObject(new minion3(), 35, 100);  
                        spawnsMade++;
                        spawnsAlive++;
                        break;
                    }
                    case 4:
                    {
                        getWorld().addObject(new minion4(), 35, 100);  
                        spawnsMade++;
                        spawnsAlive++;
                        break;
                    }
                    case 5:
                    {
                        getWorld().addObject(new Boss(), 35, 100);
                        spawnsMade++;
                        spawnsAlive++;
                        break;
                    }
                } 
        }
    }
//      put a method in here that counts how many spawns are left alive for starting the next wave when it's zero
//      or in the wave class
    private boolean spawnTime()
    {
        if(spawnTimer > 0)
        {
            spawnTimer--;
        }
        return spawnTimer == 0;
    }

    public int getSpawnsAlive()
    {
        return spawnsAlive;
    }
    
    public void setSpawnsAlive()
    {
        spawnsAlive --;
    }
    
    public boolean spawnedAll()
    {
        return spawnsMade == maxspawns;
    }
    
    public void setSpawnsMade()
    {
        spawnsMade = 0;
    }
}
world class
public class tdpath extends World
{
    /**have to add a ui for a menu to buy towers, to show towers/their prices and 
     * their abilities/stats. will have to add some music options so the player can 
     * choose from a few different musics to listen to. will try to add sound effects
     * to tower attacks, and also need to add tower attacks. need to add damage to the 
     * tower attacks that effects the spawn unit's health. will have to add health bars 
     * for units spawned as well. will also have to make a wave function so more than
     * one wave is in the game, so that spawns can be done in certain orders. also so
     * that boss waves can be made. make sure to scale unit health to waves, so make 
     * their health inc as the waves increase.
     * 
     * after game is made and playable, think of a way to make it where towers can't
     * be placed on the path, unless towers interfere with spawning paths, then figure
     * it out while making the game.
     */
    
    /**
     * Constructor for objects of class tdpath.
     * 
     */
     
    
    public Wave w = new Wave();
    public minerals m = new minerals(w);
    public stopMusic s = new stopMusic();
    public int mins;
    private long count = 0;
    private long lastSecond = 0;
    private int seconds = 20;
    private boolean timesUp = false;
    private fasttowerbutton f;
    private slowingtowerbutton sL;
    private damagetowerbutton dt;
    private splashtowerbutton st;
    private enemies E = new enemies();;
    private defenseSpot d;
    private LivesLeft life;
    private fasttower ft = new fasttower();
    private slowingtower slowT = new slowingtower();
    private damagetower damT = new damagetower();
    private splashtower splashT = new splashtower();
    private spawning spawn = new spawning();
    private Timer time = new Timer();
    public tdpath()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        life = new LivesLeft();
        d = new defenseSpot(life); 
        f = new fasttowerbutton(ft);
        sL = new slowingtowerbutton(slowT, m);
        dt = new damagetowerbutton(damT, m);
        st = new splashtowerbutton(splashT);        
        //addObject(new spawning(), 35, 100);
        addObject(spawn, 35, 100);
        addObject(d, 100, 580);
        addObject(life, 170, 550);        
        addObject(f, 905, 107);        
        addObject(sL, 905, 142);
        addObject(dt, 905, 174);
        addObject(st, 905, 207);
        addObject(m, 905, 63);
        addObject(w, 905, 21);
        addObject(new music1(), 905, 507);
        addObject(new music2(), 905, 539);
        addObject(new music3(), 905, 572);
        addObject(new stopMusic(), 905, 461);
        addObject(new buyingTowers(), 905, 260);
        addObject(new upgradingTowers(), 905, 400);
        addObject(new seeingRadius(), 905, 320);
        //addObject(new Timer(), 600, 580);
    }
    
    public void mineralsAdd(int mins)
    {
        m.addMinerals(mins);
    }
    
    public void mineralsSub(int mins)
    {
        m.subMinerals(mins);
    }
    
    public int getMins()
    {
        return m.getMinerals();
    }
    
    public int getLifeTaken()
    {
        return E.getLivesLost();
    }
    
    public void incSpawnHP()
    {
        double hp = E.getHealth();
        E.setHealth(hp*1.5);
    }
    
    public int waveNum()
    {
        return w.getWaveNumber();
    }
    
    public int getSpawnsAlive()
    {
        return spawn.getSpawnsAlive();
    }
    
    public void killSpawn()
    {
        spawn.setSpawnsAlive();
    }
    
    public boolean spawnsDone()
    {
        return spawn.spawnedAll();
    }
    
    public void waveNumSet(int num)
    {
        w.setWaveNumber(num);
    }
    
    public void setSpawnsMade()
    {
        spawn.setSpawnsMade();
    }
    
    public boolean timeUp()
    {
        return time.isTimeUp();
    }
    
    public void getTimer()
    {
        addObject(time, 400, 100);
    }
    
    public void setTimer()
    {
        time.setTime();
    }
    
    public void removeTime()
    {
        removeObject(time);
    }
}
spawning units parent class:
public class enemies extends Actor
{
    /**
     * Act - do whatever the enemies wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int x = 0;
    int y = 0;
    private int wave = 1; //get from wave class
    public double health;
    public int mins;
    public int lifeLost;
    public void act() 
    {
        // Add your action code here.
       move();
//         slowMove();
       checkHealth();
    }    
    
    public double getHealth()
    {
        return health;
    }
    
    public void decHealth(int damage)
    {
        health = health - damage;
        checkHealth();
    }
    
    public void setHealth(double hp)
    {
        health = hp;
    }
    
    public void checkHealth()
    {
        if (health <= 0)
        {
             //M.addMinerals(mins);   
             ((tdpath)getWorld()).mineralsAdd(mins);
             ((tdpath)getWorld()).killSpawn();
            ((tdpath)getWorld()).removeObject(this);
        }
    }
    
    public void move()
    {
        if(getWorld().getColorAt(getX()+1, getY()).getRed() < 10 && getWorld().getColorAt(getX()+1, getY()).getGreen() < 10 && x !=-1)      
            {      
                setLocation(getX() + 1,getY());    
                
                x = 1;
                y = 0;
            }      
            else if(getWorld().getColorAt(getX()-1, getY()).getRed() < 10 && getWorld().getColorAt(getX()-1, getY()).getGreen() < 10 && x !=1)      
            {      
                setLocation(getX()-1, getY());      
                
                x = -1;
                y = 0;
            } 
           
            else if(getWorld().getColorAt(getX(), getY()+1).getRed() < 10 && getWorld().getColorAt(getX(), getY()+1).getGreen() < 10 && y != -1)      
            {      
                setLocation(getX(), getY()+1);      
                x = 0;
                y = 1;
            }
            else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 10 && getWorld().getColorAt(getX(), getY()-1).getGreen() < 10 && y !=1)      
            {      
                setLocation(getX(), getY()-1);   
                x = 0;
                y = -1;
            }
    }
    
    public void slowMove()
    {
       
    }
    
    public int getLivesLost()
    {
        return lifeLost;
    }
}
darkmist002 darkmist002

2013/7/15

#
a separate question from ^. how do you get the speed bar to show up w/o unlocking it? don't want them to change the actors places, but the spawns move so slow i thought an option for getting them to move faster would be nice.
danpost danpost

2013/7/15

#
darkmist002 wrote...
a separate question from ^. how do you get the speed bar to show up w/o unlocking it? don't want them to change the actors places, but the spawns move so slow i thought an option for getting them to move faster would be nice.
You cannot have the speed bar (the one Greenfoot displays) control a specific object. You can however add a bar object of your own to allow the user to adjust a setting. I use my Bar class in quite a few of my scenarios. It is open source for anyone to use. My Bar Subclasses scenario may be helpful. You would subclass my Bar class with my MseActBar class which gives the bar user-changing functionality. You may want to look over how it is implemented. If you have any issues with it, I will be willing to help out.
darkmist002 darkmist002

2013/7/16

#
can it work the same way the speed bar does where it can be made to make the acts go faster? i can't speed the spawned units up themselves because they don't use the regular move methed that takes an int, they use a method made to allow them to follow a certain color (to go along a path). and i figured if there was a way to speed them up so they move faster, then i could also do a way to slow them down as well when a certain tower attacks them for a few seconds. but mainly wanted to know about the bar since it makes the acts go faster, hence the speeding them up, but i didn't want them to be able to move the actors since you can only place a tower once and moving the spawned units off of the path will make them not move.
danpost danpost

2013/7/16

#
You can use it to control the speed (like the speed slider does) by using 'Greenfoot.setSpeed(int)'. Set the range minimum to one and maximum to one hundred and any time the value of the bar changes, use the command with the int set to the value of the bar.
darkmist002 darkmist002

2013/7/16

#
ok, i'll try it. on another note, got the hp to scale with waves like i wanted (after every boss wave which is every 5th wave, the hp of the spawns doubles). had to put 2 methods in the enemies class that sees if the wave has gone past 5, is %5 >= 1, and also put a variable in there that can't be greater than 1 (to make sure it was only called once so the spawns don't get infinite health). then had it double the health if those conditions where met, and put an if up in the act method to check if it was past wave 5, if the spawns all spawned, and also if all the spawns where removed from the world, and if so, reset the variable used in the method back to 0 so it could be called once when a new wave starts.
danpost danpost

2013/7/16

#
A lot of the methods and checking can be eliminated if you do the action (adjusting the health) at the time the 'wave' value in incremented.
wave++;
if (wave > 5 && wave%5 == 1) health++;
The above is not exactly the code you need, but it is the idea behind that I am trying to explain. You are only changing the wave value once (per wave), so you are only checking its value once, and you are only incrementing the health value once when the conditions are right.
darkmist002 darkmist002

2013/7/16

#
tried with wave%5 ==1 one before, but it only did it for the one wave, the other waves didn't change, which is why i did it the way i did it.
danpost danpost

2013/7/16

#
Again, let me say that what code I gave was just for an example, not the exact code you needed. With your last post, I am not able to determine if it now works correctly with code using that idea or you still have a problem with it. If you are still having a problem with it, it may not be the particular code you are using here causing the problem (it may be something elsewhere).
darkmist002 darkmist002

2013/7/16

#
i had gotten it to work before, but it works till wave 11. (it just makes it double no matter the wave, so i need to change it) it doesn't save the current hp that i changed it to, so i'm gonna have to make a formula that doubles it for each set of waves past 5 (ie 6 would be modulus%5 = 1, so the hp would double; 11 would also be = 1, but it would have taken 2 to do the division, so the hp should quadruple, and so forth). is there something that indicates how many 5's went into the division?
danpost danpost

2013/7/16

#
'int x = wave/5;' would set x to the number of 'sets of 5 waves' completed. You could use '(int)Math.pow(2, x)' or '(int)Math.pow(2, x-1)' to get the current doubling.
darkmist002 darkmist002

2013/7/17

#
that works, thanks. can't believe i didn't think of something that simple. guess i got so caught up by using modulus division i forgot about regular lol.
You need to login to post a reply.