Skip to content

Commit 5db2759

Browse files
authored
fix: Created the loot table + Custom drawer for the drop
Created the loot table script, added the itemcontainer/drop to the tools and built a custom drawer for the drop property
2 parents 0fcafee + 2937dd1 commit 5db2759

File tree

15 files changed

+251
-2
lines changed

15 files changed

+251
-2
lines changed

Editor/DropDrawer.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using BigasTools.Rpg;
5+
using BigasMath;
6+
7+
using UnityEditor;
8+
namespace BigasTools.Editor{
9+
[CustomPropertyDrawer(typeof(Drop))]
10+
[CanEditMultipleObjects]
11+
public class DropDrawer : PropertyDrawer
12+
{
13+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
14+
{
15+
return base.GetPropertyHeight(property, label) + 75;
16+
}
17+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
18+
{
19+
EditorGUI.BeginProperty(position, label, property);
20+
21+
var title = new Rect(position.x, position.y - 8, 80, 20);
22+
var item = new Rect(position.x + 52, position.y + 10, 80, 20);
23+
var chance = new Rect(position.x + 50, position.y + 40, 50, 20);
24+
var itemName = new Rect(position.x, position.y + 10, 50, 20);
25+
var itemIcon = new Rect(position.x + 10, position.y + 30, 32, 32);
26+
var itemChance = new Rect(position.x, position.y + 60, 120, 20);
27+
28+
var indent = EditorGUI.indentLevel;
29+
EditorGUI.indentLevel = 0;
30+
31+
EditorGUI.LabelField(title, "DROP!");
32+
33+
34+
EditorGUI.PropertyField(item, property.FindPropertyRelative("item"), GUIContent.none);
35+
EditorGUI.PropertyField(chance, property.FindPropertyRelative("chance"), GUIContent.none);
36+
37+
var c = property.FindPropertyRelative("chance").intValue;
38+
if(c!=0){
39+
EditorGUI.LabelField(itemChance, $"{BMathPercentage.GetPercentageFromFloat(1, c)}% (1/{c})");
40+
}
41+
42+
EditorGUI.DrawRect(itemName, Color.red);
43+
44+
45+
var p = property.FindPropertyRelative("item");
46+
var data = p.objectReferenceValue as ScriptableObject;
47+
if(data!=null){
48+
SerializedObject obj = new SerializedObject(data);
49+
var name = obj.FindProperty("itemName");
50+
var texture = obj.FindProperty("sprite");
51+
EditorGUI.LabelField(itemName, name.stringValue);
52+
var a = AssetPreview.GetAssetPreview(texture.objectReferenceValue);
53+
if(a!=null){
54+
EditorGUI.DrawPreviewTexture(itemIcon, AssetPreview.GetAssetPreview(texture.objectReferenceValue));
55+
}
56+
}
57+
58+
EditorGUI.indentLevel = indent;
59+
60+
EditorGUI.EndProperty();
61+
}
62+
}
63+
}

Editor/DropDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/InputProfileDrawer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
[CustomPropertyDrawer(typeof(InputProfile))]
1010
public class InputProfileDrawer : PropertyDrawer
1111
{
12+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
13+
{
14+
return base.GetPropertyHeight(property, label);
15+
}
1216
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1317
{
1418
EditorGUI.BeginProperty(position, label, property);

Runtime/Loot-Table.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Loot-Table/LootTable.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace BigasTools.Rpg{
6+
public static class LootTable
7+
{
8+
public static Item GetDrop(Drop drop, int rolls){
9+
for (int i = 0; i < rolls; i++)
10+
{
11+
var r = drop.Rolled();
12+
if(!r)continue;
13+
BDebug.Log($"The object {drop.item.itemName} was rolled!", "Loot Table", Color.red);
14+
return drop.item;
15+
}
16+
return null;
17+
}
18+
}
19+
}

Runtime/Loot-Table/LootTable.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/ScriptableObjects.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/ScriptableObjects/Item.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
6+
namespace BigasTools.Rpg{
7+
8+
[CreateAssetMenu(fileName = "Item", menuName = "Drops/Items")]
9+
public class Item : ScriptableObject
10+
{
11+
public string itemName = "Item";
12+
public string description = "Base Description";
13+
public int quantity = 1;
14+
public Sprite sprite;
15+
}
16+
[System.Serializable]
17+
public class ItemDrop{
18+
public Drop[] drops;
19+
20+
public ItemDrop(Drop[] drops)
21+
{
22+
this.drops = drops;
23+
}
24+
}
25+
[System.Serializable]
26+
public class Drop{
27+
public Item item;
28+
public int chance = 16;
29+
30+
public Drop(Item item, int chance)
31+
{
32+
this.item = item;
33+
this.chance = chance;
34+
}
35+
public bool Rolled(){
36+
var i = Random.Range(0, chance);
37+
return i == 1;
38+
}
39+
}
40+
}

Runtime/ScriptableObjects/Item.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Samples/EntityExamples.unity

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,13 @@ MonoBehaviour:
638638
inputData:
639639
profile:
640640
- inputName: Interaction
641-
inputKey: 51
641+
inputKey: 53
642642
joystickKey: 0
643643
- inputName: Pause
644644
inputKey: 279
645645
joystickKey: 4
646646
- inputName: Info
647-
inputKey: 27
647+
inputKey: 115
648648
joystickKey: 5
649649
- inputName: Example
650650
inputKey: 39
@@ -707,4 +707,10 @@ MonoBehaviour:
707707
m_Script: {fileID: 11500000, guid: c33aa35226c7ef8459c14419ba3f25a3, type: 3}
708708
m_Name:
709709
m_EditorClassIdentifier:
710+
itemDrop:
711+
- item: {fileID: 11400000, guid: b27eeae83168bce49ba810731a0200b5, type: 2}
712+
chance: 15
713+
- item: {fileID: 11400000, guid: 93bad3294d8a97445bbf3cc7b821f667, type: 2}
714+
chance: 256
715+
roll: 1
710716
entityToSpawn: {fileID: 4739109322253644523, guid: 0a001269a7880c74bb4ce4d03dd45397, type: 3}

0 commit comments

Comments
 (0)