Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly moving down a class hierarchy

Tags:

java

hierarchy

I'm currently working on an "item drop" system, where items (and their properties) are randomly generated upon slaying an enemy. The current system I have is a hierarchy of classes, with Item being the root super class. Each class has specific properties common to all subclasses.

Ideally upon dropping an item, program will randomly select one property of the item and move down the hierarchy tree accordingly. For example, the process is as follows:

Class | Randomly Selected Property determining path in tree:

Item | ItemType -> Equipable | EquipType -> Weapon | WeaponType -> etc.

Example code:

abstract class Item 
{   
    private Type itemType;
    private String itemName;
    private int itemLevel;
    private Rarity itemRarity;
    private boolean questItem;
    private int itemPrice;

    public Item(String name)
    {
        itemType = Type.randomize();
        itemName = name;
        itemLevel = randomizeLevel(pLvl, eLvl);
        itemRarity = Rarity.randomize(bonus, pLvl, eLvl, iLvl);
        questItem = false;
        itemPrice = determinePrice();
    }
}

Type is an enum deciding the next level in the hierarchy. With this system, I dont know how to let the program determine the next level, and then proceed to it.

The problem I've run into is that I've realized in order for this to work, I have to work from the bottom of the hierarchy, up. Is there a way for me to work from the top-down, meaning I can start with a basic Item class and procedurally work my way down (using the methods I created to randomly select the properties).

If not, is there a specific way I should implement this system?

like image 247
G Boggs Avatar asked Jan 24 '26 09:01

G Boggs


1 Answers

You can get a list of all your enums from the enum class - the order is the order in which they are declared so if you declare them in hierarchy order, you can traverse the list however you want. The relevant oracle doc is here:

https://docs.oracle.com/javase/tutorial/reflect/special/enumMembers.html

like image 171
pvg Avatar answered Jan 25 '26 22:01

pvg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!