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

2023/4/12

How to make a loot dropping system with chance attached to it

Deepfist Deepfist

2023/4/12

#
So I'm trying to make a system where enemies have a chance to drop a powerup or weapon on dead and I have no idea how to program something like that. The weapons can only spawn as long as the player doesn't own that weapon. The powerups can always spawn. For this I could use a getRandomNumber code where the int needs to equal a given number (or another random int, if that's possible). The weapon owning part is already covered; all I need is the randomized part, for stopping the randomized part I can probably think of a code myself (if not then you'll see that). The loot can only spawn once the enemy is dead, the code for that is the following:
public boolean Dead() {
      if (Easy == true) {
        if (EnemiesHealth <= 0) {
            return true;
        }
      }
      if (Normal == true) {
        if (EnemiesHealth <= 0) {
            return true;
        }
      }
      if (Hard == true) {
        if (EnemiesHealth <= 0) {
            return true;
        }
      }
      return false;
If this code returns true, then the enemy is dead. If it returns false, then there is an else for different codes that work whilst the enemy is alive (the difficulty isn't important for any of the randomized stuff, it's just for the health and damage of enemies)
danpost danpost

2023/4/12

#
Deepfist wrote...
So I'm trying to make a system where enemies have a chance to drop a powerup or weapon on dead and I have no idea how to program something like that. The weapons can only spawn as long as the player doesn't own that weapon. The powerups can always spawn. For this I could use a getRandomNumber code where the int needs to equal a given number (or another random int, if that's possible). The weapon owning part is already covered; all I need is the randomized part, for stopping the randomized part I can probably think of a code myself (if not then you'll see that). The loot can only spawn once the enemy is dead, the code for that is the following: << Code Omitted >> If this code returns true, then the enemy is dead. If it returns false, then there is an else for different codes that work whilst the enemy is alive (the difficulty isn't important for any of the randomized stuff, it's just for the health and damage of enemies)
I do not see how the code given is relevant to the issue. You would not modify this method to chance spawning. What is important is the code which calls this method and what class it is in. I presume it is called from within the same class (the Enemies class, perhaps) although it has public access. If that is the case, then:
if (Dead()) {
    if (Greenfoot.getRandomNumber(10) == 0) {
        dropPoweverUp();
    }
    getWorld().removeObject(this);
}
should be close to what you want, where you need to add a dropPowerUp method to (absolutely) drop (add) a power-up -- one determined by the other factors (level, what player has, etc.).
Deepfist Deepfist

2023/4/13

#
Ok thx, didn't know that getRandomNumber also worked like that so that's my bad
You need to login to post a reply.