need your opinion on my loot system

alright i have a loot system that is based on the EnemyType and was wondering if the forum could give me critical feedback on how i did it.

Rundown : when an enemy dies it calls the LootTable() function and passes its transform and its EnemyType.

after building the loot table for that particular EnemyType, it uses a random number 0-100 ish to decide what rarity to drop, then uses ANOTHER random number to chose WHAT rare item to drop. initially each enemy was going have a chance to drop 2-3 items, but i’m thinking i should reserve that for boss loot only.

i have a question, should i build the loot table as totally random? or have a slot for rare loot and 2 slots for common loot drops? meaning common loot always drops, and there is just a chance for something less common to drop?

again critical feedback is welcome

-Lamont

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Loot : MonoBehaviour {
    public ItemDatabase itemDatabase;
    public List<Item> lootList = new List<Item>();
    public List<Item> rareCollection = new List<Item>();
    public List<Item> itemDrop = new List<Item>();
    public Rigidbody2D physicalLootPrefab;
    float lootRNG;
    int pickerRNG;
    void Start () {
        itemDatabase = GetComponent<ItemDatabase> ();
    }
    public void LootTable(Transform enemy, EnemyStats type){
        switch (type.enemyType){
        case EnemyStats.EnemyType.Lowly:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Common || rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
                   rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Basic:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Common || rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
                   rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Hard:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
                   rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Supporter:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Devout:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Hardned:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Crazed:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.BadAss:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.WarForged:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Insane:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.Zealot:{
            lootList = new List<Item>();
            for(int i = 0; i < itemDatabase.items.Count; i++){
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                   rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
            }
            LootPicker(enemy);
            break;
        }
        case EnemyStats.EnemyType.RightHand:{
            lootList = new List<Item>();
            break;
        }
        case EnemyStats.EnemyType.Boss:{
            lootList = new List<Item>();
            break;
        }
        }
    }
    void LootPicker(Transform enemy){
        lootRNG = Random.Range (0.0f, 100.01f);
        int lootint = 0;
        for(int j = 0; j < 1; j++){
            if (lootRNG >= 50.0f && lootRNG < 75.0f) {
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Common){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG >= 75.0f && lootRNG < 87.5f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Uncommon){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 87.5f && lootRNG <= 93.7f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Rare){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 93.7f && lootRNG <= 97.0f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Rarer){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 97.0f && lootRNG <= 99.0f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.SaughtAfter){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 99.0f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Legend){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootint >= 1){
                pickerRNG = Random.Range (0, rareCollection.Count);
                itemDrop.Add(rareCollection[pickerRNG]);
                rareCollection = new List<Item>();
                MakeLoot (enemy);
            }
            //yield return new WaitForSeconds(0.2f);
        }
    }
    void MakeLoot(Transform target){
        Vector3 position = new Vector3(target.localPosition.x, target.localPosition.y, 0);
        physicalLootPrefab = Instantiate(physicalLootPrefab, position, Quaternion.identity)as Rigidbody2D;
    }
}

Looks like the principles are there - I have a quick loop fix that will simplify your LootTable Function

In one case you have

case EnemyStats.EnemyType.Lowly:{
lootList = new List();
for(int i = 0; i < itemDatabase.items.Count; i++){
Item.Rarity rarity = itemDatabase.items[i].itemRarity;
if(rarity == Item.Rarity.Common || rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
lootList.Add (itemDatabase.items[i]);
}
}
LootPicker(enemy);
break;
}


But looking at all the other cases, they share the first 3 lines of the case in common, you could put this outside the switch and you wouldn't need to add it in each case. Same with the last line, 'LootPicker(enemy)' can be added after the switch and doesn't need to be in every case

```csharp
lootList = new List<Item>();
for(int i = 0; i < itemDatabase.items.Count; i++){
Item.Rarity rarity = itemDatabase.items[i].itemRarity;

Adding these before the switch, the LootTable function begins like

lootList = new List<Item>();
for(int i = 0; i < itemDatabase.items.Count; i++){
Item.Rarity rarity = itemDatabase.items[i].itemRarity;
switch (type.enemyType){
        case EnemyStats.EnemyType.Lowly:{
             Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Common || rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
                   rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
            break;
        }

And then after your switch, (after typing all cases like this)

//Last case
}//end switch
}//end for loop
LootPicker(enemy);

Edit: Since you have a few unique cases that are different from the other loops, I would suggest making them work in the loop somehow or adding a custom ‘if’ to modify them after the loop

hmmm, for some reason i figured the switch statement was a function by its self. i don’t know why i thought that.
so instead of putting the for loop inside the switch statement, you suggested to check the switch with each iteration within the for loop. i like it.

I haven’t decided fully what enemies drop what loot yet. so whatever is inside the switch statement is not set in stone, just place holders at the moment.
what is your opinion on having 3 separate lists handling the loot? is there a better way you can think of?

-Lamont

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Loot : MonoBehaviour {
    public ItemDatabase itemDatabase;
    public List<Item> lootList = new List<Item>();
    public List<Item> rareCollection = new List<Item>();
    public List<Item> itemDrop = new List<Item>();
    public Rigidbody2D physicalLootPrefab;
    float lootRNG;
    int pickerRNG;
    void Start () {
        itemDatabase = GetComponent<ItemDatabase> ();
    }
    public void LootTable(Transform enemy, EnemyStats type){
        lootList = new List<Item>();
        for (int i = 0; i < itemDatabase.items.Count; i++) {
            switch (type.enemyType){
            case EnemyStats.EnemyType.Lowly:{
                Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                if(rarity == Item.Rarity.Common || rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
                   rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
                    lootList.Add (itemDatabase.items[i]);
                }
                break;
            }
            case EnemyStats.EnemyType.Basic:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Common || rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
                       rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.Hard:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Uncommon || rarity == Item.Rarity.Rare ||
                       rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter || rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.Supporter:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.Devout:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.Hardned:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.Crazed:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.BadAss:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.WarForged:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.Insane:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.Zealot:{
                    Item.Rarity rarity = itemDatabase.items[i].itemRarity;
                    if(rarity == Item.Rarity.Rare || rarity == Item.Rarity.Rarer || rarity == Item.Rarity.SaughtAfter ||
                       rarity == Item.Rarity.Legend){
                        lootList.Add (itemDatabase.items[i]);
                    }
                break;
            }
            case EnemyStats.EnemyType.RightHand:{
                break;
            }
            case EnemyStats.EnemyType.Boss:{
                break;
            }
            }
        }
        LootPicker(enemy);
    }
    void LootPicker(Transform enemy){
        lootRNG = Random.Range (0.0f, 100.01f);
        int lootint = 0;
        for(int j = 0; j < 1; j++){
            if (lootRNG >= 50.0f && lootRNG < 75.0f) {
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Common){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG >= 75.0f && lootRNG < 87.5f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Uncommon){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 87.5f && lootRNG <= 93.7f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Rare){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 93.7f && lootRNG <= 97.0f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Rarer){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 97.0f && lootRNG <= 99.0f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.SaughtAfter){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootRNG > 99.0f){
                for(int i = 0; i < lootList.Count; i++){
                    if(lootList[i].itemRarity == Item.Rarity.Legend){
                        rareCollection.Add(lootList[i]);
                        lootint += 1;
                    }
                }
            }
            if(lootint >= 1){
                pickerRNG = Random.Range (0, rareCollection.Count);
                itemDrop.Add(rareCollection[pickerRNG]);
                rareCollection = new List<Item>();
                MakeLoot (enemy);
            }
        }
    }
    void MakeLoot(Transform target){
        Vector3 position = new Vector3(target.localPosition.x, target.localPosition.y, 0);
        physicalLootPrefab = Instantiate(physicalLootPrefab, position, Quaternion.identity)as Rigidbody2D;
    }
}